Due to the explosion of Docker technology, companies of all sizes are now using Docker.Friends who are familiar with Docker know that DOcker containers run for a lifetime and that if a docker container fails, it means that the data in the container will also be lost.So data is important for the enterprise!How to persist data in docker containers is an issue for maintenance staff!This post will learn how to persist data in containers!
Before you can understand Docker data persistence, you need a simple understanding of the storage type of Docker, as you can see by executing the following commands:
[root@docker ~]# docker info //View Docker details Containers: 2 //There are several containers in total Running: 2 //There are several containers running Paused: 0 //There are several containers suspended and suspended Stopped: 0 //There are several containers stopped Images: 2 //There are several mirrors Server Version: 18.09.0 //Version information for docker Storage Driver: overlay2 //Storage driver type is overlay2 Backing Filesystem: xfs //Supported file system: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local //Local Storage Network: bridge host macvlan null overlay //Supported Network Types Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: c4446665cb9c30056f4998ed953e6d4ff22c7c39 runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-862.el7.x86_64 //Kernel Information Operating System: CentOS Linux 7 (Core) //operating system OSType: linux //Operating System Type Architecture: x86_64 CPUs: 2 //Number of CPU s Total Memory: 1.779GiB //Memory capacity Name: docker ID: 3SU5:P433:UIFM:YK6O:FBGP:MJSN:MTSO:PKPA:3NMN:6VW4:XUOV:XL5H Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: //Accelerator information used http://f1361db2.m.daocloud.io/ Live Restore Enabled: false Product License: Community Engine
Data Volume (Data Volume Management Operation)
There are two types of database management operations, bind mount and docker managervolume.
1) Bind mount (bind mount)
Persistent storage: Essentially a directory or file in the DockerHost file system that can be mounted directly into the container's file system.When running containers, you can do this with -v.
Bind mount features:
- Data Volume is a directory or file and cannot be a disk (block device) without formatting;
- Containers can read and write data in volume s;
- Change with source file;
- The volume data can be permanently saved even if its container has been destroyed;
Example:
[root@docker ~]# mkdir /html [root@docker ~]# Echo "hello world" > /html/index.html//Create test page [root@docker ~]# docker run -itd --name test -p 80:80 -v /html:/usr/share/nginx/html nginx:latest //Run the container and use the'-v'option to specify the mount directory, preceded by the docker host directory, followed by the':' directory in the container [root@docker ~]# [root@docker ~]# curl 127.0.0.1 //You can see that the mount is in effect hello world [root@docker ~]# echo "lzj" > /html/index.html [root@docker ~]# curl 127.0.0.1 //This way you can see that when the source file changes, the target file changes as well. lzj [root@docker ~]# docker inspect test //View container details "Mounts": [ //Find Mount Field { "Type": "bind", //Its type is bind "Source": "/html", //Source Directory "Destination": "/usr/share/nginx/html", //Target Directory "Mode": "", "RW": true, "Propagation": "rprivate" } ],
Be careful:
- The source file or directory on the DockerHost that needs to be hung must already exist, otherwise, a directory that should be done is hung in the container;
- Files mounted into containers by default, which have read and write permissions.You can restrict the write permissions of the container by adding':ro'after the run container is -v;
- You can mount individual files into containers using scenarios: if you don't want to overwrite the entire directory but only want to add a file, you can use mounting individual files;
2)Docker Manager Volume
Example:
[root@docker ~]# docker run -itd --name test1 -v /usr/share/nginx/html nginx:latest //With the'-v'option in this way, simply add the directory in the container [root@docker ~]# docker inspect test1 "Mounts": [ { "Type": "volume", //Its type is volume "Name": "47545a64ef51aa1ea1065848b6cb5cc5f19e2da8b2c91966b256a910fca58c4d", "Source": "/var/lib/docker/volumes/47545a64ef51aa1ea1065848b6cb5cc5f19e2da8b2c91966b256a910fca58c4d/_data", //Source directory (automatically generated by docker) "Destination": "/usr/share/nginx/html", //Target Directory "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], [root@docker ~]# ls /var/lib/docker/volumes/47545a64ef51aa1ea1065848b6cb5cc5f19e2da8b2c91966b256a910fca58c4d/_data 50x.html index.html //You can see that the directory on the host machine is the directory mounted in the container
This approach features:
- It changes with the source file, just like Bind mount!
- Deleting a container does not delete the original file on the dockerhost host host by default. If you want to delete the container to delete the original file, you can add the option'-v'when deleting the container. (This is not generally recommended because the file may be used by other containers.)
3) Volume containe ainers (data sharing between containers)
Volume container: A container that provides volume storage volumes to other containers.It can also provide a bind mount or a docker manager volume.
[root@docker ~]# docker create --name vc_data -v /html:/usr/share/nginx/html busybox:latest //Create a container (no need to run) [root@docker ~]# docker run -itd --name test3 -P --volumes-from vc_data nginx:latest //Use "--volumes-from" to mount data from the vc_data container to the new container test3 [root@docker ~]# docker ps //View its mapped ports CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 021653708bb2 nginx:latest "nginx -g 'daemon of..." 7 seconds ago Up 5 seconds 0.0.0.0:32768->80/tcp test3 [root@docker ~]# curl 127.0.0.1:32768 //Test results lzj
Note: After the source directory is deleted in the above way, the data in the container will also be lost!
You can write a dockerfile file, write a directory or file to a mirror, and then generate a container from the mirror to ensure that the original data is lost and the data in the container will not change!
Because of the randomness and flexibility of this approach, this is not much discussed here so that data sharing between containers can be achieved through data volume containers.
With the above mechanism, users do not have to worry about data loss even if the container fails during operation.If something unexpected happens, simply re-create the container quickly!
Note: The most important thing in the production environment is the reliability of storage and the dynamic scalability of storage. It is important to take this into account when making data volumes. The GFS file system is also an excellent one. I just made a simple configuration above. If you are in the production environment, you must take good account, such as the mirror volume container above, you can host it.When you mount the GFS file system locally on your machine and then create a mirror volume container, map the directory where you mount the GFS to the mirror volume in the container. This is a qualified container for mirrored volumes.
To this end of the article, thank you for reading ----------