In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to mount data volumes in docker. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Create and load a data volume container 1. Create a data volume container
View command parameters: docker run-- help
See the command help of-v
Here we can see that the-v command has a total of three parameters
-- volume (equivalent to-v)
Explanation: to create a container data volume, to mount a container data volume, you must first have a container data volume. This command is to create a container data volume.
For example:
Docker run-d-- volume / var/volume-- name DATA centos ls
The above command launches (daemon thread) a container named DATA from an image named centos, and executes the ls command after startup. And share the / var/volume directory (or files) in the container, thus creating a data volume container. The above command is equivalent to: docker run-v / var/volume-name DATA centos
The / var/valume proposal here is written as an absolute path, and although the relative path is not misreported, it is easy to be misunderstood. Because even if it is written as a relative path here, docker will be treated as an absolute path. For example, writing the / var/valume here as. / var/volume or var/valume results is the same, which is easy for developers to misunderstand, so it is recommended to write an absolute path.
We notice that the parameter type of the volume command queried above is list, so we can create multiple data volumes in a container.
For example:
Docker run-d-volume / var/volume1-volume / var/volume2-name DATA centos ls
So we created two data volumes in the DATA container
In addition to the above create data volume container, we can also create data volumes in Dockerfile, using the VOLUME command.
For example:
# BUILD-USING: docker build-t data. # RUN-USING: docker run-name DATA dataFROM base-imageVOLUME ["/ var/volume1", "/ var/volume2"] CMD ["ls"]
Note: the ls command here is not necessary, because the container needs to execute a command after startup, which can be written at the end of the docker startup command line or in the CMD command of Dockerfile, and the command line overrides the CMD command in Dockerfile
two。 Load data volume-volumes-from
Explanation: install a data volume from a specified container
When we have created a data volume container or already exist a data volume container, how do we load the data volume container into another container? This requires the-- volumes-from command.
This command loads the data volume into the current container from one or more specified data volume containers. Loading means that if the current container has a directory result of the data volume, it is overwritten, and if not, it is loaded directly.
For example:
Docker run-d-- volumes-from DATA:rw-- name client centos ls
The above command launches (daemon thread) a container named client from the image named centos, and loads the data volume (read and write) from the container named DATA, and executes the ls command after startup. In this way, the data volume (/ var/volume) in DATA is shared with the loaded data volume (/ var/volume) in client, and as long as either side makes changes to the data volume, it will be synchronized to the other side. It is worth noting that instead of synchronizing data using functions like copy and paste, it is useful for oversized files to modify the data directly.
The parameter type of the-- volumes-from command here is also list, so you can also specify multiple data volume containers.
For example:
Docker run-d-volumes-from DATA1:ro-volumes-from DATA2:ro-name client centos ls
Note:-- data volumes loaded by volumes-from can be granted read-write permissions [rw | ro], that is, read write and read only, read-only and read-write. The same permissions for data volumes are used by default.
-- volume-driver
Explanation: select a volume driver for the container
This order is not within the scope of this discussion. Readers can consult the relevant materials on their own if they are interested.
Mount the host directory as a container volume
The above describes how to share data between containers as a data volume. But most of the time, we load the directory in the host computer into the container as a data volume, and the directory in the container is "plug-in", so as to facilitate the operation and maintenance staff to maintain and modify the project configuration.
-- volume [host-dir]: [container-dir]: [rw | ro]
The above command is equivalent to:-v [host-dir]: [container-dir]: [rw | ro]
Explanation: load the host's directory (host-dir) into the container (container-dir) as a data volume and specify read and write permissions to use the data volume permissions by default
For example:
Docker run-d-volume / var/logs:/var/host_logs:ro tomcat
The above command launches a container with a random name from a mirror named tomcat (daemon thread). Mount the host directory / var/logs as a data volume and load it into the currently launched container / var/host_logs, specifying read-only permissions. In this way, the logs directory of the host is shared with the host_logs directory data of the container, and the data changes in the container (data volume) or the host (data volume) are synchronized. Some actual application scenarios include project configuration file mount, log file mount, and so on. Because these files are often viewed and modified, it is very troublesome to modify and view them every time you enter the container. So mounting a host data volume directly can synchronize the data in the container by modifying and viewing the host data volume, thus simplifying the operation steps.
Here are a few points to note:
The absolute path is required for the host directory and container directory. If you do not specify an absolute path to start, an error will be reported, and the error message is invalid mount path,mount path must be abstract.
If the host directory does not exist, it is automatically created, and the created directory permissions are the corresponding directory permissions in the docker container.
If the container directory does not exist, it will be created automatically. The created permission is the directory permission corresponding to the host, and the user and user group is 1000 (seemingly default).
If both the host directory and the container directory exist, the contents of the host directory will overwrite the contents of the container directory, but the directory name will not change.
The mounted data volume (directory) cannot be deleted inside the container, but the mounted data volume can be deleted by the host. After deletion, the directory in the container still exists but the content is empty. Failed to delete Device or resource busy
The stop and deletion of the container do not affect the host data volume, even if the container has been deleted, the contents of the host data volume still exist.
Mounting the data volume is done before the container executes the command after a successful startup, that is, before the Dockerfile CMD command. So you may check the contents of the data volume in the container after mounting the data volume and find that it is not consistent with the host data volume you saw before entering the container. In fact, the data volume has been changed after the container is started. When you exit the container, check that the host data volume has been synchronized.
This is how to mount the data volume in the docker shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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
@ WebServlet (name = "httpServletDemo", urlPatterns = "/ httpServletDemo", initParams = {
© 2024 shulou.com SLNews company. All rights reserved.