英文:
Why does my Go app fail with "no such file or directory" inside a Docker container?
问题
我有一个在我的WSL2 Ubuntu上运行正常的Go 1.18应用程序,但在Docker容器中运行时出现错误消息exec /app: no such file or directory
。
我的Dockerfile(稍作修改,来自另一个正常工作的Go 1.12应用程序)如下:
FROM golang:1.18-alpine AS build
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY source/*.go ./
RUN go build -o /app
FROM gcr.io/distroless/static-debian11
COPY --from=build /app /app
USER nonroot:nonroot
CMD ["/app"]
构建过程没有显示错误。我尝试添加GOOS=linux
和GOARCH=amd64
,并使用--platform linux/amd64
进行构建,但没有任何区别(我认为这不是必需的)。我从Distroless切换到Debian,问题依然存在。
文件/app
存在(11 MB,755
权限)。file /app
的输出如下:
app: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, Go BuildID=UsV_orwX-S3Rwh16P1VH/6u2iHufDhnUYUkHBp0rE/2xn48wuW047ZRbQ7qPIy/ihQgooFxjsMgMzYGE-8h, not stripped
我无法弄清楚问题出在哪里。这里有什么问题?
英文:
I have a Go 1.18 app which runs without issues in my WSL2 Ubuntu, but fails to run in a Docker container with the error message exec /app: no such file or directory
.
My Dockerfile (slightly adapted from another Go 1.12 app that works without issues) is:
FROM golang:1.18-alpine AS build
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY source/*.go ./
RUN go build -o /app
FROM gcr.io/distroless/static-debian11
COPY --from=build /app /app
USER nonroot:nonroot
CMD ["/app"]
Building it shows no errors. I tried adding GOOS=linux
and GOARCH=amd64
and building with --platform linux/amd64
but it makes no difference (and should not be necessary I think?). I switched from Distroless to Debian, same issue.
The file /app
exists (11 mb, 755
). file /app
gives this output:
app: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, Go BuildID=UsV_orwX-S3Rwh16P1VH/6u2iHufDhnUYUkHBp0rE/2xn48wuW047ZRbQ7qPIy/ihQgooFxjsMgMzYGE-8h, not stripped
I can't figure out where I'm going wrong. What is the issue here?
答案1
得分: 1
似乎是由于使用了go-ping/ping引起的。将构建镜像切换为golang:1.18
(而不是Alpine),将最终镜像切换为gcr.io/distroless/base-debian11
可以解决这个问题。
英文:
Seems to be caused by using go-ping/ping. Switching the build image to golang:1.18
(not Alpine) and the final image to gcr.io/distroless/base-debian11
fixes the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论