Saturday, August 21, 2021

go: go.mod file not found in current directory or any parent directory; see 'go help modules'"

 Run from the terminal:

go mod init example.com/m/v2


Copy the generated file before you build the go program in the Dockerfile:

 

FROM golang:alpine

WORKDIR /myapp

COPY welcome.go .

COPY go.mod .


RUN go build -o welcome .

ENTRYPOINT ["./welcome"]


Make the generation of go.mod as part of the docker container:


FROM golang:latest AS builder

WORKDIR /myapp

COPY welcome.go .


RUN go mod init example.com/m/v2

RUN go build -o welcome .


FROM scratch

WORKDIR /myapp

COPY --from=builder /myapp/welcome .

ENTRYPOINT ["./welcome"]