403 Forbidden when downloading Go dependencies

Here’s the error:

 => ERROR [4/6] RUN go mod download                  6.9s
------                                                    
 > [4/6] RUN go mod download:                             
#7 6.683 go: github.com/klauspost/compress@v1.15.15: reading https://proxy.golang.org/github.com/klauspost/compress/@v/v1.15.15.zip: 403 Forbidden
------
Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c go mod download]: exit code: 1
make: *** [deploy] Error 1

My dockerfile:

FROM golang:1.18-alpine
LABEL version="1.0"
WORKDIR $GOPATH/src/github.com/febriliankr/PACKAGE_NAME
COPY . .
RUN go mod download
RUN go mod verify
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/rest main.go
EXPOSE 5000
ENTRYPOINT ["/go/bin/rest"]

I have also tried replacing go mod download with go mod tidy and still got the same error, please help, thanks!

I’m not sure why the Go module proxy is returning 403, but you can set the environment variable GOPROXY=direct to bypass it.

@lillian I still have that error

#12 58.16 /go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/binding/protobuf.go:12:2: unrecognized import path "google.golang.org/protobuf": reading https://google.golang.org/protobuf?go-get=1: 403 Forbidden

Error: failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c apk update && apk add --no-cache git && GOPROXY=direct go build -o app .]: exit code: 1