A summary of the methods of Docker catalog Mount _docker

Source: Internet
Author: User
Tags docker run

When the Docker container is started, if you want to mount a directory of host hosts, you can specify it with the-v parameter.

For example, I want to start a CentOS container, the host's/test directory is mounted to the container's/soft directory, which can be specified in the following ways:

# docker Run-it-v/test:/soft Centos/bin/bash

This allows the/soft directory to be created automatically within the container when the container is started. In this way, we can make it clear that, in the-v argument, the directory before the colon ":" is the host directory, and the directory that follows is the directory within the container.

Seemingly simple, but it is not, let us verify the following:

A, container directory can not be considered relative path

[Root@localhost ~]# Docker run-it-v/test:soft centos/bin/bash Invalid
value '/test:soft ' for flag-v: soft are not An absolute path to
' Docker run--help '.

Direct error, prompt soft is not an absolute path, the so-called absolute path, must be the following slash "/" start.

Second, host directory if it does not exist, it will automatically generate

If the/test directory exists in the host, delete it first

[Root@localhost ~]# rm-rf/test
[root@localhost ~]# ls/
bin boot Dev etc home lib lib64 media mnt opt proc Root Run sbin SRV sys tmp usr var

Start container

[Root@localhost ~]# Docker run-it-v/test:/soft centos/bin/bash
[root@a487a3ca7997/]# ls
bin Dev etc home Lib lib64 lost+found Media mnt opt proc root run sbin soft SRV sys tmp \ usr var

To view the host, a new/test directory was found

[root@localhost ~]# ls/
bin boot Dev etc home lib lib64 media mnt opt proc root run sbin SRV sys test TMP usr var

Third, host directory if the relative path?

This time, let's try a different directory name test1.

# docker Run-it-v Test1:/soft Centos/bin/bash

And then to the host to see if a new/test1 directory, the result is not, because I use a relative path, so the generated test1 directory in the current directory, the results found still not. Where did the/soft catalogue in the container go? We can get the answer to this question by Docker the inspect command and looking at the part of the container "mounts".

"Mounts": [
    {
      "Name": "Test1",
      "Source": "/var/lib/docker/volumes/test1/_data",
      "destination": "/ Soft ",
      " Driver ":" Local ",
      " Mode ":" Z ",
      " RW ": True
    }
  ],

As can be seen, the/soft directory in the container is mounted on the/var/lib/docker/volumes/test1/_data directory on the host

Originally, the so-called relative path refers to the/var/lib/docker/volumes/, regardless of the host's current directory.

If just-v specifies a directory, how does this correspond?

Start a container

[Root@localhost ~]# Docker run-it-v/test2 centos/bin/bash
[root@ea24067bc902/]# ls
bin Dev etc home Lib Lib Lost+found Media mnt opt proc root run sbin SRV sys test2 TMP usr var

Also use the Docker inspect command to view a host's mount directory

' Mounts ': [
    {
      ' Name ': ' 96256232eb74edb139d652746f0fe426e57fbacdf73376963e3acdb411b3d73a ', '
      Source ': ' /var/lib/docker/volumes/96256232eb74edb139d652746f0fe426e57fbacdf73376963e3acdb411b3d73a/_data ",
      " Destination ":"/test2 ","
      Driver ":" Local ",
      " Mode ":",
      "RW": True
    }
  ],

As you can see, the result in the same 3 is similar, except that it is not a directory name for a relative path, but a randomly generated directory name.

If the owners and groups of the directories are modified in the container, will the corresponding mount point be modified?

First open a container to view the properties of the/soft directory within the container

[~]# Docker run-it-v/test:/soft centos/bin/bash
[root@b5ed8216401f/]# ll-d/soft/root@localhost
drwxr-xr- X 2 root 6 Sep 03:48/soft/

To view the properties of the/test directory in a hosting host

[Root@localhost ~]# ll-d/test/
drwxr-xr-x 2 root 6 Sep 11:48/test/

Create a new user in the container, modify the/soft and the group of the owners

