In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Docker mounts local directories and implements file sharing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Summary of the methods of mounting local directories on docker:
When the Docker container starts, if you want to mount a directory of the host, you can specify it with the-v parameter.
For example, if I want to start a centos container, the / test directory of the host 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
In this way, after the container starts, the directory of / soft is automatically created in the container. In this way, we can make it clear that in the-v parameter, the colon ":" the directory in front is the host directory, and the directory behind is the directory in the container.
It seems simple, but it's not. Let's verify it:
Container directory cannot be a relative path
[root@localhost] # docker run-it-v / test:soft centos / bin/bashinvalid value "/ test:soft" for flag-v: soft is not an absolute pathSee 'docker run-- help'.
A direct error message indicates that soft is not an absolute path. The so-called absolute path must begin with a slash "/".
Second, if the host directory does not exist, it will be generated automatically
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 the container
[root@localhost ~] # docker run-it-v / test:/soft centos / bin/bash [root@a487a3ca7997 /] # lsbin 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
[root@localhost ~] # ls / bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var
What if the directory of the host is a relative path?
This time, let's try to change the directory name to test1.
# docker run-it-v test1:/soft centos / bin/bash
Then go to the host machine to see if a new / test1 directory has been added, and it turns out that it 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 it still doesn't exist. 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 through the docker inspect command.
"Mounts": [{"Name": "test1", "Source": "/ var/lib/docker/volumes/test1/_data", "Destination": "/ soft", "Driver": "local", "Mode": "z", "RW": true}]
As you can see, the / soft directory in the container is mounted to the / var/lib/docker/volumes/test1/_data directory on the host
Originally, the so-called relative path means that / var/lib/docker/volumes/, is independent of the current directory of the host.
Fourth, if only-v specifies a directory, how does this correspond?
Start a container
[root@localhost ~] # docker run-it-v / test2 centos / bin/bash [root@ea24067bc902 /] # lsbin 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}]
As you can see, the result is similar to that in 3, except that it is not the directory name of the relative path, but a randomly generated directory name.
5. If the owner and group of the directory are modified in the container, will the corresponding mount point be modified?
First open a container and check the properties of the / soft directory in the container.
[root@localhost] # docker run-it-v / test:/soft centos / bin/bash [root@b5ed8216401f /] # ll-d / soft/drwxr-xr-x 2 root root 6 Sep 24 03:48 / soft/
View the attributes of the / test directory in the host
[root@localhost] # ll-d / test/drwxr-xr-x 2 root root 6 Sep 24 11:48 / test/
Create a new user in the container and modify the owner and group of / soft
[root@b5ed8216401f /] # useradd victor [root@b5ed8216401f /] # chown-R victor.victor / soft/ [root@b5ed8216401f /] # 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?
[root@localhost] # ll-d / test/drwxr-xr-x 2 mycat mycat 6 Sep 24 11:48 / test/
It turns out to be mycat.
It turns out that this has something to do with UID. UID, that is, the "user identification number", is an integer that is used internally to identify the user. In general, it corresponds to the user name one by one.
First, check the UID corresponding to victor in the container.
[root@b5ed8216401f /] # cat / etc/passwd | grep victorvictor:x:1000:1000::/home/victor:/bin/bash
The UID of victor is 1000, so who is the corresponding user of 1000 in the host?
[root@localhost ~] # cat / etc/passwd | grep 1000mycat:x:1000:1000::/home/mycat:/bin/bash
As you can see, the user corresponding to UID 1000 in the host is mycat.
6. If the container is destroyed, will the newly created mount directory on the host disappear?
Here, we mainly verify two cases: first, specify the host directory, that is,-v / test:/soft. 2. No host directory is specified, that is,-v / soft
The first situation:
[root@localhost ~] # rm-rf/test
-- first delete the / test directory of the host
[root@localhost ~] # ls /-- you can see that 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 [root@localhost ~] # docker run-it-- name=centos_test-v / test:/soft centos / bin/bash-- start the container for convenience I specified the name of the container [root@82ad7f3a779a /] # exitexit [root@localhost ~] # docker rm centos_test with the-- name parameter-- delete the container centos_ test [root @ localhost ~] # ls /-- and 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
As you can see, even if the container is destroyed, the newly created mount directory will not disappear. It can also be verified that if the owner and group of the host directory change, after the container is destroyed, the owner and group of the host directory will not return to the state before mounting.
In the second case, through the above verification, we know that if the host directory is not specified, the container will randomly configure a directory in / var/lib/docker/volumes/, so let's see whether the destruction of the container in this case will lead to 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 /] # exitexit
View the mount directory generated by the container on the host through the docker inspect command
"Mounts": [{"Name": "b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301", "Source": "/ var/lib/docker/volumes/b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301/_data", "Destination": "/ soft", "Driver": "local", "Mode": "," RW ": true}]
Corresponding to the / var/lib/docker/volumes/b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301/_data directory
Destroy the container to see if the directory exists
[root@localhost ~] # docker rm centos_testcentos_ test [root @ localhost ~] # ll / var/lib/docker/volumes/b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301total 0drwxr-xr-x 2 root root 6 Sep 24 14:25 _ data
Found that the directory still exists, even if the docker service is restarted
[root@localhost ~] # systemctl restart docker [root@localhost ~] # ll / var/lib/docker/volumes/b53164cb1c9f1917788638692fb22ad11994cf1fbbc2461b6c390cd3e10ea301total 0drwxr-xr-x 2 root root 6 Sep 24 14:25 _ data
7. After the host already exists in the directory, operate it in the container and report to "Permission denied".
It can be solved in two ways:
1 > close selinux.
Temporary shutdown: # setenforce 0
Permanently close: modify the / etc/sysconfig/selinux file to set the value of SELINUX to disabled.
2 > start the container in privileged mode
Specify-- privileged parameter
For example: # docker run-it-- privileged=true-v / test:/soft centos / bin/bash
Docker mounts the local directory to achieve file sharing:
Docker can support mounting a directory on a host to an image.
Docker run-it-v / home/dock/Downloads:/usr/Downloads ubuntu64 / bin/bash
With the-v parameter, the directory of the host before the colon must be an absolute path, and the path mounted in the image after the colon.
Now the files in the host can be shared in the image.
The path permission of the default mount is read and write. Available if specified as read-only: ro
Docker run-it-v / home/dock/Downloads:/usr/Downloads:ro ubuntu64 / bin/bash
Docker also provides an advanced usage. It's called data volume.
Data volume: "in fact, it is a normal container designed to provide data volumes for other containers to mount." It feels like a data mount information defined by a container. Other container launches can directly mount the mount information defined in the data volume container.
Look at the example:
Docker run-v / home/dock/Downloads:/usr/Downloads-- name dataVol ubuntu64 / bin/bash
Create a normal container. He is given a name with-- name (if not specified, a random name will be generated).
Then create a new container to use this data volume.
Docker run-it-- volumes-from dataVol ubuntu64 / bin/bash
-- volumes-from is used to specify the data volume from which to mount the data.
Thank you for reading! On "Docker how to mount the local directory and achieve file sharing" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.