简体   繁体   中英

Building a docker image to run go applications

I am building a docker image on mac OS (Monterrey) with below dockerfile

FROM golang:latest
WORKDIR /src
COPY go.* ./ 
RUN go mod download 
COPY . /src
RUN go build -o /main
ENTRYPOINT ["/main"]

It works fine til the 3rd line and on the 4th it complains of

=> ERROR [4/6] RUN go mod download                                                         0.2s
------                                                                                           
 > [4/6] RUN go mod download:
#8 0.206 go mod download: no modules specified (see 'go help mod download')
------
executor failed running [/bin/sh -c go mod download]: exit code: 1

Any clues what I am doing wrong while the dependencies are being downloaded?

I was missing couple of files from the Working directory as listed below. These are needed to download the dependencies.

Dockerfile //this was already in the folder. 
go.sum
go.mod
main.go

Instead of using download do:

go mod tidy && go mod vendor

You need to specify which package to download when you use "mod download"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM