WORKDIR command in Docker

Documentation on WORKDIR command in Docker

Follow @donadoio

WORKDIR command in Docker

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.


Docker WORKDIR Graph

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.04

WORKDIR /project

RUN npm install 

If the project directory does not exist, it will be created. The RUN command will be executed inside project.

Reusing 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.04

WORKDIR /project

RUN npm install 

WORKDIR ../project2

RUN 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.

Back to Docker for ft_server