Docker images comprise a set of sequential instructions that serve as a template to build a container. In this tutorial, we'll learn how to change the directory when building a Docker image or when running a container using an image.
2. Using the WORKDIR Instruction
First, let's start by spawning a Docker container using the readily available ubuntu:latest image:
$ docker run -it ubuntu:latest [email protected]:/# pwd /
We can see that as soon as the container is up, the current directory is set as /.
Next, let's say we want to change this directory to /tmp at container startup. We can do this by using the WORKDIR instruction inside a custom image that uses the ubuntu:latest as the base image:
$ cat custom-ubuntu-v1.dockerfile FROM ubuntu:latest WORKDIR /tmp
Before we can run a container using this image, we'll need to build the image. So, let's go ahead and build the custom-ubuntu:v1 image:
$ docker build -t custom-ubuntu:v1 - < ./custom-ubuntu-v1.dockerfile
Finally, let's run a container using the custom-ubuntu:v1 image and verify the current directory:
$ docker run -it custom-ubuntu:v1 [email protected]:/tmp# pwd /tmp
It looks like we've got this right!
3. Using the –workdir Option
Using the WORKDIR instruction is the recommended practice for most cases where we want to change the directory when building the Docker image. Nevertheless, if our use case is limited to changing the directory when running the container, then we can achieve this by using the –workdir option:
$ docker run --workdir /tmp -it ubuntu:latest [email protected]:/tmp# pwd /tmp
Looking at this, we can appreciate the command's brevity and the fact that we didn't have to create a custom image in this case.
4. Using the cd Command
In Linux, the cd command is the standard way to change the directory for most use cases. On the same note, when working with some docker instructions such as RUN, CMD, and ENTRYPOINT, we can use the cd command to change the directory for the current command in context.
Let's start by writing the custom-ubuntu-v2.dockerfile to use the RUN instruction with the cd command:
FROM ubuntu:latest RUN cd /tmp && echo "sample text" > data.txt
We can see that the intention is to write “sample text” to the /tmp/data.txt file.
Next, let's add the ENTRYPOINT instruction to run bash as the default command on container startup. Additionally, we use the cd command to change the current directory to the /tmp directory:
ENTRYPOINT ["sh", "-c", "cd /tmp && bash"]
Moving on, let's build the custom image:
$ docker build -t custom-ubuntu:v2 - < ./custom-ubuntu-v2.dockerfile
Finally, let's run the container using the custom-ubuntu:v2 image and verify the execution of the commands:
$ docker run -it custom-ubuntu:v2 [email protected]:/tmp# pwd /tmp [email protected]:/tmp# cat /tmp/data.txt random text
We can see that the results of both the change directory commands are as expected. Additionally, we must remember that WORKDIR remains the recommended way. Still, for simple use cases, we can use the cd command in conjunction with the RUN, ENTRYPOINT, or the CMD instructions.
In this article, we learned different approaches to changing the directory when working with Docker images or starting the container.