Docker - alpine 图像上的 WORKDIR 问题(多阶段构建)

Docker - WORKDIR issue on alpine image (Multi-Stages build)

我的 Dockerfile 有第二阶段:

############################################ MULTI STAGE BUILD PART 2 ##############################################

# Start from alpine image
FROM alpine

# Creating work directory
WORKDIR /service

# Copy the certificats and executable into new Docker image
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /service/main /service/
COPY --from=builder /service/.credentials /service/.credentials/

# Expose port
EXPOSE ${GRPC_PORT}
EXPOSE ${REST_PORT}

## Get required ARGs and put them into ENVs variables
ARG ENVIRONMENT
ARG NAMESPACE
ARG GRPC_PORT
ARG REST_PORT
ENV _ENVIRONMENT=${ENVIRONMENT}
ENV _NAMESPACE=${NAMESPACE}
ENV _GRPC_PORT=${GRPC_PORT}
ENV _REST_PORT=${REST_PORT}

### HERE YOU CAN TEST WITH ANY OF THE FOLLOWING ENTRYPOINT

# The One I need
ENTRYPOINT /main "ENVIRONMENT=${_ENVIRONMENT}" "NAMESPACE=${_NAMESPACE}" "GRPC_PORT=${_GRPC_PORT}" "REST_PORT=${_REST_PORT}"

# This one isn't able to resolve ENVs variables, but I use it as an example for my ISSUE
ENTRYPOINT [ "/main" ]

As you can see, the WORKDIR is set to be /service

但是,如果您 运行 具有第一个入口点的图像,您将得到:

/bin/sh: /main: not found

如果你 运行 使用第二个入口点,你会得到:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"/main\": stat /main: no such file or directory": unknown.

注意: 另一个有趣的地方是 COPY --from=builder ... 我需要将 dest 设置为 /service/ 否则我的文件会被复制到 / 目录


根据documentation:

The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.


有什么想法吗?真的和我有关还是Docker/alpine?

Entrypoint 需要./main.

/main 是一个绝对路径,指的是根目录下的主路径。

由于您在 /service 中,因此您需要一个相对路径。你想要 ./main,它指向 /service/main.