$ PWD not set in ENV statement in Dockerfile

I have a Dockerfile that starts like this:

FROM ubuntu:16.04
WORKDIR /some/path
COPY . .
ENV PYTHONUSERBASE=$PWD/pyenv PATH=$PWD/pyenv/bin:$PATH
RUN echo "PWD is: $PWD"
RUN echo "PYENV is: $PYTHONUSERBASE"

      

I found that $PWD

(or ${PWD}

) was not installed when I ran docker build

, as a comparison it $PATH

was expanded correctly.

Moreover, $PWD

in RUN

has no problem (in which case it prints /some/path

)

So the output of the given Dockerfile will be:

PWD is: /some/path
PYENV is: /pyenv

      

Can anyone tell me why $PWD

so special? I am guessing it might be related to behavior WORKDIR

, but I am not aware of this.

+3


source to share


1 answer


PWD is a special variable that is set inside the shell. When docker RUN does something, it does it with this form sh -c 'something'

, passing in predefined environment variables from ENV statements where PWD is not on that list (see it with docker inspect <image-id>

).

ENV statements do not start the shell. Just add or update the current list of env vars in the image metadata.

I would write your Dockerfile like this:



FROM ubuntu:16.04
ENV APP_PATH=/some/path
WORKDIR $APP_PATH
COPY . .
ENV PYTHONUSERBASE=$APP_PATH/pyenv PATH=$APP_PATH/pyenv/bin:$PATH
RUN echo "PWD is: $PWD"
RUN echo "PYENV is: $PYTHONUSERBASE"

      

More info in the docs :

The WORKDIR statement sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD statements that follow it in the Dockerfile. If WORKDIR does not exist, it will be created even if it is not used in any subsequent Dockerfile statement.

+3


source







All Articles