Search code examples
dockerdocker-entrypointworkdir

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


I have this second stage for my 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

However, if you run the image with the first entrypoint, you get:

/bin/sh: /main: not found

And if you run with the second entrypoint, you get:

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.

Note: Another interesting point is the COPY --from=builder ... where I need to set the dest as /service/ otherwise my file gets copied in the / directory


According to the documentation:

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


Any idea? Is it really relating to me or Docker/alpine?


Solution

  • The Entrypoint needs to be ./main.

    /main is an absolute path would refer to a main that was on your root.

    Since you are in /service you will need a relative path. You want ./main, which points to /service/main.