A data volume is a special directory that can be used by one or more containers, bypassing UFS and providing a number of useful features:

  • Data volumes can be shared and reused between containers
  • Changes to data volumes take effect immediately
  • Data volume updates do not affect mirroring
  • The volume exists until no container is used

Using data volumes is similar to mounting directories or files in Linux.

1. Create

When using the Docker run command, use the -v flag to create a data volume and mount it to the container. Multiple uses in a run can mount multiple data volumes. Let’s create a Web container and load a data volume into the container’s/WebApp directory.

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

Note: You can also use VOLUME in a Dockerfile to add one or more new volumes to any container created by the image.

2. Mount the host directory

The -v flag can also be used to specify the directory to mount a localhost to the container.

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

Load the/SRC /webapp directory of the host to the /opt/webapp directory of the container. This feature is handy for testing, such as placing programs in a local directory to see if the container is working properly. The path to the local directory must be an absolute path, if the directory does not exist Docker will automatically create it for you.

Note: This usage is not supported in Dockerfile because Dockerfile is intended for porting and sharing. However, the path format varies from operating system to operating system, so it is not currently supported.

The default permission for Docker to mount data volumes is read and write. Users can also specify read-only by :ro.

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

After adding :ro, the mount is read-only.

3. Mount the local host file

The -v flag can also mount a single file from the host to the container

$ sudo docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
Copy the code

This allows you to record the commands entered in the container.

Note: If you mount a file directly, many file editing tools, including vi or sed –in-place, may cause the file inode to change. Starting with Docker 1.1.0, this will result in an error message. So the easiest way is to mount the parent directory of the file directly.

4. Data volume container

If you have some continuously updated data that needs to be shared between containers, it is best to create a data volume container. A data volume container is a normal container that provides data volumes for other containers to mount. First, create a named volume container dbData:

$ sudo docker run -d -v /dbdata --name dbdata training/postgres echo Data-only container for postgres
Copy the code

Then use –volumes-from in another container to mount the data volumes in the dbData container.

$ sudo docker run -d --volumes-from dbdata --name db1 training/postgres
$ sudo docker run -d --volumes-from dbdata --name db2 training/postgres
Copy the code

You can also use multiple –volumes-from parameters to mount multiple data volumes from multiple containers. You can also mount data volumes from other containers that have already mounted data volumes.

$ sudo docker run -d --name db3 --volumes-from db1 training/postgres
Copy the code

Note: The container that used the –volumes-from parameter to mount the data volumes does not need to be running by itself.

If you delete mounted containers (including DBData, DB1, and DB2), the data volume will not be deleted automatically. If you want to delete a data volume, you must use the docker rm -v command when deleting the last container that is still attached to it to specify that the associated container is also deleted. This allows users to upgrade and move data volumes between containers. This is explained in the next section.

Data volumes can be used to back up, restore, and migrate data.

5. The backup

Use the –volumes-from tag to create a container to load the dbData container volume and mount the current /backup directory to the container from the localhost. The command is as follows:

$ sudo docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
Copy the code

After the container is started, the tar command is used to backup the dbdata volume to local /backup/backup.tar.

6. Return

If you want to restore data to a container, first create a container dbDATA2 with the data volume.

$ sudo docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
Copy the code

Then create another container, mount the container for DBdata2, and untar the backup files to the mounted container volume.

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