英文:
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.sum
。COPY 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
---> 7642119cd161
步骤 2/9 : WORKDIR /app
---> 正在运行 f8ad2994262c
正在删除中间容器 f8ad2994262c
---> 6e079707cc3e
步骤 3/9 : COPY go.mod .
---> c79a0cc04ff5
步骤 4/9 : COPY . .
---> 897027112e73
步骤 5/9 : RUN go build -o main ./cmd/api
---> 正在运行 eb4579f1c339
正在删除中间容器 eb4579f1c339
---> e41f6e5542a4
步骤 6/9 : WORKDIR /dist
---> 正在运行 92115865d1ec
正在删除中间容器 92115865d1ec
---> 8152a0ebe4bc
步骤 7/9 : RUN cp /app/main .
---> 正在运行 5c68cb195826
正在删除中间容器 5c68cb195826
---> 6f2e1fb8a611
步骤 8/9 : EXPOSE 3000
---> 正在运行 fba77820c1ec
正在删除中间容器 fba77820c1ec
---> 182a624d807a
步骤 9/9 : CMD ["/dist/main"]
---> 正在运行 164ba25694bd
正在删除中间容器 164ba25694bd
---> 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/<FOLDER_NAME>
WORKDIR /dist
RUN cp /app/main .
EXPOSE 3000
CMD ["/dist/main"]
Works fine
Status: Downloaded newer image for golang:1.16-alpine
---> 7642119cd161
Step 2/9 : WORKDIR /app
---> Running in f8ad2994262c
Removing intermediate container f8ad2994262c
---> 6e079707cc3e
Step 3/9 : COPY go.mod .
---> c79a0cc04ff5
Step 4/9 : COPY . .
---> 897027112e73
Step 5/9 : RUN go build -o main ./cmd/api
---> Running in eb4579f1c339
Removing intermediate container eb4579f1c339
---> e41f6e5542a4
Step 6/9 : WORKDIR /dist
---> Running in 92115865d1ec
Removing intermediate container 92115865d1ec
---> 8152a0ebe4bc
Step 7/9 : RUN cp /app/main .
---> Running in 5c68cb195826
Removing intermediate container 5c68cb195826
---> 6f2e1fb8a611
Step 8/9 : EXPOSE 3000
---> Running in fba77820c1ec
Removing intermediate container fba77820c1ec
---> 182a624d807a
Step 9/9 : CMD ["/dist/main"]
---> Running in 164ba25694bd
Removing intermediate container 164ba25694bd
---> 7cf22cffc472
Successfully built 7cf22cffc472
Successfully tagged bla:latest
答案2
得分: 0
go build [-o 输出文件] [构建标志] [包名]
英文:
go build [-o output] [build flags] [packages]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论