Docker file mounting

1. Mount Docker data to the container

  In Docker, in order to achieve data persistence (the so-called Docker data persistence means that the data does not end with the end of the Container ), the data needs to be mounted from the host to the container. Currently Docker provides three different ways to mount data from the host to the container:

  (1) Volumes: Docker manages a part of the host's file system, which is located in the /var/lib/docker/volumes directory by default; (the most common way )

  

  As you can see from the above figure, all the Container data is currently stored in this directory. Since no volume was specified when creating it, Docker helped us create many anonymous (named with the long ID) volume by default.

  (2) bind mounts: It means that it can be stored in any location of the host system; (the more common way )

  However, bind mount is not portable in different host systems. For example, the directory structure of Windows and Linux is different, and the host directory pointed to by bind mount cannot be the same. This is why the bind mount cannot appear in the Dockerfile, because the Dockerfile is not portable.

  (3) tmpfs: The mount is stored in the memory of the host system, and will not be written to the host's file system; ( generally, it is not used )

  The schematic diagrams of the three methods are as follows:

  

Second, the basic use of Volume

2.1 Manage volumes

# docker volume create edc-nginx-vol // Create a custom container volume 
# docker volume ls // View all container volumes 
# docker volume inspect edc-nginx-vol // View detailed information about the specified container volume

  For example, here we create a custom container volume named "edc-nginx-vol":

  

2.2 Create a container using the specified volume

  With a custom container volume, we can create a container that uses this data volume. Here we take nginx as an example:

# docker run -d -it --name=edc-nginx -p 8800:80 -v edc-nginx-vol:/usr/share/nginx/html nginx

  Among them, -v stands for mounting data volume, here use custom data volume edc-nginx-vol, and mount the data volume to /usr/share/nginx/html (this directory is the default web page directory for yum to install nginx).

  If it is not specified by -v, then Docker will help us create an anonymous data volume for mapping and mounting by default.

  After creating the container, we can enter the container and have a look:

  

   You can see that there are two default pages. At this time, we start a new SSH connection to the host to check the data volume we just created:

  

   As you can see, we can access the two default pages in the container. From this we can see that volume helps us to do a function similar to a soft link. We can perceive the changes in the container in the host, and the changes in the host can be sensed in the container.

  At this time, if we manually stop and remove the current nginx container, we will find that the files in the container volume are still there and have not been deleted.

  

   It can be verified that the things in the data volume can be persisted. If you need to create an nginx container next time, then reuse the files in the current data volume.

  In addition, we can also start multiple nginx container instances and share the same data volume, which has strong reusability and scalability.

2.3 Clean up the volume

  If you no longer use the custom data volume, you can manually clean it up:

# docker stop edc-nginx // Pause container instance 
# docker rm edc-nginx // Remove container instance 
# docker volume rm edc-nginx-vol // Delete custom data volume

Three, the basic use of Bind Mounts

3.1 Create a container using a volume

docker run -d -it --name=edc-nginx -v /app/wwwroot:/usr/share/nginx/html nginx

  This specifies that the /app/wwwroot directory on the host machine (it will be created automatically if it is not) mounted to /usr/share/nginx/html (this directory is the default web directory for yum to install nginx).

  At this time, we enter the container again to take a look:

  

   It can be seen that, unlike volumes, the method of bind mounts hides the contents of the mounted directory (if it is not empty). Here, the contents of the /usr/share/nginx/html directory are hidden, so we can not see.

  However, we can mount the files on the host into the container at any time:

  Step1 . Create a new index.html

  

  Step2 . View in the container

  

3.2 Verify binding

docker inspect edc-nginx

   A large wave of configurations can be seen through the above commands. What we need to pay attention to is:

  

3.3 Clean up

docker stop edc-nginx
docker rm edc-nginx

  Like volumes, when we clean up the container, the files in the mount directory are still there and will not disappear with the end of the container, thus achieving data persistence.

3.4 Application case

  Among the service governance components, the service discovery component is one of the most commonly used components. Consul is a popular service discovery open source project. Consul recommends that we use configuration files to register service information. Therefore, we often put the completed service registration configuration file in a file directory of the host machine and mount it to the specified directory of the Consul container, as shown below:

docker run -d -p 8500:8500 --restart=always \
-v /XiLife/consul/data/server1:/consul/data -v /XiLife/consul/conf/server1:/consul/config \
-e CONSUL_BIND_INTERFACE='eth0' --privileged=true \
--name=consul_server_1 consul:1.4.4 agent -server -bootstrap-expect=3 -ui -node=consul_server_1 -client='0.0.0.0' \
-data-dir /consul/data -config-dir /consul/config -datacenter=xdp_dc;

  As you can see, we have mounted the /XiLife/consul/data/server1 directory on the host machine to the container's /consul/data directory through Bind Mounts, and also mounted the /XiLife/consul/conf/server1 directory to The /consul/config directory of the container, and the two directories /consul/data and /consul/config under the container are the places we specify to store agent data and configuration files. Therefore, the change of the configuration file on the host machine will be reflected in the container in time. For example, if we update the configuration file in the directory on the host machine, then we only need to reload the container instance of Consul:

docker exec consul-server consul reload

  *. The consul-server here is the name of the container, and consul reload is the command to reload (not restart).

Four, summary

  This article explores the Docker data volume and the two main methods of mounting data to the container, Volumes and Bind Mounts, and introduces the basic usage and steps. Through the data volume, we can achieve Docker data persistence, which is widely used in practical applications. .

Reference

(1) Li Zhenliang, " Detailed Explanation of Docker Volume "

(2) CloudMan, "Playing with Docker container technology in 5 minutes a day "

(3) Aaron, " Detailed Explanation of Docker Storage Volumes "

 

Author: Zhou Xulong

Source: https://edisonchou.cnblogs.com

The copyright of this article belongs to the author and the blog garden, welcome to reprint, but this statement must be retained without the author’s consent, and the original link should be given in an obvious place on the article page

Guess you like

Origin blog.csdn.net/qq_42533216/article/details/114067850