[root@b5ed8216401f/]# useradd Victor
[root@b5ed8216401f/]# chown-r] victor.victor/soft/
[root@b5ed8216401f/ ]# ll-d/soft/
drwxr-xr-x 2 Victor Victor 6 Sep 03:48/soft/

To see if the host and group of the/test directory will change?

[Root@localhost ~]# ll-d/test/
drwxr-xr-x 2 mycat mycat 6 Sep 11:48/test/

Turned into Mycat ...

Originally, this is related to UID, UID, that is, "User ID", is an integer, the system uses it to identify users. In general, it corresponds to a username of one by one.

First look at what the UID of Victor corresponds to in the container,

[Root@b5ed8216401f/]# cat/etc/passwd | grep Victor
Victor:x:1000:1000::/home/victor:/bin/bash

Victor's UID is 1000, so who is the 1000 corresponding user in the host?

[Root@localhost ~]# cat/etc/passwd |grep 1000 Mycat:x:1000:1000::/home/mycat:/bin/bash

As can be seen, the host in the UID 1000 corresponding user is mycat.

Six, the container destroyed, the new Mount directory on the host will disappear?

Here, the main validation of two cases: one, the designated host directory, that is,-v/test:/soft. Second, does not specify the host directory, namely-v/soft

In the first case:

[Root@localhost ~]# rm-rf/test  --First delete the host's/test directory
[root@localhost ~]# ls/  --you can see that there is no/test directory bin on host hosts
Boot Dev etc home lib lib64 media mnt opt proc root run sbin SRV sys tmp usr var
[root@localhost ~]# Docker- Name=centos_test-v/test:/soft Centos/bin/bash--boot container, in order to remove the convenience, I used the--name parameter to specify the name of the container
[root@82ad7f3a779a/]# exit
Exit
[Root@localhost ~]# Docker RM centos_test-  -delete container
centos_test
[root@localhost ~]# ls/  --Discovery/ The test directory still exists
bin Boot Dev etc home lib lib64 media mnt opt proc root run sbin SRV sys test TMP usr var

As you can see, even if the container is destroyed, the new mount directory will not disappear. Further, it is also verifiable that, if a change occurred to the owner and group of the host directory, the owner and group of the host directory would not revert to the state before the mount if the container was destroyed.

In the second case, it is known from the above verification that if the directory of the host is not specified, the container will randomly configure a directory in the/var/lib/docker/volumes/, so let's see if the container destruction in this case will result in the deletion of the corresponding directory.

Start the container first

[Root@localhost ~]# Docker run-it--name=centos_test-v/soft centos/bin/bash
[root@6b75579ec934/]# exit
exit

To view the mount directory generated by the container on the host by the Docker inspect command

' Mounts ': [
    {
      ' Name ': ' b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301 ', '
      Source ': ' /var/lib/docker/volumes/b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301/_data ",
      " Destination ":"/soft ","
      Driver ":" Local ",
      " Mode ":",
      "RW": True
    }
  ],

Corresponds to the/var/lib/docker/volumes/b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301/_data directory

Destroy the container to see if the directory exists

[Root@localhost ~]# Docker rm centos_test
centos_test
[root@localhost ~]# ll/var/lib/docker/volumes/ b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301 Total
0
drwxr-xr-x 2 root 6 Sep 24 14:25 _data

found that the directory still exists, even if the Docker service is restarted, the directory still exists

[root@localhost ~]# systemctl Restart Docker
[root@localhost ~]# ll/var/lib/docker/volumes/ b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301 Total
0
drwxr-xr-x 2 root 6 Sep 24 14:25 _data

Seven, mount the host already exists in the directory, in the container for its operation, reported "Permission denied".

Can be resolved in two ways:

1> closes the SELinux.

Temporary shutdown: # Setenforce 0

Permanent shutdown: Modifies the/etc/sysconfig/selinux file to set the SELinux value to disabled.

2> start the container in a privileged manner

Specify--privileged parameters

Such as:

# docker run-it--privileged=true-v/test:/soft Centos/bin/bash

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.