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.
Article URL:https://www.jiyik.com/en/xwzj/opersys_10090.html
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:98 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:122 Category:Docker
-
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list
Tagging images with Docker and Docker Compose
Publish Date:2025/03/25 Views:70 Category:Docker
-
When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images. This article discusses tagging ima
Creating a database user with Docker Postgres
Publish Date:2025/03/24 Views:166 Category:Docker
-
When developing applications, we usually use database management systems such as PostgreSQL, MySQL, MongoDB, etc. to record application data. Docker helps us run an instance of these application database management systems. This helps save
Adding unsafe registry keys in Docker
Publish Date:2025/03/24 Views:130 Category:Docker
-
While it is highly recommended to secure your registry using a Transport Layer Security (TLS) certificate issued by a known Certificate Authority (CA), we have the option of using our insecure registry over an unencrypted Hypertext Transfer
List all images in Docker Registry V2
Publish Date:2025/03/24 Views:131 Category:Docker
-
After several iterations, Docker Registry was upgraded from version 1 to version 2. Especially being new, some commands needed to be included or adequately documented on its official documentation website. An example is getting a list of im