Change directory command in Docker?
Categories:
Mastering Directory Changes in Docker: A Comprehensive Guide

Learn how to effectively manage working directories within Docker containers and Dockerfiles using WORKDIR
and other commands, ensuring your applications run smoothly.
When working with Docker, understanding how to navigate and set the working directory within your containers is crucial for building efficient and reliable images. Unlike traditional shell environments where you might frequently use cd
, Docker has its own mechanisms for managing the current directory. This article will explore the primary methods for changing directories in Docker, focusing on WORKDIR
in Dockerfiles and runtime considerations.
The WORKDIR
Instruction in Dockerfiles
The most common and recommended way to set the working directory inside a Docker container is by using the WORKDIR
instruction in your Dockerfile. This instruction sets the working directory for any RUN
, CMD
, ENTRYPOINT
, COPY
, and ADD
instructions that follow it in the Dockerfile. If the WORKDIR
does not exist, it will be created automatically.
FROM alpine:latest
# Set the working directory to /app
WORKDIR /app
# Subsequent commands will execute in /app
COPY . .
RUN ls -la
Using WORKDIR in a Dockerfile
WORKDIR
(e.g., /app
) to avoid unexpected behavior due to previous WORKDIR
instructions or the default root directory.How WORKDIR
Affects Subsequent Commands
Once WORKDIR
is set, all subsequent instructions that operate on the filesystem will use this directory as their base. This simplifies your Dockerfile by allowing you to use relative paths for files and commands. You can use multiple WORKDIR
instructions; each one will change the directory for the following instructions relative to the previous WORKDIR
if a relative path is provided, or set a new absolute path.
FROM alpine:latest
WORKDIR /app
COPY src/ ./
WORKDIR subfolder
RUN touch my_file.txt # This creates /app/subfolder/my_file.txt
Multiple WORKDIR instructions and their effect
flowchart TD A[Dockerfile Start] --> B{WORKDIR /app} B --> C[COPY src/ .] C --> D{WORKDIR subfolder} D --> E[RUN touch my_file.txt] E --> F[Result: /app/subfolder/my_file.txt] F --> G[Container Runtime]
Flow of WORKDIR instructions in a Dockerfile
Changing Directory at Container Runtime
While WORKDIR
is for building the image, you might also need to specify the working directory when running a container. This can be achieved using the -w
or --workdir
flag with the docker run
command. This flag overrides any WORKDIR
specified in the Dockerfile for the specific docker run
command.
docker run -it --workdir /tmp my_image bash
# Now you are in /tmp inside the container
Setting working directory at container runtime
WORKDIR
in your Dockerfile or with docker run -w
, the default working directory inside the container will be /
(the root directory).Why Not Use RUN cd ...
?
A common misconception for newcomers is to use RUN cd /path/to/dir
in a Dockerfile. This approach is ineffective because each RUN
instruction executes in a new shell, and the cd
command only changes the directory for that specific shell. The change is lost once that RUN
instruction completes. Subsequent RUN
instructions will start from the directory set by the last WORKDIR
or the default root directory.
FROM alpine:latest
RUN mkdir /mydata
RUN cd /mydata
RUN pwd # This will output '/', not '/mydata'
Incorrect use of RUN cd in a Dockerfile
sequenceDiagram participant Dockerfile participant Shell1 participant Shell2 Dockerfile->>Shell1: RUN mkdir /mydata Shell1-->>Dockerfile: Directory created Dockerfile->>Shell1: RUN cd /mydata Shell1-->>Dockerfile: Shell1's CWD is /mydata Note over Shell1: Shell1 exits, CWD reset Dockerfile->>Shell2: RUN pwd Shell2-->>Dockerfile: Outputs '/' Note over Dockerfile: WORKDIR is the correct approach
Why RUN cd
is ineffective in Dockerfiles