A preface.

I haven’t updated it for a long time. I’m lazy. Consequences are also very serious, at least for writing this article I reviewed another half an hour, haha. Today is about data volume in Docker, let’s get started…

What is a data volume? What’s it good for?

Before answering the above two questions, I would like to raise a question: after the introduction of the last article, we have learned the basic use of Docker, such as pulling images, starting containers and other operations. So if we accidentally delete the container, the data in the container will also disappear, which is obviously not true. In this case, we need to back up the files in the container. How do we do that?

Docker provides us with a cp command that is not the subject of this article, but is worth mentioning anyway, using the following: Docker cp container ID :/ source path/destination path — copy files under the container to Linux docker cp/source path container ID :/ destination path — copy files under Linux to the container

It may be confusing to see it this way… TXT file from /home/docker to /opt/docker

Easy, right? If you want to copy files from docker, just swap the source path and destination path. Although this approach is simple, it clearly does not meet our backup needs. For example, it is impossible to write a script to do this for our mysql data files. First, it consumes performance, and second, the data may not be synchronized.

It would be nice if we could make a connection between the files inside the Docker container and the files on Linux so that when any changes are made on one side, the other side automatically synchronizes. Install mysql and use data volume to back up and synchronize data files to Linux.

3. Use Docker to build mysql and mount data volumes

(1) You can use -v to mount the docker run it -v host directory: container directory

Docker pull mysql:8

(3) start the mysql: 8, docker run -d -it –rm -p 3306:3306 -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=1234 –name mysql01 mysql:8

-e # add environment variables, MYSQL_ROOT_PASSWORD set password -v # Mount data volumes, you can use multiple -v to mount multiple data volumes, after mounting, files in the two directories will be synchronized

(4) However, I encountered a problem. After mounting the configuration file inside the mysql container to the external Linux, the startup failed or the configuration file was not synchronized to the host. I don’t know the specific reason, please leave a message. However, in my tests, you can simply create the configuration file in advance and mount it. Here’s what the website says:

The /my/custom directory already contains a custom configuration file called config-file.cnf. So, I also created one and threw it in the directory with nothing:

Then mount and start it:

docker run -d -it --rm -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=1234 --name mysql01 mysql:8

Then it started successfully, but I think the configuration file needs to be created by itself, because I went into the mysql container to look at the configuration file without mounting the configuration file, and the inside configuration file is also not configured at all.

(5) To sum up, you only need to mount the configuration folder and define the configuration content by yourself. The name of the configuration file can be config-file.cnf.

(6) If you accidentally delete the mysql container, it doesn’t matter, just specify the mount path of the data volume again, and the data will return.

Four. Three methods for mounting data volumes and their differences

The mounting modes are as follows:

(1) Anonymous mount: -v container path. Such as: Docker run – d – it – rm -p 8080:8080 – name tomcat01 – v/usr/local/tomcat/webapps tomcat: nine at the moment, The Tomcat Webapps will be mounted to the /var/lib/docker-volumes folder on the host computer, and a random folder will be generated. You can use the docker Volumes ls command to see which anonymously mounted volumes exist.

(2) Named mount: -v Volume name: container path example: Docker run – d – it – rm -p 8080:8080 – name tomcat01 -v tomcat01: / usr/local/tomcat/webapps tomcat: 9 and the difference between anonymous mount, The name of the /var/lib/docker-volumes directory is no longer meaningless, but the name you specified, in this case tomcat01

(3) Specify the mount path: -v host path: container path this way is demonstrated above. The difference is also obvious, customizing where data is stored.

Well, the use of data volume is recorded here, welcome to comment and exchange