多阶段 Dockerfile 中二进制文件路径错误

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

Error in path to binary in multistage Dockerfile

问题

我在尝试在多阶段的Dockerfile中运行我的可执行文件时遇到了困难。

我一直收到以下错误信息:

"Exec user process caused: no such file or directory unknown"

这个错误与我在第一阶段构建的二进制文件的路径有关,所以我知道我引用它的方式是错误的。

有人能看出我哪里出错了吗?WORKDIR参数是否也适用于构建的第二阶段?

#####################################
# PHASE 1: build an executable binary
#####################################
FROM golang:alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o server .

#####################################
# PHASE 2: build a small disk image
#####################################
FROM scratch
COPY --from=builder /app/server /app/server
EXPOSE 8000
CMD [ "/app/server" ]
英文:

I'm lost at trying to get my executable to run in a multistage Dockerfile.

I keep getting

"Exec user process caused: no such file or directory unknown"

errors relative to the path to the binary I've built in the first stage, so I know I'm referencing it wrong.

Can anyone see where I'm going wrong? Does the WORKDIR argument apply to the second stage of the build, as well?

#####################################
# PHASE 1: build an executable binary
#####################################
FROM golang:alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o server .

#####################################
# PHASE 2: build a small disk image
#####################################
FROM scratch
COPY --from=builder /app/server /app/server
EXPOSE 8000
CMD [ "/app/server" ]

答案1

得分: 1

当您在同一台机器上构建可执行文件时,如果没有显式关闭CGO_ENABLED,它会构建动态链接的二进制文件;而在交叉编译时,它会尝试构建静态二进制文件。您可以在构建可执行文件后使用ldd <executable_path>来确认是否动态链接。

因此,您是在alpine基础上构建,然后将其复制到scratch基础上,而scratch基础上没有这些需要进行动态链接的库。

因此,首先构建静态二进制文件,然后再复制,它应该可以运行。尝试使用CGO_ENABLED=0 go build -o server .命令,看看是否有效。根据您的代码可能还有其他情况,但基本上您需要创建一个静态链接的可执行文件,它应该可以在无发行版的基础上正常运行。

英文:

When you are building the executable for the same machine without explicitly turning off CGO_ENABLED then it does build binaries that are dynamically linked and when cross-compiling it tries to build a static binary. You can confirm the same by using ldd &lt;executable_path&gt; after building the executable if it's dynamically linked or not.

So, you are building on an alpine base and then copying it to the scratch base which doesn't have those libraries to be dynamically linked.

Hence, build static binaries first and then copy the same, it should run. See if CGO_ENABLED=0 go build -o server . works or not. There could be more depending on your code; so basically you have to create a statically linked executable and it should run fine on a distro-less base.

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

发表评论

匿名网友

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

确定