Docker Basics: Data Management

Source: Internet
Author: User
Tags docker run

In the process of using Docker, users often need to be able to view the data generated by the application in the container, or need to back up the data in the container, or even share the data between multiple containers, which inevitably involves the data management operation of the container. There are two main ways to manage data in a container: Data Volumes, Data Volume Containers.

Data Volumes

A data volume is a special directory that can be used by a container, bypassing the file system and providing 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 the data volume do not affect mirroring.
4. The data volume will persist until no container is used.

The use of a data volume is similar to the mount operation of a directory or file under Linux.

create a data volume within a container

When using the Docker Run command, use the-V flag to create a data volume within the container. Multiple data volumes can be created using the-v flag multiple times.

In the following example we use the Myimg/webapp image to create a 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 app.py
mount a host directory as a data volume

Using the-V flag can also specify that a local existing directory be attached to the container as a data volume:

$ sudo docker run-d-p–name web-v/var/data:/opt/webdata myimg/webapp python app.py

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

This feature is especially handy when testing, such as a user can place programs or data into a local directory and then use it in a container. Also, the path to the local directory must be an absolute path, and if the directory does not exist, Docker will create it automatically.
The default permissions for Docker mounted data volumes are read-write (rw) and can be specified by the RO flag as read-only:

$ sudo docker run-d-p–name web-v/var/data:/opt/webdata:ro myimg/webapp python app.py

Add: After Ro, the data in the data volume that is mounted in the container becomes read-only.

mount a local host file as a data volume

The-V flag can also mount a file from a host to the container as a data volume, but doing so poses some problems. The directory where the files are mounted is recommended or not.

Data Volume Container

If users need to share some continuously updated data between containers, the simplest way is to use a data volume container. A data volume container is actually a generic container that is dedicated to providing data volumes for other containers to mount. Here is a brief description of how to use it.

First, you create a data volume container mydata, and in which you create a data volume to mount to the/data directory.

$ sudo docker run-it-v/data–name mydata Ubuntu

Then use--volumes-from in other containers to mount the data volumes in the MyData container. For example, create two containers mycon1 and Mycon2, and mount the data volumes from the MyData container:

$ sudo docker run-it--volumes-from mydata–name mycon1 ubuntu$ sudo docker run-it--volumes-from mydata–name Mycon2 UB Untu

(Note that the data volume information is not specified in the command, i.e. the directory where the data volume is mounted in the new container is the same as the source container.) )

At this point the containers mycon1 and Mycon2 both mount the same data volume to the same directory/data. Three containers can be seen by any of the other containers that write data in that directory.
You can use the--volumes-from parameter multiple times to mount multiple data volumes from multiple containers. You can also mount data volumes from other containers that already have containers mounted. And the container that uses the--volumes-from parameter to mount the data volume itself does not need to remain in the running state.
However, when you delete a container that is mounted on a data volume, 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 delete the associated container at the same time when you delete the last container that also mounts it.
The use of data volume containers allows users to freely upgrade and move data volumes between containers, which are described in detail below.

migrating data with a data volume container

A data volume container can be used to back up and restore data volumes to enable migration of data.

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 with an Ubuntu image. The--volumes-from mydata parameter is also used to let the worker container mount the data volume of the MyData container. Next 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/data command is used to back up the contents of/data to/backup/backup.tar within the container, that is, the Backup.tar under the current directory of the host host.

Recovery

If you want to restore data to a container, you can follow the steps below. First, create a container with a data volume MYDATA2:

$ sudo docker run-v/data–name mydata2 Ubuntu/bin/bash

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

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

Docker Basics: Data Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.