使用golang:1.13
base时,我能够运行Web应用程序的docker映像,但使用scratch
时,我无法运行。工作的Dockerfile是:
FROM golang:1.13 AS builder
WORKDIR /app
COPY . .
RUN go build -o server
FROM golang:1.13
COPY --from=builder /app/server /app/server
COPY --from=builder /app/credentials/service-account.json /app/credentials/service-account.json
ENTRYPOINT ["/app/server"]
但是当我像这样将最终图像库更改为scratch
(第6行)时:
FROM golang:1.13 AS builder
WORKDIR /app
COPY . .
RUN go build -o server
FROM scratch # <-- CHANGED
COPY --from=builder /app/server /app/server
COPY --from=builder /app/credentials/service-account.json /app/credentials/service-account.json
ENTRYPOINT ["/app/server"]
我收到一个standard_init_linux.go:211: exec user process caused "no such file or directory"
错误。
要构建docker映像,请使用docker build -t myimage .
,要运行该映像,请使用docker run --rm -p 8080:8080 myimage:latest
。
该应用程序是一个基于Go的Web API,它使用Gin框架和GCP服务帐户来访问GCP服务(我在构建时复制的JSON文件。)
答案 0 :(得分:3)
如果您没有使用CGO(如@jakub所述),请尝试在构建中禁用CGO。
因此请在您的Dockerfile
中更改此行:
#RUN go build -o server
RUN CGO_ENABLED=0 go build -o server