There are two main ways to manage data in a container:
Data Volumes
Data Volumes Dontainers
data volume
Use -v to mount a local directory into the container as a data volume.
[root@wls12c /]$ ls /b2b backup prog_update wasStatus.log [root@wls12c /]$ docker run -d --name=tomcat -v /b2b:/test -it centos /bin/bash f85c616bde3317c857b657355561a1cfa4203fab2b8619386435e541089bde23 [root@wls12c /]$ docker exec -it tomcat /bin/bash [root@f85c616bde33 /]# ls /test backup prog_update wasStatus.log [root@f85c616bde33 /]# touch test/demo [root@f85c616bde33 /]# ls /test backup demo prog_update wasStatus.log [root@f85c616bde33 /]# exit exit [root@wls12c /]$ ls /b2b backup demo prog_update wasStatus.log
You can see that the /b2b directory has been mounted in the container and has data in it.
data volume container
If the containers need to share some continuously updated data, the easiest way is to use the user data volume container. The data volume container is a common container that provides data volumes for other containers to mount and use.
Create data volume container dbdata
[root@wls12c /]$ docker run -it -v /dbdata:/dbdata --name dbdata centos [root@07e4ad5587e1 /]
Create two containers, db1 and db2, and use --volumes-from to mount the data volumes in the dbdata container
[root@wls12c /]$ docker run -it --volumes-from dbdata --name db1 centos
[root@wls12c /]$ docker run -it --volumes-from dbdata --name db2 centos
In this way, any one of the three containers can be written in this directory, and other containers can see it.
backup
Back up the data in the dbdata data volume container to the current directory of the host.
[root@wls12c /]$ docker run --volumes-from dbdata -v $(pwd):/backup --name worker centos tar zcf /backup/backup.tar.gz /dbdata tar: Removing leading `/' from member names [root@wls12c /]$ ls b2b boot dev lib media net root srv tmp backup.tar.gz cgroup etc lib64 misc opt sbin sys usr bin dbdata home lost+found mnt proc selinux test var [root @ wls12c /] $ tar -tvf backup. tar .gz drwxr-xr-x root/root 0 2016-08-26 10:51 dbdata/ -rw-r--r-- root/root 0 2016-08-26 10:40 dbdata/demo -rw-r--r-- root/root 6 2016-08-26 10:48 dbdata/test.txt -rw-r--r-- root/root 0 2016-08-26 10:51 dbdata/hello
recover
Create a container with a data volume
[root@wls12c /]$ docker run -v /dbdata/dbdata --name db centos /bin/bash
recover