The last section mentions that when the container is running, the data generated will not be in the write image, and re-starting the new container with this image will initialize the image and add a new read-write layer to hold the data. If you want to do data persistence, Docker provides data volume or data container volumes to solve the problem, and commits a new image to hold the resulting data. So, let's look at each of the different ways to use it.
First, the data volume
Data volume Features:
You can bypass the UFs file system and provide access to one or more containers.
is completely independent of the container's life cycle, so it does not delete the data volume it hangs on when the container is deleted.
Data volume Features:
The data volume is copied to the newly initialized data volume when the container initiates initialization, if the image used by the container contains data at the mount point.
Data volumes can be shared and reused directly in containers.
You can modify the contents of the data volume directly.
Changes to the data volume do not affect the update of the mirror.
The volume will persist even if the container on which the data volume is mounted has been deleted.
1. Data Volume usage
To create and mount a data volume:
$ sudo docker run-itd--name ubuntu_test1-v/container_data:/data Ubuntu
Note: Container_data is a host directory,/data is a directory in the container, directory does not exist automatically created
$ sudo docker inspect Ubuntu_test1
"Mounts": [
{
"Source": "/container_data",
"Destination": "/data",
"Mode": "",
"RW": True
}
],
You can see that it has been mounted successfully and that the container has read and write permissions to the directory.
Test:
$ CD Container_data
$ sudo touch test.txt
$ sudo docker exec ubuntu_test1 ls/data
Test.txt
Files created in the host directory are also seen inside the container.
2. Delete the container, will the data be deleted together?
$ sudo docker stop Ubuntu_test1
$ sudo docker rm ubuntu_test1
$ ls Container_data
Test.txt
The files in the data volume directory on the host are not changed, indicating that deleting the container does not affect the data volume.
3. Reboot a container and mount the data volume again try
$ sudo docker run-itd--name ubuntu_test2-v/container_data:/data Ubuntu
$ sudo docker exec ubuntu_test1 ls/data
Test.txt
The file still exists, indicating that the data was copied to the container during initialization.
3. Start a container and mount the data volume here
$ sudo docker run-itd--name ubuntu_test3-v/container_data:/data Ubuntu
$ sudo docker exec ubuntu_test1 ls/data
Test.txt
You can also see the data that the data volume can share with multiple containers.
Ii. container data volumes
Use a normal container as a data volume, and let other containers implement data sharing by mounting the container.
Note: Data volume containers can degrade I/O performance.
1. Container Data Volume usage
To create a Dvdata data volume container:
$ sudo docker run-itd-v/data--name dvdata Ubuntu
Note:/data is a shared directory within the data volume container
Mount the data volume of the Dvdata container in another container:
$ sudo docker run-itd--name web1--volumes-from dbdata Ubuntu
$ sudo docker run-itd--name web2--volumes-from dbdata ubuntu
Respectively into the WEB1, WEB2 container, there will be a/data directory, in the Web1/data directory to create files, WEB2 can also see.
Summary: If you delete Dvdata, Web1, WEB2, the data volume is not automatically deleted. Use the Docker rm-v command to specify the container to delete if you want to delete the last container to which it is attached.
Blog Address: http://lizhenliang.blog.51cto.com
Third, commit command use
The commit command is the ability to submit the image and modifications in an existing container as a new image, which also preserves the read-write layer content.
1. Start a new container and create a test.txt file in the container/opt directory
$ sudo docker run-itd--name web Ubuntu
$ sudo docker exec web Touch/opt/test.txt
$ sudo docker exec web ls/opt
Test.txt
2. Submit a new image
$ sudo docker commit web Web:v2
$ sudo docker images
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/79/13/wKiom1aHmizRJTH3AAALZBBhRf0914.png "title=" QQ picture 20160102173631.png "alt=" Wkiom1ahmizrjth3aaalzbbhrf0914.png "/>
3. Successfully submit a new image, use this new image to start a container to see if the created file exists
$ sudo docker run-itd--name web_v2 web:v2
$ sudo docker exec web_v2 ls/opt
Test.txt
Summary: The Commit command also enables the storage of read-write data, but is not suitable for data persistence
Iv. Data Volume container backup and restore
Backup:
$ sudo docker run--volumes-from dvdata-v/container_backup:/backup ubuntu tar cvf/backup/backup.tar/data
Note: Create a temporary container, mount the Dvdata container data volume, mount the data volume/container_backup directory to the container/bakcup, perform a backup/data directory in the container to/backup, that is, back up to the host/container_ Backup directory.
Recovery:
#先创建一个数据卷容器
$ sudo docker run-v/data--name dvdata2 Ubuntu
Note: This data volume directory name should be the same as the backup
#再将备份文件恢复到这个数据卷容器
$ sudo docker run--volumes-from dvdata2-v/container_backup:/backup ubuntu tar Xvf/backup/backup.tar
#最后启动一个容器挂载验证/data Directory Data Recovery succeeded
$ sudo docker run-itd--volumes-from dvdata2--name web_recover Ubuntu
V. Migrating containers and Mirrors
Export and Import commands use:
#export导出容器会丢失历史记录和元数据, similar to the snapshot
Create a Test Container first:
$ sudo docker exec web Touch/opt/test.txt
$ sudo docker exec web ls/opt
Test.txt
To perform an export:
$ sudo docker export web > Web.tar
To perform an import:
$ Cat Web.tar | sudo docker import-web:v2
$ sudo docker images
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/79/13/wKiom1aHmtuhyu9UAAAHiNrmLo0553.png "title=" QQ picture 20160102173920.png "alt=" Wkiom1ahmtuhyu9uaaahinrmlo0553.png "/>$ sudo docker run-itd--name web_v2 web:v2/ Bin/bash
#启动这个镜像要加/bin/bash, otherwise error error response from Daemon:no command specified
$ sudo docker exec web_v2 ls/opt
Test.txt
Summary: The Export command also allows you to save the data in the container and migrate to another Docker host
The Save and Load commands use:
#一般用于迁移镜像到别处
Export:
$ sudo docker save web > Web.tar
Import:
$ sudo docker load < Ubuntu.tar
Note: History and metadata are not discarded, and versions can be rolled back. Start without adding/bin/bash
This article is from the "Li Zhenliang Technology Blog" blog, make sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1730892
Docker data persistence and container migration