Docker tutorial on data management

  • 2020-06-03 08:52:45
  • OfStack

Docker data management

In the process of using Docker, users often need to view the data generated by the application in the container, or backup the data in the container, or even share the data among multiple containers, which inevitably involves the container's data management operation. There are two main ways to manage data in a container: data volumes (Data Volumes) and data volume containers (Data Volume Containers).

Data volume

A data volume is a special directory for containers to use that bypasses the file system and provides many useful features:

1. Data volumes can be Shared and reused between containers.
2. Changes to the data volume take effect immediately.
3. Updates to data volumes do not affect mirroring.
4. The data volume will exist until there is no container to use.

The use of data volumes is similar to mount for directories or files under linux.

Create 1 data volume in the container

When using the docker run command, it is possible to create 1 data volume in the container using the -ES32en tag. Multiple data volumes can be created using the -ES33en tag multiple times.

In the following example we use the myimg/webapp image to create an web container and create a data volume to mount to the container's /webdata directory.

$ sudo docker run -d -P  � name web -v /webdata myimg/webapp python

Mount 1 host directory as data volume

Using the -ES46en tag, you can also specify to mount an existing directory locally to the container as a data volume:

$ sudo docker run -d -P  � name web -v /var/data:/opt/webdata myimg/webapp python

The above command mounts the host's /var/data directory to the container's /opt/webdata directory.

This feature is especially handy when testing, for example, users can place a program or data into a local directory and use it in a container. In addition, the path to the local directory must be absolute, and if the directory does not exist, Docker creates it automatically.

The default permissions for the Docker mount data volume are read-write (rw), or the user can specify it as read-only via the ro tag:

$ sudo docker run -d -P  � name web -v /var/data:/opt/webdata:ro myimg/webapp python

After adding :ro, the data in the volume mounted in the container becomes read-only.

Mount 1 localhost file as a data volume

The -ES72en tag can also mount a file from a host into a container as a data volume, but doing so creates a problem. It is recommended that you also mount the files in the same directory.

Data volume container

If a user needs to share continuously updated data between containers, the easiest way is to use a data volume container. A data volume container is simply a common container that is used to provide data volumes for mounting by other containers. The following is a brief introduction to its use.

The first step is to create a data volume container, mydata, in which to mount a data volume to the /data directory.

$ sudo docker run -it -v /data  � name mydata ubuntu

It is then used in other containers -- ES85en-ES86en to mount the data volumes in the mydata container. For example, create two containers mycon1 and mycon2 and mount the data volume from the mydata container:

$ sudo docker run -it --volumes-from mydata  � name mycon1 ubuntu
$ sudo docker run -it --volumes-from mydata  � name mycon2 ubuntu

(Note that there is no data volume information specified in the command, meaning that the directory where the data volume is mounted in the new container is the same as in the source container.)

At this point, both containers mycon1 and mycon2 mount the same data volume to the same directory /data. Any one of the three containers writes to this directory can be seen by other containers.

The --volumes-from parameter can be used multiple times to mount multiple data volumes from multiple containers. You can also mount a data volume from another container that already has a container mounted. And the container that mounts the data volume using the -- ES104en-from parameter does not itself need to be kept running.

When you delete a container that has a data volume mounted, however, the volume is not automatically deleted. If you want to delete a data volume, you must explicitly use the docker ES110en-ES111en command when you delete the last container that still has it mounted.

Using a data volume container gives users the freedom to upgrade and move data volumes between containers, as described in more detail below.

Migrate data using a data volume container

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

The backup

Use the following command to back up the data volumes in the mydata data volume container:

$ sudo docker run --volumes-from mydata -v $(pwd):/backup  � name worker ubuntu tar cvf /backup/backup.tar /data

This command first creates a container worker using the Ubuntu image. The -- ES131en-ES132en mydata parameter is also used to have the worker container mount the data volume of the mydata container. Next, use the -v $(pwd):/backup parameter to mount the current directory locally to the /backup directory of the worker container.

After the worker container is started, the tar cvf /backup/ backup.tar /data command is used to back up the contents under /data to /backup/ backup.tar in the container, which is backup.tar under the current directory of the host host.


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

$ sudo docker run -v /data  � name mydata2 ubuntu /bin/bash

Then create another new container, mount the mydata2 data volume, and use tar to unzip the backup file to the mounted container volume:

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

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

Related articles: