How to Mount a Host Directory in a Docker Container?

Better Stack Team
Updated on January 31, 2023

If you want to mound a host directory in a Docker container, you have to main ways to do that:

Using the ADD command:

The simplest way is to use the dockers ADD command as shown below:

 
ADD . /path/inside/docker/container

This command will copy the files from the current working directory to the path in the docker container specified in the command.

However, any changes made to this directory on the host after building the dockerfile will not show up in the container. This is because when building a container, docker compresses the directory into a .tar and uploads that context into the container permanently.

Mount a volume

The second way to do this is to mount a volume. Due to docker trying to be as portable as possible, you cannot map a host directory to a docker container directory within a dockerfile, because the host directory can change depending on which machine you are running on. To map a host directory to a docker container directory you need to use the -v flag when using docker run

 
docker run \
    -v /tmp:/container/directory \
    <container> \
    ls /container/directory

If you are new to Docker, feel free to start with our Getting started logging guide.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →