Docker Quick Start series (4): Concepts and operations of data volumes and data volume containers, docker Quick Start
Introduction
Sometimes, some logs are generated when our services are running, or we need to back up the data in the container, or even share data between multiple containers, this must involve data management operations on containers.
There are two ways to manage data in containers:
- Data Volume
- Data Volume container
A data volume is a special directory for containers. It bypasses the file system and provides many useful features:
-Data volumes can be shared and reused between containers.
-Modifications to the data volume take effect immediately
-Updating the data volume does not affect the image.
-The volume always exists until no container is used.
# (Similar to mount in linux ))
Create a data volume
When using the Docker run command, you can use the-v parameter marker to create a data volume in the container. Multiple Data volumes can be created using the-v mark multiple times.
Docker run-dp -- name web-v/webapp ubuntu: 14.04 # Here we have no-p and no port is set, if we do not specify the port ing relationship between the container and the host, Docker will map the port at will.
Mount a host directory as a data volume
You can also use the-v tag to mount a local existing Directory to the container as a data volume.
docker run -dp --name web -v /src/webapp:/opt/webapp ubuntu:1404
The above command loads the host/src/webapp directory to the/opt/webapp directory of the container:
This function is very convenient during testing. For example, you can place some programs or data to a local directory, and then run and use it in the container. In addition, the path of the local directory must be an absolute path. If the directory does not exist, Docker will automatically create it.
The default permission for Docker to mount data volumes is read/write. You can also specify the read-only permission through ro:
Docker run-dp -- name web-v/src/webapp:/opt/webapp: ro ubuntu: 14.04 # after adding: ro, the data of the Data volumes attached to the container cannot be modified.
Mount local files as data volumes
The-v tag can also mount a single file from the host to the container as a data volume:
Docker run -- rm-it-v ~ /. Bash_history:/. bash_history ubuntu: 14.04 # in this way, you can record the command history entered in the container (different shell versions)
Data Volume container
If you need to share some continuously updated data between containers, the simplest way is to use a data volume container, which is actually a common container, it is used to provide data volumes for other containers to mount.
First, create a data volume container dbdata, and create a data volume in it and mount it to/dbdata:
docker run -ti -v /dbdata --name dbdata ubuntu:14.04
Then we can use-volumes-form in other containers to mount the data volumes in the dbdata container. For example, create the db1 and db2 containers and mount the data volumes from the dbdata container:
docker run -ti --volumes-from dbdata --name db1 ubuntu:14.04docker run -ti --volumes-from dbdata --name db2 ubuntu:14.04
At this time, both the container db1 and db2 mount the same data volume to the same/dbdata directory. The writing of either of the three containers in this directory can be seen by other containers.
For example, create a test file in the dbdata container:
root@df392e32f0p6:/# touch test1root@df392e32f0p6:/#lstest
We can view it in the db1 container:
docker run -ti --volumes-from dbdata --name db1 ubuntu:14.04root@92597e32f0p6:/# ls dbdata/test
We can use the-volumes-from parameter multiple times to mount multiple volumes from multiple containers. You can also mount data volumes from other containers that have already attached container volumes:
Docker run-d -- name db3 -- volumes-from db1 ubuntu: 14.04 # Note: the container that uses the -- volumes-from parameter to mount the data volume does not need to be running.
If the mounted containers (including dbdata, db1, and db2) are deleted, the data volumes are not automatically deleted. To delete a data volume, you must explicitly use the Docker rm-v command to delete the associated container when deleting the last container that is attached to it.
Using a data volume container allows you to freely upgrade and move data volumes between containers.
Migrate data using a data volume container
You can use the data volume container to back up and reply to the data volume to migrate the data.
Backup
docker run --volumes-from dbdata -v $(pwd):/backup --name worker ubuntu:14.04 tar cvf /backup/backup.tar /dbdata
This command is a bit complicated. Let's take a look at the write operations of this command.
1. Create a worker container using the ubuntu image.
2. Use the-volumes-from dbdata parameter to mount the data volume (dbdata volume) of the dbdata container to the worker container );
3. Use the-v $ (pwd):/backup parameter to mount the local current directory to the/backup Directory of the worker container.
After the worker container is started, the tar cvf/backup/backup.tar/dbdata command is used to back up the content in/dbdata to the/backup/backup.tarin the container, that is, the backup.tar in the front directory of the client.
Restore
To restore data to a container, follow these steps to first create a container dbdata2 with data volumes:
docker run -v /dbdata --name dbdata2 ubuntu:14.04
Create another container, mount the dbdata2 container, and decompress the backup file to the mounted container volume using ubtar:
docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar