Mount the host directory into the Docker container
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories into Docker containers.
Mounting directories into Docker containers is helpful in development and production environments. In addition to creating services that rely on host system directories, this ensures that the entire file system is not destroyed if the Docker container is destroyed.
Let's say we want to build a new version of the container. In this case, mounting the directory directly into the Docker container will enable hot reloading.
This article requires that Docker is correctly set up and that you have a Docker image and container. This will work whether we are running Docker on wsl or Linux.
Mounting a host directory into a Docker container using bind mount
Bind mounts were one of the earliest solutions that allowed us to persist data by mounting a Docker container to a directory on the host system. These would allow us to reference directories by referencing the absolute file path of the target directory.
In this case, Docker is not in control of this directory, unlike the case of Docker volumes, which create a directory in the Docker storage system that Docker manages.
In addition, unlike Docker volumes, we cannot directly manage directories mounted to containers through the Docker CLI or Docker API. However, Docker mounts have high performance in hosts such as MAC or Windows, where the performance of Docker volumes is slightly inferior.
We should also note that mounting a container to a directory using a bind mount will definitely increase the size of the container.
We can mount a directory into a container using two flags when starting the container. These include the -v
and --mount
flags.
Mount a host directory into a Docker container
using -v
the or flags--volume
It consists of three fields which should always be in the correct order and separated by colons. These include:
- The path to the directory on the host that we want to mount.
- The path to the directory in the container where we should mount this directory.
-
Other optional options include
ro
specifying read-only mode.
docker run -t -i -v <host_dir>:<container_dir
Once we have determined the host and container directories we want to mount together, we can implement the above command. However, we cannot modify sensitive files.
This is because mounts give us access to sensitive files which, if tampered with, can cause fatal failures in our system.
In this case, we will use the official ruby image to create the container and mount the directory. We name the script with the directory name, the same as the Docker container.
isaac@DESKTOP-HV44HT6:~/isaac$ docker run -it --rm -v$HOME/Desktop/scripts:/scripts --name scripts ruby bash
Unable to find image 'ruby:latest' locally
latest: Pulling from library/ruby
e4d61adff207: Pull complete
4ff1945c672b: Pull complete
ff5b10aec998: Pull complete
12de8c754e45: Pull complete
ada1762e7602: Pull complete
f8f0dec0b2ef: Pull complete
7109f2ab3080: Pull complete
fe1e1dda18a5: Pull complete
Digest: sha256:a1ebc64daa170324dde5b668829de85487575eaa2bdea5216b4c983b1822f9df
Status: Downloaded newer image for ruby:latest
If we don't have the image locally, Docker will automatically download it. Once the container building process is complete, Docker will open the container for us.
root@9d057cf9e33d:/#
root@9d057cf9e33d:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin scripts srv sys tmp usr var
root@9d057cf9e33d:/# cd scripts
root@9d057cf9e33d:/scripts# ls
We can see that the scripts directory in the container is empty.
Now, let's go to the scripts directory in the host and create a new file in that directory. It will automatically be reflected in the Docker container directory scripts.
isaac@DESKTOP-HV44HT6:~/Desktop$ cd scripts
isaac@DESKTOP-HV44HT6:~/Desktop/scripts$ touch new_file.txt
touch: cannot touch 'new_file.txt': Permission denied
isaac@DESKTOP-HV44HT6:~/Desktop/scripts$ sudo touch new_file.txt
[sudo] password for isaac:
isaac@DESKTOP-HV44HT6:~/Desktop/scripts$ ls
new_file.txt
isaac@DESKTOP-HV44HT6:~/Desktop/scripts$
Once we create a file in the local directory, it will appear in the container directory below.
root@913609933be2:/scripts# ls
new_file.txt
--mount
Mount a host directory into a Docker container
using the
This is a -v
more direct approach than the tags and consists of multiple key-value pairs separated by commas. For these tags, the order of the fields does not matter.
However, -v
it is more verbose compared to the tag.
These methods consist of the following commands:
-
The type of load, for example
Bind
, ,volume
ortmpfs
. -
The source, that is, the path to the directory on the host that we want to mount, is usually
src
represented by . - The target is the path to the directory on the container where we want to mount the directory.
- We also have other options such as a read-only option and bind propagation to designate this directory as private or shared.
Once we have noted down all the needed fields, especially the source and target, we can run the command as shown below.
In this case, we use the official Nginx image to build the container and new_scripts
map the local directory name to etc/nginx
a directory located at .
$ docker run -d \
> -it \
> --mount type=bind,source=$HOME/Desktop/scripts/new_scripts,target=/etc/nginx \
> nginx \
> bash
e079e3254970e290ae68473239e101c6aa8ba4ba56482c75cd21f9bb9f49600b
Now that we have successfully mapped both directories, any changes made to the directory on the host will automatically be reflected on the directory in the container.
isaac@DESKTOP-HV44HT6:~/Desktop/scripts/new_scripts$ sudo touch new_file.txt
[sudo] password for isaac:
isaac@DESKTOP-HV44HT6:~/Desktop/scripts/new_scripts$ ls
new_file.txt
This file will be reflected in the docker directory mapped to this directory, indicating that we have successfully mapped these two directories.
root@e079e3254970:/etc# cd nginx
root@e079e3254970:/etc/nginx# ls
new_file.txt
in conclusion
We have successfully demonstrated how to mount directories using Docker -v
and tags.-mount
However, we should also note that when -v
mounting a non-existent directory using a tag, Docker will automatically create it; this --mount
is different from the case when using Docker tags.
When developing new applications, we should consider using Docker volumes.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:103 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:131 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
Publish Date:2025/03/26 Views:97 Category:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
Publish Date:2025/03/26 Views:125 Category:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
Publish Date:2025/03/26 Views:202 Category:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
Publish Date:2025/03/26 Views:88 Category:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
Publish Date:2025/03/26 Views:140 Category:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application