为什么我的 Go 应用在 Docker 容器中出现“没有该文件或目录”的错误?

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

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=linuxGOARCH=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.

huangapple
  • 本文由 发表于 2022年6月24日 14:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/72739892.html
匿名

发表评论

匿名网友

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

确定