1. Data volume

A data volume is a special directory that can be used by containers and has the following features:

Data volumes can be shared and reused between containers

The modification takes effect immediately

Data volume updates do not affect mirroring

If a container uses a data volume, it will always exist

Preparations:

Create a directory, and create a file in the directory, the file to write content.




Create a data volume in a container

When using the docker run command, you can use the -v flag to create a data volume within the container, and you can specify to mount a local existing directory to the container as the data volume:

docker run -d –name app1-it -v ${PWD}/webapp:/root/webapp ubuntu bash

The directory establishes a relationship with the inside of the container so that changes to the data volume occur both inside and outside of the container. For example, if the container mounts a file, the file will not be lost when the container is suspended.

Note: The default permission of the mounted data volume is RW (read/write). If ro (read/write) is required, you need to add the corresponding ro parameter. The command can be changed to:

docker run -d –name app1-it -v ${PWD}/webapp:/root/webapp:ro ubuntu bash

Let’s operate it together:

Create a webApp directory, create a file file in the directory, and write this is a file to the file.




The echo ${PWD} command identifies the current directory.




Create and start the app1 container and mount the data volume




Access the container and find the root directory to view the mounted data volumes.




The data volume directory is mapped to the directory in the container. Therefore, whether the data volume is modified inside the container or outside the container, the corresponding data volume will be changed.




Read-only demo




2. Data volume container

The data volume container is used to share continuously updated data between containers. The data volume container provides data volumes for other containers to mount.

Example:

Create a data volume container DB1

docker run -d –name db1 -v/dbdata -ti ubuntu bash

Create containers db2 and DB1 to share dbData data

docker run -d –name db2 –volumes-from db1 -ti ubuntu bash

Modifying the contents of DBData in either db1 or DB2 takes effect in both containers

Delete data volume container:

If the mounted container is deleted, the data volume will not be deleted automatically. If you want to delete a data volume, you must display the docker rm -v command when deleting the last container that is still attached to it. In the figure below, you can see that even after aatest is deleted, there are still fileA files in bbtest.

In aatest, the file fileA is created, and fileA is visible in bbtest. After aatest is deleted, fileA is still visible in bbtest.

Data volume containers can be used to back up and restore data volumes to achieve data migration.

Backup:

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

docker run –volumes-from dbdata -v ${PWD}:/backup –name worker ubuntu \tar cvf /backup/backup.tar/dbdata

Description:

Create a container worker using an Ubuntu image. Use the –volumes-from dbdata parameter to get the worker container to mount the dbData data volume. Use the ${PWD}:/backup parameter to mount the local directory to the /backup directory of the worker container.

After the worker starts, use tar to backup the contents under /dbdata to /backup/backup.tar in the container.

Create a dbData volume container and write the following files: fileA, fileB, and fileC

Run a backup command to create a backup tar package:

Recovery:

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

docker run -d -v /dbdata–name dbdata2 ubuntu /bin/bash

Create a new container, mount the dbdata2 container, and use tar to decompress the backup files into the mounted container volume:

docker run –volumes-from dbdata2 -v ${pwd}:/backup ubuntu tar xvf /backup/backup.tar