Docker data volume data volume container details

  • 2020-05-27 07:44:05
  • OfStack

Docker data volume, data volume container details


Sometimes, when our service runs, it is necessary to generate some logs, or we need to backup the data in the container, or even share the data among multiple containers, which inevitably involves the data management operation of the container.

There are two main ways to manage data in a container:

Data volume
Data volume container

The data volume is a special directory that can be used by the container. It bypasses the file system and provides many useful features:
- data volumes can be Shared and reused between containers
- changes to the data volume will take effect immediately
- updates to data volumes will not affect mirroring
- volume 1 will persist until no container is used

#(similar to mount under linux (mount))

Creating a data volume

When using the Docker run command, use the -v parameter tag to create a single data volume in the container, and use the -v tag multiple times to create multiple data volumes

docker run -dp --name web -v /webapp ubuntu:14.04
We don't have a port after -p. If we don't have a port relationship between the container and the host,Docker will map at will

Mount a host directory as the data volume
The -v tag can also be used to specify that a local existing directory be mounted into the container as a data volume

docker run -dp --name web -v /src/webapp:/opt/webapp ubuntu:1404

The command above loads the host /src/webapp directory into the container's /opt/webapp directory:

This feature is convenient for testing, for example, users can put some programs or data into a local directory, and then run and use it in a container. In addition, the

The path to the directory must be an absolute path, and if the directory does not exist, Docker will automatically create it.

The default permission of Docker mounting data volume is read and write. Users can also specify read only by ro:

docker run -dp --name web -v /src/webapp:/opt/webapp:ro ubuntu:14.04
After adding: ro, the data of the data volume mounted in the container cannot be modified.

Mount native files as data volumes

The -v tag can also mount a single file from the host into the container as a data volume:

Docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu:14.04
This allows you to record the history of commands entered in the container (different versions of shell vary)

Data volume container

If a user needs to share continuously updated data between containers, the easiest way is to use a data volume container, which is a common container that provides data volumes for other containers to mount.
First, create one data volume container, dbdata, and mount one data volume to /dbdata:

docker run -ti -v /dbdata --name dbdata ubuntu:14.04

We can then mount the data volumes in the dbdata container with the use of the volumes-form in other containers, such as creating the db1 and db2 containers and mounting the data volumes from the dbdata container:

docker run -ti --volumes-from dbdata --name db1 ubuntu:14.04
docker run -ti --volumes-from dbdata --name db2 ubuntu:14.04

At this point the containers db1 and db2 both mount the same data volume to the same /dbdata directory. 3 containers any 1 side of the write in this directory, the other containers can see.

For example, create an test file in the dbdata container:

root@df392e32f0p6:/# touch test1

Let's look at it in the db1 container:

docker run -ti --volumes-from dbdata --name db1 ubuntu:14.04
root@92597e32f0p6:/# ls dbdata/

We can mount multiple data volumes from multiple containers multiple times using the WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD/WSD. You can also mount data volumes from other containers that have already mounted container volumes:

docker run -d --name db3 --volumes-from db1 ubuntu:14.04
Note: the container of the data volume mounted using the -- volumes-from parameter does not itself need to be in a running state

If the mounted container is deleted (including dbdata, db1, and db2), the data volume is not automatically deleted. If you want to delete a data volume, you must explicitly use the Docker rm-v command to specify that the associated container is deleted at the same time when you delete the last container that still has it mounted.

Using data volume containers allows users to freely upgrade and move data volumes between containers.

Migrate data using data volume containers

The data volume container can be used to backup and restore the data volume in order to realize the data migration.

The backup

docker run --volumes-from dbdata -v $(pwd):/backup --name worker ubuntu:14.04 tar cvf /backup/backup.tar /dbdata

This command is a little bit more complicated, so let's see what it does

1. First, a container worker was created using the ubuntu image.
2. Mount the data volume of the dbdata container (dbdata data volume) using the volumes-from dbdata parameter for the worker container;
3. Use the -v $(pwd):/backup parameter to mount the local current directory to the worker container's /backup directory

After the worker container is started, the tar cvf /backup/ backup.tar /dbdata command is used to backup the contents under /dbdata to the backup/ backup.tar in the container, backup.tar in the host's current directory.


If you want to restore data to a container, you can do the following to create a container dbdata2 with a data volume:

docker run -v /dbdata --name dbdata2 ubuntu:14.04

Then create another new container, mount the dbdata2 container, and unzip the backup file into the mounted container volume using ubtar:

docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: