Golang-创建docker镜像的问题



我试图制作docker映像,但当我在终端中运行cmd时。

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:

来自戈兰:1.16-阿尔卑斯

WORKDIR/app

复制go.mod./

RUN go mod下载

副本../

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

暴露8080

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

试试这种方法。

此外,如果您有,请不要忘记在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"]

工作良好

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

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

最新更新