I. Introduction
Docker
When the container starts, if you want to mount a directory of the host, you can -v
specify it with parameters.
For example, to start a centos
container, mount the /test directory of the host to the /soft directory of the container, which can be specified in the following ways:
docker run -it -v /test:/soft centos /bin/bash
In this way, after the container is started, the /soft directory will be automatically created in the container. In this way, we can make it clear that in the -v parameter, the directory before the colon “:” is the host directory, and the following directory is the directory in the container.
It seems simple, but it’s not. Let’s verify it:
2. The container directory cannot be a relative path
[[email protected] ~]# docker run -it -v /test:soft centos /bin/bash
invalid value "/test:soft" for flag -v: soft is not an absolute path
See 'docker run --help'.
An error is reported directly, indicating that soft is not an absolute path. The so-called absolute path must start with the following slash “/”.
3. If the host directory does not exist, it will be automatically generated
If the /test directory exists on the host, delete it first
[[email protected] ~]# rm -rf /test
[[email protected] ~]# ls /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
start the container
[[email protected] ~]# docker run -it -v /test:/soft centos /bin/bash
[[email protected] /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin soft srv sys tmp usr var
Check the host and find that a new /test directory has been added
[[email protected] ~]# ls /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var
4. What if the host’s directory is a relative path?
This time, let’s try changing the directory name test1
docker run -it -v test1:/soft centos /bin/bash
Then go to the host to see if a new /test1 directory has been added. The result is not. Is it because I used a relative path, so the generated test1 directory is in the current directory, and it turns out that there is still no. Where is the /soft directory in the container mounted? We can get the answer to this question by looking at the “Mounts” part of the container with the docker inspect command.
"Mounts": [
{
"Name": "test1",
"Source": "/var/lib/docker/volumes/test1/_data",
"Destination": "/soft",
"Driver": "local",
"Mode": "z",
"RW": true
}
],
It can be seen that the /soft directory in the container is mounted on the /var/lib/docker/volumes/test1/_data directory on the host
It turns out that the so-called relative path refers to /var/lib/docker/volumes/, which has nothing to do with the current directory of the host.
5. If only -v specifies a directory, how does this correspond?
start a container
[[email protected] ~]# docker run -it -v /test2 centos /bin/bash
[[email protected] /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test2 tmp usr var
Also use the docker inspect command to view the mount directory of the host
"Mounts": [
{
"Name": "96256232eb74edb139d652746f0fe426e57fbacdf73376963e3acdb411b3d73a",
"Source": "/var/lib/docker/volumes/96256232eb74edb139d652746f0fe426e57fbacdf73376963e3acdb411b3d73a/_data",
"Destination": "/test2",
"Driver": "local",
"Mode": "",
"RW": true
}
],
It can be seen that it is similar to the result in 3, except that it is not a directory name of a relative path, but a randomly generated directory name.
6. If the owner and group of a directory are modified in the container, will the corresponding mount point be modified?
First open a container and view the properties of the /soft directory in the container
[[email protected] ~]# docker run -it -v /test:/soft centos /bin/bash
[[email protected] /]# ll -d /soft/
drwxr-xr-x 2 root root 6 Sep 24 03:48 /soft/
View the properties of the /test directory in the host
[[email protected] ~]# ll -d /test/
drwxr-xr-x 2 root root 6 Sep 24 11:48 /test/
Create a new user in the container, modify the owner and group of /soft
[[email protected] /]# useradd victor
[[email protected] /]# chown -R victor.victor /soft/
[[email protected] /]# ll -d /soft/
drwxr-xr-x 2 victor victor 6 Sep 24 03:48 /soft/
Let’s see if the owner and group of the /test directory in the host will change?
[[email protected] ~]# ll -d /test/
drwxr-xr-x 2 mycat mycat 6 Sep 24 11:48 /test/
Turned into mycat. . .
It turns out that this is related to UID. UID, that is, “user identification number”, is an integer, which is used internally by the system to identify users. In general, it corresponds to the user name one-to-one.
First, check the UID corresponding to the victor in the container.
[[email protected] /]# cat /etc/passwd | grep victor
victor:x:1000:1000::/home/victor:/bin/bash
The UID of victor is 1000, so who is the user corresponding to 1000 in the host?
[[email protected] ~]# cat /etc/passwd |grep 1000
mycat:x:1000:1000::/home/mycat:/bin/bash
It can be seen that the user corresponding to UID 1000 in the host is mycat.
7. If the container is destroyed, will the newly created mount directory on the host disappear?
Here, two situations are mainly verified: 1. The host directory is specified, ie -v /test:/soft. 2. No host directory is specified, ie -v /soft
The first case:
[[email protected] ~]# rm -rf /test --First delete the /test directory on the host
[[email protected] ~]# ls / --As you can see, there is no /test directory on the host
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[[email protected] ~]# docker run -it --name=centos_test -v /test:/soft centos /bin/bash --To start the container, I specified the name of the container with the --name parameter for ease of removal
[[email protected]82ad7f3a779a /]# exit
exit
[[email protected] ~]# docker rm centos_test --delete container
centos_test
[[email protected] ~]# ls / --Found that 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
It can be seen that even if the container is destroyed, the newly created mount directory will not disappear. It can be further verified that if the owner and group of the host directory are changed, after the container is destroyed, the owner and group of the host directory will not be restored to the state before mounting.
In the second case, through the above verification, we know that if the host’s directory is not specified, the container will randomly configure a directory in /var/lib/docker/volumes/, then we will see if the container destruction in this case will Causes the deletion of the corresponding directory
Start the container first
[[email protected] ~]# docker run -it --name=centos_test -v /soft centos /bin/bash
[[email protected]6b75579ec934 /]# exit
exit
docker inspect
View the mount directory generated by the container on the host through the 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 and see if the directory exists
[[email protected] ~]# docker rm centos_test
centos_test
[[email protected] ~]# ll /var/lib/docker/volumes/b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301
total 0
drwxr-xr-x 2 root root 6 Sep 24 14:25 _data
It is found that the directory still exists, even if the docker service is restarted, the directory still exists
[[email protected] ~]# systemctl restart docker
[[email protected] ~]# ll /var/lib/docker/volumes/b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301
total 0
drwxr-xr-x 2 root root 6 Sep 24 14:25 _data
8. After mounting the existing directory on the host, operate it in the container and report “Permission denied”.
It can be solved in two ways:
- Close selinux.Temporarily closed:
setenforce 0
Permanently close: Modify the/etc/sysconfig/selinux
file, setSELINUX
the value ofdisabled
. - Start the container as privilegedSpecify
--privileged
parameterslike:docker run -it --privileged=true -v /test:/soft centos /bin/bash
Read More:
- Docker Run ‘echo core > /proc/sys/kernel/core_pattern‘ Error
- [Solved] docker Error: System has not been booted with systemd as init system (PID 1). Can‘t operate. Failed to con
- Linux Install Docker Error: Failed to restart docker.service: Unit docker.service not found.
- The docker export container cannot be run after being imported: error response from daemon: no command specified
- [Solved] docker: Error response from daemon: OCI runtime create failed: container_linux.go:380
- Docker Start Container Error: Error response from daemon: task already exists: unknown
- How to Solve Docker delete container image error: Error response from daemon: conflict: unable to delete 7cc1942f1ed5 (must be forced)
- Docker Error: error invoking remote method ‘docker-start-container‘: error: (http code 500) server error –
- [Solved] Exception in replication between CentOS virtual machine and host
- /usr/bin/ssh-copy-id: ERROR: No identities found [How to Solve]
- SCP path contains special characters Error [How to Solve]
- The solution of insufficient disk space of docker in Ubuntu
- [Solved] error:getaddrinfo ENOTFOUND xxx.xxxx.com xxx.xxxx.com:443
- -bash: sqlplus: command not found [How to Solve]
- Termux setting path environment variable
- [Solved] Docker Staratup Error: Failed to start Docker Application Container Engineadsafdsad.
- Es Container Error: too many open files [How to Solve]
- [Solved] Failed to execute/bin/bash: resource temporarily unavailable
- Installation, Configuration and Simple Use of Rancher
- [Solved] Docker error: “unknown runtime specified NVIDIA” using GPU“