Golang – 创建 Docker 镜像时出现问题

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

Golang - problem with creating docker image

问题

我尝试创建 Docker 镜像,但在终端中运行命令时遇到了错误。

sudo docker build testapi .

我得到了以下错误信息:

=> ERROR [6/6] RUN go build -o /app/testapi/cmd/test-api                                                                                                              0.3s
------
> [6/6] RUN go build -o /app/testapi/cmd/test-api:
#14 0.231 no Go files in /app
------
executor failed running [/bin/sh -c go build -o /app/testapi/cmd/test-api]: exit code: 1

文件结构如下:

/testapi
 /cmd
  /test-api
   maing.go
 /pkg
  /...
 Dockerfile

Dockerfile 内容如下:

FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod ./

RUN go mod download

COPY . ./

RUN go build -o /app/testapi/cmd/test-api

EXPOSE 8080

CMD ["/testapi/cmd/test-api"]
英文:

I try to make docker image, but when I run cmd in terminal.

sudo docker build testapi .

I get an error:

 => ERROR [6/6] RUN go build -o /app/testapi/cmd/test-api                                                                                                              0.3s
------
 > [6/6] RUN go build -o /app/testapi/cmd/test-api:
#14 0.231 no Go files in /app
------
executor failed running [/bin/sh -c go build -o /app/testapi/cmd/test-api]: exit code: 1

File structure

/testapi
 /cmd
  /test-api
   maing.go
 /pkg
  /...
 Dockerfile

Dockerfile:

FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod ./

RUN go mod download

COPY . ./

RUN go build -o /app/testapi/cmd/test-api

EXPOSE 8080

CMD [ "/testapi/cmd/test-api" ]

答案1

得分: 1

尝试这种方式。

如果有的话,不要忘记在Dockerfile中添加go.sumCOPY go.sum .

FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod .

COPY . .

RUN go build -o main ./cmd/<FOLDER_NAME>

WORKDIR /dist

RUN cp /app/main .

EXPOSE 3000

CMD ["/dist/main"]

运行正常

状态: 正在下载较新的镜像 golang:1.16-alpine
 ---&gt; 7642119cd161
步骤 2/9 : WORKDIR /app
 ---&gt; 正在运行 f8ad2994262c
正在删除中间容器 f8ad2994262c
 ---&gt; 6e079707cc3e
步骤 3/9 : COPY go.mod .
 ---&gt; c79a0cc04ff5
步骤 4/9 : COPY . .
 ---&gt; 897027112e73
步骤 5/9 : RUN go build -o main ./cmd/api
 ---&gt; 正在运行 eb4579f1c339
正在删除中间容器 eb4579f1c339
 ---&gt; e41f6e5542a4
步骤 6/9 : WORKDIR /dist
 ---&gt; 正在运行 92115865d1ec
正在删除中间容器 92115865d1ec
 ---&gt; 8152a0ebe4bc
步骤 7/9 : RUN cp /app/main .
 ---&gt; 正在运行 5c68cb195826
正在删除中间容器 5c68cb195826
 ---&gt; 6f2e1fb8a611
步骤 8/9 : EXPOSE 3000
 ---&gt; 正在运行 fba77820c1ec
正在删除中间容器 fba77820c1ec
 ---&gt; 182a624d807a
步骤 9/9 : CMD ["/dist/main"]
 ---&gt; 正在运行 164ba25694bd
正在删除中间容器 164ba25694bd
 ---&gt; 7cf22cffc472
成功构建 7cf22cffc472
成功标记 bla:latest
英文:

Try this way.

Also do not forget to add go.sum in Dockerfile if you have. COPY go.sum .

FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod .

COPY . .

RUN go build -o main ./cmd/&lt;FOLDER_NAME&gt;

WORKDIR /dist

RUN cp /app/main .

EXPOSE 3000

CMD [&quot;/dist/main&quot;]

Works fine

Status: Downloaded newer image for golang:1.16-alpine
 ---&gt; 7642119cd161
Step 2/9 : WORKDIR /app
 ---&gt; Running in f8ad2994262c
Removing intermediate container f8ad2994262c
 ---&gt; 6e079707cc3e
Step 3/9 : COPY go.mod .
 ---&gt; c79a0cc04ff5
Step 4/9 : COPY . .
 ---&gt; 897027112e73
Step 5/9 : RUN go build -o main ./cmd/api
 ---&gt; Running in eb4579f1c339
Removing intermediate container eb4579f1c339
 ---&gt; e41f6e5542a4
Step 6/9 : WORKDIR /dist
 ---&gt; Running in 92115865d1ec
Removing intermediate container 92115865d1ec
 ---&gt; 8152a0ebe4bc
Step 7/9 : RUN cp /app/main .
 ---&gt; Running in 5c68cb195826
Removing intermediate container 5c68cb195826
 ---&gt; 6f2e1fb8a611
Step 8/9 : EXPOSE 3000
 ---&gt; Running in fba77820c1ec
Removing intermediate container fba77820c1ec
 ---&gt; 182a624d807a
Step 9/9 : CMD [&quot;/dist/main&quot;]
 ---&gt; Running in 164ba25694bd
Removing intermediate container 164ba25694bd
 ---&gt; 7cf22cffc472
Successfully built 7cf22cffc472
Successfully tagged bla:latest

答案2

得分: 0

go build [-o 输出文件] [构建标志] [包名]

英文:

go build [-o output] [build flags] [packages]

huangapple
  • 本文由 发表于 2022年8月13日 17:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/73343208.html
匿名

发表评论

匿名网友

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

确定