How to Change Working Directory Command in Docker
- Understanding Docker’s Working Directory
- Method 1: Using the Dockerfile
- Method 2: Using the docker run Command
- Method 3: Changing the Working Directory in a Running Container
- Conclusion
- FAQ

Changing the working directory in Docker is a fundamental aspect of container management that can significantly impact your development workflow. Whether you’re setting up a new project or modifying an existing one, understanding how to adjust the working directory is crucial.
This article will explore various methods to change the working directory in Docker, providing practical examples and clear explanations. By the end, you’ll have a solid grasp of how to manipulate your working directory effectively, making your Docker experience smoother and more efficient.
Understanding Docker’s Working Directory
Before diving into the methods, it’s essential to understand what the working directory is in the context of Docker. The working directory is the directory in which your commands will be executed when the container is running. By default, Docker uses the root directory, but this can be changed using the WORKDIR
instruction in your Dockerfile or the -w
option in the docker run
command.
Method 1: Using the Dockerfile
One of the most common methods to change the working directory in Docker is by specifying it in the Dockerfile. The WORKDIR
instruction allows you to set the working directory for any subsequent instructions in the Dockerfile. This is particularly useful for organizing your application files and dependencies.
Here’s a simple example of how to use the WORKDIR
instruction in your Dockerfile:
FROM ubuntu:latest
WORKDIR /app
COPY . .
RUN make /app
In this example, the working directory is set to /app
. This means that any subsequent commands, such as COPY
and RUN
, will be executed in the /app
directory of the container. This approach helps maintain a clean and organized structure, especially when dealing with multiple files and dependencies.
By using the WORKDIR
instruction, you ensure that your application is always executed in the correct directory, reducing the chances of errors related to file paths. Furthermore, if you need to change the working directory later in the Dockerfile, you can simply add another WORKDIR
instruction.
Method 2: Using the docker run Command
Another effective way to change the working directory in Docker is by using the -w
option with the docker run
command. This method is particularly useful for quick tests or when you want to override the working directory without modifying the Dockerfile.
Here’s how to do it:
docker run -w /app -v $(pwd):/app my_image
In this command, the -w /app
option sets the working directory to /app
when the container starts. The -v $(pwd):/app
option mounts the current directory to the /app
directory inside the container. This allows you to work with files directly from your host machine.
Using the -w
option is straightforward and efficient, especially for development environments where you frequently change directories. It allows you to quickly test changes without needing to rebuild your Docker image. This method is particularly handy for scripting and automation tasks, making it a valuable addition to your Docker toolkit.
Method 3: Changing the Working Directory in a Running Container
If you need to change the working directory of a running container, you can do so by using the docker exec
command. This command allows you to run a command inside an already running container, which can be useful for debugging or making temporary changes.
Here’s how you can change the working directory in a running container:
docker exec -it my_container bash -c "cd /app && exec bash"
In this command, docker exec -it my_container
attaches an interactive terminal to the running container named my_container
. The bash -c "cd /app && exec bash"
part changes the working directory to /app
and then starts a new bash shell in that directory.
This method is particularly useful for troubleshooting or making adjustments on the fly. However, keep in mind that this change is temporary and will not persist if the container is restarted. This approach is great for quick fixes but not ideal for long-term solutions.
Conclusion
Changing the working directory in Docker is a straightforward process that can significantly enhance your workflow. By using the WORKDIR
instruction in your Dockerfile, the -w
option in the docker run
command, or the docker exec
command for running containers, you have multiple options to manage your working directory effectively. Understanding these methods will empower you to create more organized and efficient Docker environments, whether you’re developing applications or running scripts.
FAQ
-
What is the default working directory in Docker?
The default working directory in Docker is the root directory (/
). -
Can I change the working directory after the container is running?
Yes, you can use thedocker exec
command to change the working directory in a running container temporarily.
-
What happens if I don’t specify a working directory?
If you don’t specify a working directory, commands will run in the root directory, which may lead to issues if your application relies on specific file paths. -
Is it possible to set multiple working directories in a Dockerfile?
Yes, you can use multipleWORKDIR
instructions in a Dockerfile to set different working directories for different stages of your build. -
How do I verify the current working directory in a running container?
You can use thepwd
command in a running container to verify the current working directory.
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn