英文:
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 <executable_path>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论