1. Mount the local directory into the container
#docker run-itd-v/data/:/data/image name/image_id bash
You can use another container to mount the directory for this container to synchronize and share multiple containers and local directories, such as 2. Mount the directory of other containers
-d option indicates background run
The-v option specifies the directory in which to mount (create a mapping relationship)
/data/:/data/Host Directory: Container directory,: The front of the/data/for the local directory: The following/data/is a directory in the container, the name does not have to be the same as long as you specify the corresponding directory can be host to manually create this directory container will automatically generate this directory
The mirror name/image_id is the image name of the REPOSITORY column displayed for the #docker images or the ID of the image_id column
[[email protected] ~]# docker run -tid -v /data/:/data1/ centos bashaa6ad30230f0eaaf47f35e26a97928bea620bc1d5ad0c8c1a3f97968076e77e6[[email protected] ~]# touch /data/1.txt[[email protected] ~]# echo "ABCDF" > /data/1.txt[[ email protected] ~]# docker exec -it aa6ad30230f bash[[email protected ] /]# ls /data1/1.txt mysql[[email protected] /]# cat 1.txtcat: 1.txt: No such file or directory[[email protected] /]# cat /data1/1.txtabcdf[[email protected] /]# touch /data1/2.txt[[email protected] /]# exitexit[[email protected] ~]# ls /data/1.txt 2.txt mysql[[ email protected] ~]# docker stop aa6ad30230faa6ad30230f[[email protected] ~ ]# ls /data1.txt 2.txt mysql[[email protected] ~]# docker rm aa6ad30230faa6ad30230f[[email protected] ~]# ls /data/1.txt 2.txt Mysql
2. Mount the data volume
in fact, when we mount the directory, we can specify the container name, which is defined randomly if not specified. For example, if we don't specify it, it generates a name of Lonely_archimedes, which can use the command Docker PS to see the rightmost column .
Docker run-itd--volumes-from llonely_archimedes CentOS Bash
In this way, we create a new container using the CentOS image and use the data volume of the Lonely_archimedes container
[[email protected] ~]# docker run -itd -v /data/:/data1 centos bash009f70e70fa76591f81b4a7e1414d12bce4d65688b33fa426ffe83e942f87a57[[email protected] ~]# docker pscontainer id image command created STATUS PORTS NAMES009f70e70fa7 centos "Bash" 7 seconds ago Up 5 Seconds lonely_archimedes[[email protected] ~]# docker run -itd --volumes-from lonely_archimedes centos Bash42dab4b88ba10dffef378b824c77524f348aa0d284f2f868e298fd234d831fb8you have new mail in /var/spool/mail/root[[email protected] ~]# docker psCONTAINER ID image command created status PORTS NAMES42dab4b88ba1 centos "Bash" 22 seconds ago up 20 seconds cranky_ lovelace009f70e70fa7 centos "Bash" 9 minutes ago up 9 minutes lonely_archimedes[[email protected] ~]# docker exec -it 42dab4b88ba1 bash[[email protected] /]# ls /data1/1.txt 2. txt mysql[[email protected] /]# touch /data1/3.txt[[email protected] /]# ls /data1/1.txt 2.txt 3.txt mysql[[email protected] / ]# exitexityou have new mail in /var/spool/mail/root[[email protected] ~]# docker exec -it 009f70e70fa7 bash[[email protected] /]# ls /data1/1.txt 2.txt &Nbsp;3.txt mysql
Custom container Name
Docker run-itd-v/data/:/data1--name datavol CentOS Bash
3. Defining Data Volume Containers
sometimes we need to share data between multiple containers, similar to NFS in Linux, so we can build a dedicated data volume container and then mount the data volume directly from other containers.
First, create a data volume container
Docker run-itd-v/data/--name testvol CentOS Bash//Note here/data/is the/data directory of the container, not the local/data/directory.
then let the other container mount the data volume
Docker run-itd--volumes-from testvol aming bash
4. Backup and recovery of data volumes
(1) Backup
Mkdir/vol_data_backup
Docker run--volumes-from testvol-v/vol_data_backup/:/backup aming/centos tar cvf/backup/data.tar/data/
Note: First we need to use the Testvol data volume to open a new container, and we also need to attach the local/vol_data_backup/directory to the/backup of the container, so that in the container/backup directory inside the new file, we can directly in the/ See in the vol_data_backup/directory. Then, package the files under the/data/directory into a Data.tar file and place them under the/backup directory.
(2) Recovery
idea: Create a new data volume container, then build another container and mount the data volume container before unpacking the TAR package.
New Data Volume container: Docker run-itd-v/data/--name testvol2 aming/centos bash
mount the data volume new container, and unpack: Docker run--volumes-from testvol2-v/vol_data_backup/:/backup Aming/centos tar Xvf/backup/data.tar
This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1753952
Docker Data Management