ScanSkill

WORKDIR

The WORKDIR command is used to define the working directory of a Docker container at any given time for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.

Syntax

WORKDIR /path/to/workdir

Note: It can be run multiple times in a Dockerfile. If WORKDIR doesn't exist, it will be created. And if a relative path is provided, it will be relative to previous WORKDIR instruction.

Example

  • Multiple WORKDIR instructions
WORKDIR /c
WORKDIR d
WORKDIR e
RUN pwd  // Output will be /c/d/e
  • Using environment variables
ENV DIRPATH=/path/to/directory
WORKDIR $DIRPATH/$DIRNAME
RUN pwd

And the output will be /path/to/directory/$DIRNAME

Note: If the path is not specified, the default working directory / will be used.