Docker use tutorial related series catalog
table of Contents
Introduction to bind mounts
Function to mount the host's directory to the container
Suitable for scenario 1: Temporarily generated directory;
Suitable for scenario 2: Sharing source code between the development environment and the container on the docker host;
Create a container
Use -v
docker run -d -it --name=nginx03 -p 88:80 -v /zenghw/nginx:/usr/share/nginx/html nginx
Separate with a comma after the --mount parameter
docker run -d -it --name=nginx03 -p 88:80 --mount type=bind,src=/zenghw/nginx,dst=/usr/share/nginx/html nginx
Note: When using the --mount command, the docker version must be higher than the version above 17.06
It is found that there are no files in the directory where the host is mounted. There were two files before. This is the difference between bind mounts and volumes. If the mount target is not empty in the container, then the existing contents of the directory (host mounting The loaded directory and the directory on the container) will be hidden.
Test: Create index.html in the /usr/share/nginx/html directory of the container
The mount directory on the host will also have this file
Browser test
First pause the nginx03 container and create the nginx04 container
summary:
Features of bind mounts:
1. Share configuration files from the host to the container. By default, mount the host /etc/resolv.conf to each container to provide DNS resolution.
2. Share the source code between the development environment and the container on the docker host. For example: you can mount the maven target directory to the container, and every time a maven project is built on the docker host, the container can access the built project package.
3. Keep the directory structure of the host machine consistent with the directory structure of the container