Docker Learning Note 18:dockerfile Instruction VOLUME Introduction

Source: Internet
Author: User

Before introducing the volume directive, let's look at the following scenario requirements:

1) The container is created based on the image, and the final container file system consists of a mirrored read-only layer + a writable layer, and the data persistence for process operations in the container is persisted on the writable layer of the container. Once the container is deleted, the data is gone, unless we manually back it up (or create a new image based on the container). Can I keep the data persisted by the container process on the host? So even if the container is deleted, the data is still there.

2) When we are developing a Web application, the development environment is local to the host, but the test environment is run on the Docker container.

In this case, I need to synchronize to the container after I have modified the files (such as HTML,JS, etc.) on the host. This is obviously more troublesome.

3) Multiple containers run a set of associated services, what if they want to share some data?

For these problems, of course we can think of solutions. Docker itself provides a mechanism for associating a directory on a host with a container's directory (called a mount point, or volume), and the content under the container's mount point is the contents of the host directory, similar to the Linux system mount mechanism. In this case, when we modify the contents of the directory on the host, we do not need to synchronize the container, which is immediately effective for the container. Mount points allow multiple containers to be shared.

Let's introduce the specific mechanism.

First, through the Docker Run command

1. Run command: Docker run--name test-it -v/home/xqh/myimage:/data Ubuntu/bin/bash

The-v flag in the container sets a mount point/data (which is a directory in the container) and associates the contents of the/home/xqh/myimage directory on the host with/data.

In this case, the operation of the/data directory in the container, or the/home/xqh/myimage operation on the host, are all in real-time synchronization, because both directories are actually pointing to the host directory.

2. Run command: Docker run--name test1-it -v/data ubuntu /bin/bash

The above-V flag only sets the container's mount point and does not specify the associated host directory. Docker then automatically binds a directory on the host. You can see it through the Docker inspect command.

[Email protected]:~/myimage$ Docker Inspect test1[{"Id":"1fd6c2c4bc545163d8c5c5b02d60052ea41900a781a82c20a8f02059cb82c30c",.............................    "Mounts": [        {            "Name":"0AB0AAF0D6EF391CB68B72BD8C43216A8F8AE9205F0AE941EF16EBE32DC9FC01",            "Source":"/var/lib/docker/volumes/0ab0aaf0d6ef391cb68b72bd8c43216a8f8ae9205f0ae941ef16ebe32dc9fc01/_data",            "Destination":"/data",            "Driver":"Local",            "Mode":"",            "RW":true        }    ],...........................

Each piece of information in the above mounts records information about a mount point on the container, the "Destination" value is the container's mount point, and the "Source" value is the corresponding host directory.

It can be seen that the corresponding host directory is created automatically, and is not intended to be modified on the host, but to allow multiple containers to be shared.

Ii. creating a mount point from Dockerfile

The mount points that are created above with the-V identity of the Docker Run command are valid only for the container that you create.

The dockerfile VOLUME instruction allows you to create a mount point in the mirror so that only the containers created through the image have mount points.

Another difference is that the mount point created by the VOLUME instruction cannot specify the corresponding directory on the host and is automatically generated.

#testFROM ubuntumaintainer hello1volume ["/data1","/ Data2"]

The Dockfile file above specifies two mount points/data1 and/data2 through the volume directive.

We view the container generated by the image created by the Dockerfile through Docker inspect, and we can see the following information

    "Mounts": [        {            "Name":"D411f6b8f17f4418629d4e5a1ab69679dee369b39e13bb68bed77aa4a0d12d21",            "Source":"/var/lib/docker/volumes/d411f6b8f17f4418629d4e5a1ab69679dee369b39e13bb68bed77aa4a0d12d21/_data",            "Destination":"/data1",            "Driver":"Local",            "Mode":"",            "RW":true        },        {            "Name":"6d3badcf47c4ac5955deda6f6ae56f4aaf1037a871275f46220c14ebd762fc36",            "Source":"/var/lib/docker/volumes/6d3badcf47c4ac5955deda6f6ae56f4aaf1037a871275f46220c14ebd762fc36/_data",            "Destination":"/data2",            "Driver":"Local",            "Mode":"",            "RW":true        }    ],

You can see information for two mount points.

Iii. Container Shared Volume (mount point)

Docker Run--name test1-it Myimage/bin/bash

The myimage in the above command is a mirror built with the previous Dockerfile file. This allows the container test1 to have the/data1 and/data2 two mount points.

Below we create another container that can share/data1 and/data2 volumes with Test1, which is the use of--volumes-from tags in Docker run, such as:

Can be a source of different images, such as:

Docker run--name test2-it--volumes-from test1 Ubuntu/bin/bash

It can also be the same image, such as:

Docker run--name test3-it--volumes-from test1 Myimage/bin/bash

The above three containers test1, Test2, test3 have/data1 and/data2 two directories, and the contents of the directory is shared, any one container modified the content, other containers can be obtained.

Iv. Best Practices: Data containers

If multiple containers need to share data (such as persistent databases, configuration files, or data files), consider creating a specific data container with 1 or more volumes.

Other containers share the volume of this data container through--volumes-from.

Because the container's volume essentially corresponds to a directory on the host, this data container does not need to be started.

such as: Docker run--name dbdata myimage echo "Data container"

Note: There is a volume, the data sharing between the container is more convenient, but there are many problems to be solved, such as permission control, data backup, volume deletion and so on. These contents are described in a subsequent article.

Docker Learning Note 18:dockerfile Instruction VOLUME Introduction

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.