What are the mounting methods for docker?
Method: 1. Use the run command, the syntax is "docker run --name test1 -it -v"; 2. Use the VOLUME instruction of dockerfile to create a mount point, the syntax is "VOLUME ["/data1" ,"/data2"]"; 3. Use container shared volumes.
The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.
What are the mounting methods of docker?
Before introducing the VOLUME instruction, let’s take a look at the following scenario requirements:
1. The container is created based on the image. Finally The container file system includes a read-only layer and a writable layer of the image. The data persistence of process operations in the container is saved on the writable layer of the container. Once the container is deleted, the data is gone unless we manually back it up (or create a new image based on the container). Can the data persisted by the container process be saved on the host? In this way, even if the container is deleted, the data is still there.
2. When we develop a web application, the development environment is local to the host, but the running test environment is placed on the docker container.
In this case, after I modify the files (such as html, js, etc.) on the host, I need to synchronize them to the container. This is obviously more troublesome.
3. Multiple containers run a set of associated services. What if they want to share some data?
We can certainly think of various solutions to these problems. Docker itself provides a mechanism that can associate a directory on the host with a directory in the container (called a mount point, or volume). The content under the mount point on the container is the host. The contents of the directory are similar to the mount mechanism under Linux systems. In this case, when we modify the contents of the directory on the host, we do not need to synchronize the container, and it will take effect immediately for the container. Mount points can be shared by multiple containers.
Let’s introduce the specific implementation mechanism.
First pass the docker run command
1. Run the command: docker run --name test -it -v /home/xqh/myimage:/data ubuntu /bin/bash
The -v flag sets a mount point /data in the container (which is a directory in the container), and associates the contents of the /home/xqh/myimage directory on the host to /data.
In this way, operations on the /data directory in the container and operations on /home/xqh/myimage on the host are completely synchronized in real time, because these two directories actually point to the host directory. .
2. Run the command: docker run --name test1 -it -v /data ubuntu /bin/bash
The -v mark above only sets the mount point of the container, and does not Specify the associated host directory. At this time, docker will automatically bind a directory on the host. You can view it through the docker inspect command.
xqh@ubuntu:~/myimage$ docker inspect test1 [ { "Id": "1fd6c2c4bc545163d8c5c5b02d60052ea41900a781a82c20a8f02059cb82c30c", ............................. "Mounts": [ { "Name": "0ab0aaf0d6ef391cb68b72bd8c43216a8f8ae9205f0ae941ef16ebe32dc9fc01", "Source": "/var/lib/docker/volumes/0ab0aaf0d6ef391cb68b72bd8c43216a8f8ae9205f0ae941ef16ebe32dc9fc01/_data", "Destination": "/data", "Driver": "local", "Mode": "", "RW": true } ],
Each piece of information under Mounts above records the information of a mount point on the container. The "Destination" value is the mount point of the container, and the "Source" value is the corresponding host directory. It can be seen that the host directory corresponding to this method is automatically created. Its purpose is not to modify it on the host, but to share it with multiple containers.
2 Create a mount point through dockerfile
The mount point created by the -v flag of the docker run command introduced above can only be valid for the created container. Mount points can be created in the image through the VOLUME directive of the dockerfile, so that all containers created through the image will have a mount point. Another difference is that the mount point created through the VOLUME command cannot specify the corresponding directory on the host and is automatically generated.
#test FROM ubuntu MAINTAINER hello1 VOLUME ["/data1","/data2"]
The above dockfile file specifies two mount points /data1 and /data2 through the VOLUME instruction.
We use docker inspect to view the container generated by the image created by this dockerfile , you can see the following information
"Mounts": [ { "Name": "d411f6b8f17f4418629d4e5a1ab69679dee369b39e13bb68bed77aa4a0d12d21", "Source": "/var/lib/docker/volumes/d411f6b8f17f4418629d4e5a1ab69679dee369b39e13bb68bed77aa4a0d12d21/_data", "Destination": "/data1", "Driver": "local", "Mode": "", "RW": true }, { "Name": "6d3badcf47c4ac5955deda6f6ae56f4aaf1037a871275f46220c14ebd762fc36", "Source": "/var/lib/docker/volumes/6d3badcf47c4ac5955deda6f6ae56f4aaf1037a871275f46220c14ebd762fc36/_data", "Destination": "/data2", "Driver": "local", "Mode": "", "RW": true } ],
You can see the information of the two mount points.
Three-container shared volume (mount point)
Next we create another container that can share the /data1 and /data2 volumes with test1 (already created container). This is done in docker run Use the --volumes-from tag, such as:
can be from different mirrors, such as:
docker run --name test2 -it --volumes-from test1 ubuntu /bin/bash
, or it can be the same mirror, such as:
docker run --name test3 -it --volumes-from test1 myimage /bin/bash
The three above Each container test1, test2, and test3 has two directories: /data1 and /data2, and the contents in the directory are shared. If any container modifies the content, other containers can obtain it.
Four Best Practices: Data Container
If multiple containers need to share data (such as persistent databases, configuration files or data files, etc.), you can consider creating a specific data container. Containers have 1 or more volumes.
Other containers share the volumes of this data container through –volumes-from.
Because the volume of the container essentially corresponds to the directory on the host, this data container does not need to be started.
For example: docker run --name dbdata myimage echo “data container”
Note: There is a volume, data sharing between containers is more convenient, but there are also many problems that need to be solved, such as permissions Control, data backup, volume deletion, etc. These contents will be introduced in subsequent articles.
Recommended learning: "docker video tutorial"
The above is the detailed content of What are the mounting methods for docker?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

To get the Docker version, you can perform the following steps: Run the Docker command "docker --version" to view the client and server versions. For Mac or Windows, you can also view version information through the Version tab of the Docker Desktop GUI or the About Docker Desktop menu.

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)
