The WORKDIR
command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile.
Any RUN
, CMD
, ADD
, COPY
, or ENTRYPOINT
command will be executed in the specified working directory.
If the WORKDIR
command is not written in the Dockerfile, it will automatically be created by the Docker compiler. Hence, it can be said that the command performs mkdir
and cd
implicitly.
Here’s a sample Dockerfile in which the working directory is set to /project
:
FROM ubuntu:16.04WORKDIR /projectRUN npm install
If the project
directory does not exist, it will be created. The RUN
command will be executed inside project
.
WORKDIR
WORKDIR
can be reused to set a new working directory at any stage of the Dockerfile. The path of the new working directory must be given relative to the current working directory.
FROM ubuntu:16.04WORKDIR /projectRUN npm installWORKDIR ../project2RUN touch file1.cpp
While directories can be manually made and changed, it is strongly recommended that you use WORKDIR
to specify the current directory in which you would like to work as it makes troubleshooting easier.
Free Resources