Three ways for Docker to implement mounting

1. Why is there a mount?

The application running in the container may generate data. If the data is directly stored in the container, if the container is deleted, the data in the container will be deleted together, for example:

If you are running a database container, the data files stored directly in the database container may easily lead to data loss when you delete the container.

In order to prevent data loss, the data can be saved on the host machine, which is convenient for data sharing between containers

2. Mounting method: single file folder data volume

1. Single file

The command is as follows:

docker run -d --name cat4 \

--restart = always \

-v ~/server.xml:/usr/local/tomcat/conf/server.xml \

tomcat:8

This code completes in total:

1. Created a new container, running tomcat8, when the host computer restarts, it will automatically start the container, and name the container cat4, so that it is convenient to enter the command to view the log, -d is the background operation

2. Use the file mounting method to mount the server.xml file of the host machine to the /usr/local/tomcat/conf/server.xml file of the container, which can be understood as /usr/local/tomcat/conf/server. xml is equivalent to a shortcut

The container details can be viewed through the command: docker inspect cat4

2. folder

The command is as follows:

docker run -d --name mysql \ 

-v /data/mysql:/var/lib/mysql \

-e MYSQL_ROOT_PASSWORD=root \

mariadb

This code completes in total:

1. Create a new container, run mariadb, name it mysql, and mount the folder's /data/mysql folder on the host machine to the /var/lib/mysql path of the container. -v will Automatically create host folders, -e configuration environment variables

2. Among them, /var/lib/mysql and MYSQL_ROOT_PASSWORD=root depend on the mirror source

After the creation is complete, you can also run docker inspect mysql to view the container details

In addition, you can also run the command: ll /data/mysql to view the mounted source file, there is data at this time

 You can also run the command: docker rm -f mysql to delete the container

Continue to run the command after deleting the container: ll /data/mysql , the result is consistent, because the file exists on the host machine

3. Data volume

The essence of a data volume is still a folder. Docker can automatically specify a mount folder, but the path is relatively deep and mixed with random characters, so you can create a data volume and give it an alias. When using the path, You can use the alias to indicate the complete folder path, the command is as follows:

docker volume create my-vol   creates a data volume named my-vol 

docker volume ls to view the list of data volumes

docker inspect my-vol to view the details of the data volume, where "/var/lib/docker/volumes/my-vol/_data" is the real directory for storing data

 After preparing the data volume, create a container, similar to how you mount a folder

docker run -d --name mysql1\

-v my-vol:/var/lib/mysql \

-e MYSQL_ROOT_PASSWORD=root \

mariadb

Manually specify my-vol to mount to the /var/lib/mysql path. When deleting the container, the data volume will not be deleted. If you want to delete the container together with the data volume, you can add --volumes after the delete command, such as: docker rm -f mysql --volumes

If no mount is specified manually, a new data volume mount will be created automatically, as shown in the following code

docker run -d --name mysql2\

-e MYSQL_ROOT_PASSWORD=root \

mariadb

You can also view the list of data volumes through the command docker volume ls , and the path of the specified mount folder is very complicated

Guess you like

Origin blog.csdn.net/weixin_72125569/article/details/126920724