The Dockerfile WORKDIR
directive
... sets the working directory.... If the WORKDIR
doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile
instruction.
I occasionally see SO questions that RUN mkdir
a directory before switching WORKDIR
to it. Since WORKDIR
will create the directory, this isn't necessary.
All paths in a Dockerfile are always inside the image, except for the source paths for COPY
and ADD
instructions, which are inside the build context directory on the host. Absolute paths like /code
will be directly inside the root directory in the image, following normal Unix conventions.
You can run temporary containers off of your image to examine this, even if the Dockerfile isn't complete yet.
host$ docker build -t my-image .
host$ docker run --rm my-image ls -l /
host$ docker run --rm -it my-image /bin/sh
0123456789ab# ls -l /
0123456789ab# exit
(This will always work, assuming the image includes core tools like sh
and ls
. docker exec
requires the container to be running first; while you're refining the Dockerfile this may not be possible yet.)