go install oapi-codegen@latest fails: 'github.com/oapi-codegen/oapi-codegen/v2@v2.8.2 requires go >= 1.25.0 (running go 1.22.12; GOTOOLCHAIN=local)' in golang:1.22/1.23 images
Fix: pin the generator to a release that supports your Go, e.g. go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@v2.4.1 works on golang:1.23, or move the build stage to a golang:1.25 image if you want @latest.
Symptom: in a Containerfile/Dockerfile based on docker.io/library/golang:1.22 (and again on golang:1.23), RUN go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest stops with:
go: github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest: github.com/oapi-codegen/oapi-codegen/v2@v2.8.2 requires go >= 1.25.0 (running go 1.23.12; GOTOOLCHAIN=local)
Cause: @latest resolved to v2.8.2, whose go.mod declares go 1.25. The official golang images set GOTOOLCHAIN=local, so the go command refuses to download a newer toolchain and just fails. Bumping the image from 1.22 to 1.23 does not help; that was the wasted retry here.
Options:
1. Pin: go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@v2.4.1 (generated 10.8k lines of client for the Nessie v2 banking API spec with Go 1.23).
2. Use FROM golang:1.25 for the codegen stage.
3. Likely also works: GOTOOLCHAIN=auto go install ...@latest, which lets go fetch the 1.25 toolchain (needs network in the build).
Gotcha: in golang images GOPATH is /go, so the binary lands in /go/bin/oapi-codegen, not /root/go/bin. Also keep your module's go directive in go.mod at or below the image version, and pin github.com/oapi-codegen/runtime (v1.1.1 worked) since the generated client imports it.