Monday, October 3, 2022
Home CONTAINERIZATION How to mount a host directory in a Docker container?

How to mount a host directory in a Docker container?

Description

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.

Demo

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 \
  nginx:latest

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 \
  nginx:latest

Conclusion

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.

References

Created by using the content of https://docs.docker.com/storage/bind-mounts/

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 query EC2 tags from within the instance?

Intro To help you manage your instances, images, and other Amazon EC2 resources, you can assign your own...

How to install Python3 on Amazon Linux 2?

Intro Python is an object-oriented programming language that is widely used. In addition to that, it's free and...

How to connect an AWS EC2 instance with FileZilla and do SFTP?

Intro EC2 is one of the essential services of Amazon Web Services (AWS), providing highly available and scalable...

How to install AWS CLI v2 on Linux?

Intro Whether you’ve been using AWS for some time or you are a novice, you may have noticed...

Recent Comments