Docker Data Management of Data volume Data volume container

  • 2020-12-18 01:59:48
  • OfStack

In the process of using Docker in production environment, it is often necessary to persist data or share data among multiple containers, which inevitably involves the container's data management operation.

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

1. Data volume (Data Volumes) : Data in the container is directly mapped to the local host environment; How to create a data volume in a container and mount a local directory or file into a data volume in the container.
2. Data volume containers (Data Volume Containers) : Use specific containers to maintain data volumes. How to use data volume containers to share data between containers and hosts, containers and containers, and to backup and restore data.

Data volume

A data volume is a special directory available to the container that maps the host operating system directory directly into the container, similar to the mount operation in Linux.

Data volumes can provide many useful features, as shown below:
1. Data volumes can be shared and reused between containers, and passing data between containers will become efficient and convenient;
2. Changes to the data in the data volumes will take effect immediately, whether in container operations or local operations;
3. Updating the data volume will not affect the mirroring, which decouples the application and data;
4. The volume will remain 1 until there is no container to use and it can be safely unloaded.

1. Create 1 data volume in the container

When using the docker run command, you can create 1 data volume in the container using the -ES31en flag. Multiple reuses of the -ES32en tag can create multiple data volumes.

Create 1 web container using the training/webapp image and create 1 data volume to mount to the /webapp directory of the container:


$ docker run -d -P --name web -v /webapp training/webapp python app.py

-ES42en is the port that exposes the container service and is a temporary port that is automatically mapped to the local host.

2. Mount 1 host directory as data volume

Using the -ES47en tag, you can also specify to mount an existing directory locally to the container as a data volume (recommended).


$ docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py

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

This feature is convenient for testing purposes. For example, users can place a program or data into a local directory and then run 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 is created automatically.

Docker mount data volumes have read and write permissions by default (rw), or they can be specified as read-only by ro:


$ docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py

After adding :ro, the data in the mounted data volume in the container cannot be modified.

3. Mount 1 localhost file as a data volume

The -ES71en tag can also mount a single file from the host into a container as a data volume (not recommended).


$ docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash

This allows you to record the command history that has been entered in the container.

If you mount a file directly to the container, using file editing tools, including vi or sed-- ES79en-ES80en, may cause file inode to change, starting with Docker 1.1.0, which will cause error messages. So the recommended way is to mount the file directly in the directory.

Data volume container

If a user needs to share continuously updated data across multiple containers, the easiest way is to use a data volume container. The data volume container is also a container, but its purpose is specifically to provide data volumes to be mounted by other containers.

First, create a data volume container, dbdata, and in it create a data volume mount to /dbdata:


$ docker run -it -v /dbdata --name dbdata ubuntu

root@3ed94f279b6f:/#

View the /dbdata directory:

root@3ed94f279b6f:/# ls

bin boot dbdata dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

You can then use -- ES103en-ES104en in other containers to mount the data volumes in the dbdata container.

For example, create db1 and db2 containers and mount the data volume from the dbdata container:


$ docker run -it --volumes-from dbdata --name db1 ubuntu

$ docker run -it --volumes-from dbdata --name db2 ubuntu

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

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


root@3ed94f279b6f:/# cd /dbdata

root@3ed94f279b6f:/dbdata# touch test

root@3ed94f279b6f:/dbdata# ls

test

View it in the db1 container:


$ docker run -it --volumes-from dbdata --name db1 ubuntu

root@4128d2d804b4:/# ls

bin boot dbdata dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

root@4128d2d804b4:/# ls dbdata/

test

The -- ES134en-ES135en 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 volume mounted.

Containers that mount data volumes using the -- ES138en-from parameter do not themselves need to be kept running.

If mounted containers (including dbdata, db1, and db2) are removed, the data volume will not be automatically deleted. If you want to delete a data volume, you must explicitly use the docker ES146en-ES147en command when you delete the last container that still has it mounted to it to specify that the associated containers are also deleted.

Migrate the data using the data volume container

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

These two operations are described below.

1. Backup

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

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

First, a container worker was created using the ubuntu image. Use the -ES166en-ES167en dbdata parameter to have the worker container mount the data volume of the dbdata container (that is, the dbdata data volume), and the -ES172en $(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 /dbdata command is used to back up the contents under /dbdata to /backup/ backup.tar in the container, that is, backup.tar in the current directory of the host host.

2. Restore

If you want to restore the data to a container, follow these steps.

First create a container dbdata2 with a data volume:

$ docker run -v /dbdata --name dbdata2 ubuntu /bin/bash

Then create another new container, mount the dbdata2 container, and use untar to unzip the backup file to the mounted container volume:


$ docker run --volumes-from dbdata2 -v $(pwd):/backup --name worker ubuntu bash

cd /dbdata

tar xvf /backup/backup.tar

Related articles: