Wednesday, January 19, 2022
Home CONTAINERIZATION How to mount a host directory in a Docker container?

How to mount a host directory in a Docker container?


Docker containers are a prevalent way of deploying applications. One of the main reasons is that it provides you with independent images of the host system. But, what would you do when debugging applications running in the container? Or what if you need to make changes to the configuration file? Of course, you could build a new version of the container and re-deploy it. However, directly mounting a directory could be more convenient to enable hot-reloading of code or configuration.

Bind mounts vs Volumes

There are two ways to mount a directory on the host machine to a Docker container. Bind mounts is a relatively old method and have been around since the early days of Docker. Once you use a bind mount, a file or directory on the host machine is mounted into a container. The other way is to mount volumes from the local host to the containers. Now let’s compare these two methods.

Type of mount binds

Bind mounts are the most straightforward and performant yet have limited functionality than volumes. With bind mount, you can mount a file or folder that already exists on the host. So this method relies on the host machine’s filesystem and your administrator skill set. You also need to reference the absolute path of the file or folder on the host.

However, when you use a volume, a new directory is created within Docker’s storage directory on the host machine. The volume and stored data will be managed by Docker, and the volume directory will be created automatically in Docker’s directory. The file or directory does not need to exist on the Docker host already. It is made on-demand if it does not yet exist.


Now, let’s consider a case. There is a directory source. We save the artifacts into source/trial_dir/ when building the source code. Because they will be available to the container at /app, we want the container to access a new build each time you build the source on your development host. Apply the following command to bind-mount the trial_dir/ directory into your container at /app/. Remember to run the command from within the source directory. 

 docker run -d \
  -it \
  --name test_cont \
  --mount type=bind,source="$(pwd)"/trial_dir,target=/app \

We would employ the following if we mount a volume instead of a bind mount.

 docker run -d \
  -it \
  --name test_cont \
  -v "$(pwd)"/trial_dir:/app \


In general, –mount is more explicit and wordy. The biggest difference is that the -v syntax holds all the options together in one field, while the –mount syntax separates them. Don’t forget! If you use --mount to bind-mount a file or directory that does not yet exist on the Docker host, it generates an error. Docker does not automatically create it for you.


Created by using the content of

Mustafa Gonen
DevOps engineer, helps companies to balance their needs throughout the software development life cycle, from coding and deployment to maintenance and updates by automation. He believes the beauty of diversity. Working in DevOps culture and being a part of this harmony makes him highly motivated and satisfied.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

How to pass environment variables to Docker containers?

Intro In application development, it is best practice to separate services from configurations. The Twelve-factor app methodology recommends...

How to rename files and folders in Amazon s3?

Intro: Is it possible to rename files and folders in Amazon S3? How to rename files or folders...

How can we make an S3 bucket public?

Amazon S3 buckets are by default private to avoid accidental exposure of personal data by everyone. But it is possible to...

The difference between public and private subnets in Amazon VPC

Intro Subnets, public and private subnets, these are all networking issues. Therefore, network basics are required to understand...

Recent Comments