Data-persistent data volumes that you learn Docker:5.docker

Source: Internet
Author: User

First, don't forget the two previous questions:
1. How do I save our changes in the container?
2. How do I install the software in Docker if the Apt-get is not installed?

Remove Mirror command

Before that, remember a command,

sudo docker rm ID/NAME

The image that was created is deleted.

Now delete the mirror with id "cc8a23b1d624".

sudo docker rm cc8a23b1d624


As you can see, the image with ID cc8a23b1d624 is deleted.

It is also possible to find that the corresponding directories in the local /var/lib/docker/containers directory have been deleted.
The data persistence for Docker is discussed below.

Creation of data volume data volumes

The official explanation is that the data volume is a special directory that can be used by one or more containers, bypassing UFS.
UFS is the abbreviation for UNIX file systems.

    1. Data volumes can be shared and reused between containers
    2. Changes to the data volume will take effect immediately
    3. Updates to the data volume do not affect mirroring
    4. The data volume is always present by default, even if the container is deleted
      The use of data volumes, similar to Linux under the directory or file mount, the image is designated as a mount point in the directory of the files will be hidden, can be displayed to see the mounted data volumes.
      Specify a directory using parameter v when creating a container.
-i-t---v /home/mungo/workspace/docker/container_data:/data docker/whalesay /bin/bash

This lets us mount a data volume. If the/home/mungo/workspace/docker/container_data directory does not exist, Docker will automatically create the directory.

The above command is to hang the container_data under the /data of the container. You can see in the container:

Last time, learn how to view container details, namely:

sudo docker inspect DataTest

Here "Datatest" is the name that you just created the container to specify. The JSON-formatted container information is returned above, and the Mounts node data is viewed as follows:

You can see that Mounts is the directory we specified when we created it.
This feature is very handy, like the second question before – How do I install the software in Docker if the Apt-get is not installed? at this point the user can place some programs in the local directory and then install them. Note that the path to the local directory must be an absolute path .
For example, I copy a file vscode-amd64.deb to a directory that is hanging, and I can see it directly in the Docker container.

It is important to note that the default permissions for Docker mounted data volumes are read-write, and users can also specify as read-only by: Ro.

-i-t---v /home/mungo/workspace/docker/container_data:/data:ro docker/whalesay /bin/bash

At this point, the problem left before is solved by one.

Data sharing for data volumes

So now that you can hang a local directory in a Docker container, can the mounted data volume be reused by other Docker containers?

-i-t---v /home/mungo/workspace/docker/container_data:/data docker/whalesay /bin/bash


You can see that the container named "Datatest" and "DataTest2" are started. At the same time they hang in the same directory as the host host, and see a consistent one in their respective containers.
Then create a file in "DataTest2"-"test.txt", in the go to see "datatest" Hanging directory and host local mounted directory, found that the new "test.txt" file exists.

After that, add "hello" to "datatest" to modify the file "Test.txt". Take three places to view and find the results of three places are consistent.

Deletion of data volumes

As you can see from the above, data volumes are designed to persist data. So what does it have to do with its lifecycle and container? Delete container data volumes will not be deleted?
Below I exit the container DataTest2 and delete it.

View the data volumes hanging on the container datatest and host native Local.

You can see that the changes are in the container datatest and before the native local host.
Therefore, the life cycle of a data volume is independent of the container, and Docker does not automatically delete the data volume after the container is deleted, and there is no mechanism for garbage collection to handle data volumes that do not have any container references.
If you need to remove the data volume while removing the container. The data says you can use this command when you delete a container docker rm -v . But I try not to succeed, I do not know why.
However, these non-master data volumes can occupy a lot of space and it can be cumbersome to clean up. But Docker officials are trying to solve the problem, and I believe there will be an easy way to do this, and now we can look at the pull requests for the progress of this issue.

This time it's over. If the apt-get is not installed, how to install the software in Docker is a problem. But with the depth of the study of Docker, there is still a lot of things to know about the month. Go ahead.

Data-persistent data volumes that you learn Docker:5.docker

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.