构建一个Docker镜像来运行Go应用程序

huangapple go评论90阅读模式
英文:

Building a docker image to run go applications

问题

我正在构建一个在 macOS(Monterrey)上的 Docker 镜像,使用以下 Dockerfile:

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

在第三行之前都正常工作,但在第四行时出现以下错误:

=> 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

请问在下载依赖项时我做错了什么?

英文:

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?

答案1

得分: 0

我在工作目录中缺少以下列出的几个文件。这些文件是下载依赖项所需的。

Dockerfile //这个文件已经在文件夹中了。
go.sum
go.mod
main.go
英文:

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

答案2

得分: -2

使用download命令时,需要指定要下载的包,而不是使用go mod tidy && go mod vendor命令。

英文:

Instead of using download do :

go mod tidy && go mod vendor

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

huangapple
  • 本文由 发表于 2022年2月10日 03:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/71055589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定