Change directory command in Docker?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Docker is a powerful tool for creating and managing lightweight, portable containers. When interacting with containers, managing the directory structure of the filesystem is crucial, particularly during the build process. This article focuses on the concept of changing directories within the context of Docker, specifically exploring the WORKDIR command in Dockerfiles.
Understanding the Docker WORKDIR Command
In Docker, each container runs from a root filesystem created from a Docker image. Like any Unix-based system, you can navigate and manipulate this filesystem. However, instead of repeatedly specifying the path for every file-related operation, Docker provides the WORKDIR instruction in Dockerfiles to set a working directory for subsequent instructions like RUN, CMD, ENTRYPOINT, COPY, and ADD.
The WORKDIR Instruction
The WORKDIR command sets the working directory inside the container. If the directory does not already exist, it is created, ensuring that commands following WORKDIR operate within this specified directory.
Syntax
Example
Consider the following Dockerfile example:
In this Dockerfile:
- The
WORKDIRinstruction creates the/usr/src/appdirectory (if it doesn't already exist) and changes the current working directory to this path. - The
COPY . .command copies files from the build context on the host to the current working directory in the container, which is/usr/src/app. - The
RUNandCMDinstructions operate relative to this directory. This meansmake /usr/src/appexecutes within/usr/src/app, and./appassumes thatappis present in/usr/src/app.
Benefits of Using WORKDIR
- Simplifies File Operations: Eliminates the need to specify absolute paths for file operations, thereby enhancing readability and manageability.
- Consistency Across Builds: Guarantees a consistent working directory, preventing issues arising from relative path dependencies in scripts and commands.
- Improved Docker Layer Caching: Using
WORKDIReffectively can enhance layer caching. EachWORKDIRinstruction creates a new layer, so placing it strategically might reduce redundant layers and speed up builds.
WORKDIR and Multi-Stage Builds
Multi-stage builds are valuable for reducing the final image size by separating the build environment from the runtime environment. WORKDIR plays a crucial role in organizing these stages.
Example of Multi-Stage Build Using WORKDIR
- Build Stage: A
WORKDIRis specified to manage the build process of the Go application within the/go/src/appdirectory. - Deployment Stage: The binary is copied into
/usr/local/bin, with theWORKDIRfacilitating organization and execution.
Handling Multiple WORKDIR Instructions
In a Dockerfile, using multiple WORKDIR instructions consecutively updates the working directory each time. The path can be specified as absolute or relative:
The resulting working directory becomes /base/relative/path.
Summary Table
| Aspect | Description |
| Purpose | Sets the working directory for subsequent instructions. |
| Default Directory | /, the root of the filesystem. |
| Directory Creation | Automatically creates if the specified directory does not exist. |
| Typically Used With | RUN, CMD, ENTRYPOINT, COPY, ADD. |
| Multi-Stage Builds | Organizes and segregates the build and deploy stages. |
| Layer Impact | Creates a new layer per WORKDIR instruction. |
Conclusion
Leveraging WORKDIR in Dockerfiles simplifies path management, ensures consistency across operations, and can positively impact build efficiency. Understanding its use is essential for anyone seeking to optimize their Docker workflows, particularly when dealing with complex applications and multi-stage builds. By effectively employing WORKDIR, developers can simplify their Docker operations and enhance overall project organization.
Related reading
- Changing the default command of base docker images
- Check architecture in dockerfile to get amd/arm
- Checking Kubernetes pod CPU and memory utilization
- Clone private git repo with dockerfile
- Clone private git repo with dockerfile
- Cohabitation Docker VirtualBox on Windows
- Command running in kubernetes hangs
- Communicating and persisting data between apps with App Groups

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.