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.

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/