This is the 19th day of my participation in the Genwen Challenge
Mount Docker data to the container
In Docker, to persist data, you need to mount data from the host machine to the Container. Currently Docker provides three different ways to mount data from the host machine to the container:
Volumes: run the /var/lib/docker-/ volumes directory to manage the host file system. (Most commonly used)
As you can see from the above image, all the Container data is stored in this directory. Docker creates a lot of anonymous volumes by default because the volume is not specified at creation time.
“Bind” means that it can be stored anywhere on the host system. (More common way)
However, bind mount is not portable between different host systems. For example, the directory structures of Windows and Linux are different, and the host directory to which bind mount points is not the same. This is why bind mount cannot appear in a Dockerfile, because then the Dockerfile is not portable.
(3) TMPFS: it is mounted and stored in the memory of the host system, but not written to the file system of the host; (In a way you wouldn’t normally)
The schematic diagram of the three modes is as follows:
2. Basic use of Volume
2.1 managing volume
# docker volume inspect edc-nginx-vol # docker volume inspect edc-nginx-vol # docker volume inspect edc-nginx-vol // View details about the specified container volumeCopy the code
For example, here we create a custom container volume named “edc-nginx-vol” :
2.2 Creating a container that uses the specified volume
With a custom container volume, we can create a container that uses this data volume. Here we use nginx as an example:
\# docker run -d -it --name=edc-nginx -p 8800:80 -v edc-nginx-vol:/usr/share/nginx/html nginx
Copy the code
– v represents the mount data volume, set the data used here (edc – nginx – vol, and the data volume mount to/usr/share/nginx/HTML (the directory is yum install nginx web directory by default).
If not specified by -v, Docker will create anonymous data volumes for us to map and mount by default.
After creating the container, we can go inside the container and have a look:
You can see that there are two default pages. Now we can start a new SSH connection to the host machine and look at the data volume we just created:
As you can see, we can access the two default pages in the container. Therefore, volume helps us to do something like a soft link. Changes that are in the container can be sensed in the host, and changes that are in the host can be sensed in the container.
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.
This verifies that the contents of the data volume can be persisted. If you need to create an Nginx container next time, 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 is reusable and extensible.
2.3 clean up the volume
If the custom data volume is no longer used, you can manually clean it up:
# docker volume rm edc-nginx-vol // Delete the custom data volumeCopy the code
You can Bind the Mounts for use
3.1 Creating a Container using a Volume
docker run -d -it --name=edc-nginx -v /app/wwwroot:/usr/share/nginx/html nginx
Copy the code
Here it is specified to mount the /app/wwwroot directory on the host (automatically created if not) to /usr/share/nginx/html (this is the default web directory for yum to install nginx).
Now let’s go inside the container again:
You can use virtualized binders to hide the contents of the /usr/share/nginx/ HTML directory. If you use virtualized binders to hide the contents of the directory, you can use virtualized binders to hide the contents of the /usr/share/nginx/ HTML directory.
However, we can mount files from the host into the container at any time:
Step1. Create a new index.html file
Step2. Check in the container
3.2 Verifying the Binding
docker inspect edc-nginx
Copy the code
From the above commands, we can see a large number of configurations. We should pay attention to:
3.3 clean up
docker stop edc-nginx
docker rm edc-nginx
Copy the code
Just like volumes, when you clean up the container, the files in the mount directory are still there and will not disappear with the end of the container for data persistence.
3.4 Application Cases
Among service governance components, the service discovery component is one of the most commonly used. Consul is a popular open source service discovery project, and Consul recommends that we register service information using a configuration file. Therefore, we often place the filled out service registration profile in a file directory on the host and mount it in Consul’s container specified directory as follows:
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;Copy the code
As you can see, we will through the way of Bind Mounts/XiLife/consul on host/data/server1 directory mounted to the container/consul/data directory, Will/XiLife/consul/conf/server1 directory mounted to the container/consul/config directory, but the container two directories under/consul/data and consul/config is our designated agent data and configuration file location. Therefore, a configuration file change on host Consul will be reflected in the container in real time. For example, if we update the configuration file in host Consul’s directory, we just need to reload the Consul container instance:
docker exec consul-server consul reload
Copy the code
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
Four, summary
This article explores the use of Volumes and Bind Mounts for Docker data, and introduces basic usage methods and steps. Docker data persistence can be implemented through data Volumes, which is widely used in practical applications.