3

我的 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" ]

如您所见,WORKDIR 设置为 /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/否则我的文件会被复制到/目录中


根据文档

WORKDIR 指令为 Dockerfile 中任何 RUN、CMD、ENTRYPOINT、COPY 和 ADD 指令设置工作目录。


任何想法?它真的与我或 Docker/alpine 有关吗?

4

1 回答 1

2

Entrypoint需要是./main 。

/main是一个绝对路径将引用您根目录上的主路径。

既然你在里面,/service你将需要一个相对路径。你想要./main的,指向的/service/main

于 2020-02-20T19:41:16.770 回答