In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
To understand Docker Volume, we first need to know how Docker's file system works. A Docker image is made up of multiple file systems (read-only layers) superimposed. When we start a container, Docker loads the read-only mirror layer and adds a read-write layer on top of it (that is, the top of the mirror stack). If the running container modifies an existing file, the file will be copied from the read-only layer below the read-write layer to the read-write tier, and the read-only version of the file still exists, but has been hidden by a copy of the file in the read-write layer. When you delete the Docker container and restart through the image, the previous changes will be lost. In Docker, the combination of the read-only layer and the read-write layer at the top is called Union File System (federated file system).
In order to preserve (persist) data and share data between containers, Docker proposed the concept of Volume. Simply put, Volume is a directory or file, which can bypass the default federated file system and exist on the host in the form of a normal file or directory.
Summary: Volume can separate the container and the data generated by the container, when you use docker rm my_container to delete the container, it will not affect the relevant data.
We can initialize Volume in two ways, with some small but important differences. We can declare Volume at run time with-v:
Docker manages data in two ways:
Data volume (Data Volumes)
Data volume container (Data Volumes Containers)
Data volumes:
A data volume is a directory specifically designated for one or more containers to bypass Union File System. It is located in the container, and the host directory can be mounted on the data volume. The modification of the data volume is immediately visible, and updating the data will not affect the mirror, thus realizing the migration of data between the host and the container, providing some useful functions for persistence or sharing data, and the use of data volumes. Similar to the mount operation on the directory under Linux
Data volumes can be shared and reused between containers
Data volume data changes are modified directly
Volume data changes are not included in the container
The data volume is persistent until no container uses it
1. Create a data volume
Use the-v option in the docker run command to create data volumes in the container. Use the-v option multiple times to create multiple data volumes. For example:
[root@docker01] # docker run-itd-v / data1-v / date2-- name web003 centos:httpv1
90030942574d6b47b6be2922a86b38f7483956f6231ab5e30fccc9d4431f61ec
When you enter the container web003, you can see that the two data volumes have been successfully created and mounted on / data1 and / date2, respectively.
You can use docker inspect web003 to view mounted data volumes.
[root@docker01 ~] # docker inspect web003
Users can also specify data volumes in Dockerfile:
For example:
FROM debian:wheezy
VOLUME / data
Note the default permission for 1:Docker to mount data volumes is read and write, and users can also specify read-only through: ro.
For example:
2. Mount the host directory as a data volume
If you want to use a directory on the host in the container, you can specify it with the-v parameter (note: pay attention to the contents before and after the colon):
Docker run-v / host/path:/some/path...
This explicitly tells Docker to use the specified host path instead of the root path created by Docker itself and mount it to the specified path in the container (the above example is / some/path). It is important to note that the path on the host local directory must use an absolute path, and if the path on the host does not exist, the directory will be automatically created in the given path.
When using the docker run command, you can specify to mount a local host directory to the container, and you can use the multiple-v option to mount multiple local host directories for a docker container.
Example:
1. Create a / web/webapp1 directory on the host, and create an index.html file as follows:
[root@docker01 ~] # mkdir / web/webapp1-p
[root@docker01 ~] # cd / web/webapp1/
[root@docker01 webapp1] # echo "hello world" > > index.html
[root@docker01 webapp1] # cat index.html
Hello world
[root@docker01 webapp1] #
2. Use an image with an apache service to generate a container
[root@docker01] # docker run-itd-v / web/webapp1/:/var/www/html/-p 8000 itd 80-- name web005 centos:httpv1
3. Visit the container's website service:
The above command loads the host's / web/webapp1 directory into the container's / var/www/html directory. This feature is very convenient when testing, for example, users can place some programs in the local directory to see if the container is working properly. The path to the local directory must be absolute. If the directory does not exist, Docker will automatically create it for you. All the files in the / web/webapp1 directory will appear in the container. This is very helpful for sharing files between hosts and containers, such as mounting source code that needs to be compiled. To ensure portability (not all systems' host directories are available), mount host directories do not need to be specified from Dockerfile.
Data volume container:
If you need to share some data between containers, the easiest way is to use a data volume container. A data volume container is an ordinary container that provides data volumes for mounting use by other containers.
The usage is as follows: first, you need to create a container as a data volume container, and then use-- volumes-from to mount the data volumes in the data volume container when other containers are created.
A common usage scenario is to use pure data volume containers to persist databases, configuration files, or data files, and so on.
For example:
Docker run-name dbdata postgres echo "Data-only container for postgres"
This command will generate a container dbdata (such as VOLUME / var/lib/postgresql/data) using a postgres image that already has a Volume defined in Dockerfile, run the echo command and exit. When we run the docker ps command, echo can help us identify the purpose of a mirror. We can use the-- volumes-from command to identify the Volume of other containers:
Docker run-d-- volumes-from dbdata-- name db1 postgres
Example:
Now let's create a named data volume container:
# docker run-dit-v / test-- name data image
Mount the / test volume in another container using the-- volumes-from option. No matter whether the data container is running or not, other containers can mount the container data volume, of course, it is not necessary to run the container if it is just a separate data volume.
You can then mount the / test volume using-- volumes-from in other containers
# docker run-dit-- volumes-from data-- name test1 image
Add another container:
Note: you can also use multiple-- volumes-from parameters to mount multiple data volumes from multiple containers
Perform docker ps View
Enter the test1 and test2 containers, and execute df to view
You can also inherit other containers containing / test volumes
# docker run-dit-- volumes-from test1-- name test3 image
If you delete mounted containers (including data, db1, and db2), the data volume is not automatically deleted. If you want to delete a data volume, you must use the docker rm-v command to delete the associated container when you delete the last container that still holds it.
Use Data Volume Container to back up, restore and move data
Backup
Another function of data volumes is to use them to back up, restore, and move data. If you are using a data container, it is quite easy to make a backup. Use the-- volume flag to create a new container with the volume loaded, with the following command:
This example should compress everything in Volume into a single tar package
[root@docker01] # docker run-- rm-- volumes-from data-v $(pwd): / backup centos:httpv1 tar cvf / backup/backup.tar / test
Here we create a container to mount the data volume from the data container. Then mount the current directory from the local host to the container's / backup directory. Finally, use the tar command to back up the data volume as backup.tar. When the command is executed and the container stops, we back up the data data volume
Delete the container-rm after execution, and the backup is in the current directory, named backup.tar.
The backup file test.tar of test volume is generated under the current directory of the host.
Restore
Attached:
Permissions and licenses
Usually you need to set the permissions of Volume or initialize some default data or configuration files for Volume. The key point to note is that no instruction after the VOLUME instruction of Dockerfile can change the Volume, such as:
FROM debian:wheezy
RUN useradd foo
VOLUME / data
RUN touch / data/x
RUN chown-R foo:foo / data
The Docker file does not work as expected, and we had hoped that the touch command would run on the mirrored file system, but it was actually running on the Volume of a temporary container. As follows:
FROM debian:wheezy
RUN useradd foo
RUN mkdir / data & & touch / data/x
RUN chown-R foo:foo / data
VOLUME / data
So, remember the location of the VOLUME instruction in Dockerfile (VOLUME is the setting instruction)
If you do not set permissions through the RUN directive, then you need to use the CMD or ENTRYPOINT directive to execute when the container starts
Delete Volumes
Volume can only be deleted under the following circumstances:
Docker rm-v added the-v option when deleting the container
For example, you can tell Docker to delete both the container and its Volume:
Docker rm-v my_container
Docker run-- rm added the-- rm option when running the container
Even with the above two commands, you can only delete Volume without container connections. A Volume connected to a user-specified host directory is never deleted by docker. Otherwise, you'll get some zombie files and directories in the / var/lib/docker/volumes directory, and it's not easy to say what they represent.
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.