In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Data involved in the door
In the previous case, if we need to copy data from the host to the container, we usually use the copy command of Docker, so the performance is still a little bit poor, and there is no way to achieve the Imando performance of the local disk? Yes!
Data volumes can bypass the copy system and share directories or files among multiple containers and between containers and host hosts. Data volumes bypass the copy system and achieve local disk Imax O performance.
This article first uses a simple case to show the reader the basic use of data volumes.
Take the nginx image used earlier as an example. When you run the container, you can specify a data volume as follows:
Docker run-itd-- name nginx-v / usr/share/nginx/html/-p 80:80 bc26f1ed35cf
The running effect is as follows:
At this point, we create a data volume and mount it to the container's / usr/share/nginx/html/ directory. Friends know that this directory is actually the nginx html directory, where the data volume is mounted. After a while, we only need to change the local mapping location to modify the page.
Next, use the docker inspect command to view the details of the container you just created, and find the data volume mapping directory, as follows:
As you can see, Docker defaults to the / var/lib/docker/volumes/0746bdcfc045b237a6fe2288a3af9d7b80136cacb3e965db65a212627e217d75/_data directory of the host as the source directory. Next, go to this directory, as follows:
It is found that the contents of the files in this directory are the same as those in the / usr/share/nginx/html/ directory in the container, because if an empty data volume is mounted to a non-empty directory in the container, the files in this directory will be copied to the data volume (if a non-empty data volume is mounted to a directory in the container, the data in the data volume will be displayed in the directory in the container. If there is data in the directory in the original container, the original data will be hidden.
Tips:
Because Docker in Mac is a bit special, the / var/lib/xxxx directory mentioned above can be entered directly if it is in the linux environment. If it is in mac, you need to execute the following command first and enter the / var/lib/xxx directory in the newly entered command line:
Screen ~ / Library/Containers/com.docker.docker/Data/vms/0/tty
Next, modify the contents of the index.html file in the file as follows:
Echo "hello volumes" > index.html
After the modification is completed, go back to the browser and type http://localhost to view the data in the index.html page in nginx and find that it has changed. Indicates that the files in the host are shared to the container.
Combined with host directory
The use of data volumes mentioned above is not the best solution. In general, we may need to explicitly specify that a directory in the host should be mounted to the container as follows:
Docker run-itd-- name nginx-v / Users/sang/blog/docker/docker/:/usr/share/nginx/html/-p 80:80 bc26f1ed35cf
This is to mount the / Users/sang/blog/docker/docker/ directory in the host to the container's / usr/share/nginx/html/ directory. Next, the reader only needs to add the html file in the / Users/sang/blog/docker/docker/ directory, or modify the html file, and you can see the effect immediately in the nginx access.
This usage is very convenient for development and testing without having to redeploy, restart the container, and so on.
Note: the host address is an absolute path
More actions
Data volumes in Dockerfile
If the developer uses Dockerfile to build the image, you can also declare the data volume when building the image, such as the following:
FROM nginxADD https://www.baidu.com/img/bd_logo1.png / usr/share/nginx/html/RUN echo "hello docker volume!" > / usr/share/nginx/html/index.htmlVOLUME / usr/share/nginx/html/
In this way, an anonymous data volume is configured, and during the run, the data is written to the / usr/share/nginx/html/ directory to achieve stateless changes in the container storage layer.
View all data volumes
Use the following command to view all data volumes:
Docker volume ls
As shown in the figure:
View data volume details
You can view the data details according to volume name, as follows:
Docker volume inspect
The execution result is as follows:
Delete data Volum
You can use the docker volume rm command to delete a data volume, or you can use docker volume prune to bulk delete a data volume, as follows:
During batch deletion, all the data volumes cannot be deleted, and one is left. This is because the data volume is still in use. Stop and remove the relevant container, and delete the data volume again, as shown in the figure:
Data volume container
A data volume container is a container dedicated to mounting data volumes, which is mainly used for reference and use by other containers. The so-called data volume container is actually an ordinary container, as shown below:
Create a data volume container
Create a data volume container as follows:
Docker run-itd-v / usr/share/nginx/html/-- name mydata ubuntu
The execution effect of the command is as follows:
Reference container
Use the following command to reference the data volume container:
Docker run-itd-- volumes-from mydata-p 80:80-- name nginx1 nginxdocker run-itd-- volumes-from mydata-p 81:80-name nginx2 nginx
At this point, both nginx1 and nginx2 mount the same data volume to the / usr/share/nginx/html/ directory. Any one of the three containers modifies the files in this directory, and the other two can see the changes.
At this point, use the docker inspect command to check the details of the container and find that the descriptions of the data volumes of the three containers are consistent, as shown below:
Summary
This article mainly introduces the container operation in the data volume, on the whole, it is still very simple, friends, have you learned it?
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support 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.