(Docker notes): Container data volume concept, MySQL synchronization data case

table of Contents

Container data volume

What is a container data volume?

Use data volume

MySQL sync data


Container data volume

What is a container data volume?

  • Package the application and environment into a mirror!
  • data? If the data is in the container, then we delete the container and the data will be lost. Requirements: data can be persisted
  • MySQL, the container is deleted, and the database is deleted. Requirements : MySQL data can be stored locally
  • There can be a data sharing technology between containers. The data generated in the Docker container is synchronized to the local .
  • This is the volume technology : the mounting of directories, the directories in our containers are mounted on Linux.

  • To sum up one sentence: the persistence and synchronization operations of containers can also share data between containers !
     

Use data volume

  • Method 1: directly use the command to mount 
docker run -it -v 主机目录:容器内目录
  • Test: Mount the container's home directory to the ceshi directory under the host's home directory
 docker run -it -v /home/ceshi:/home centos /bin/bash
  • After entering the container, enter the home directory and create a new file
touch test.java
ls 查看目录下的内容,能看到这个文件
  • Switch to the host at this time, or open a terminal to view the ceshi directory under the host's home directory
cd /home/ceshi
ls 查看目录下的内容,发现 test.java 文件已经同步过来了

  • Let's test again. Can the data be synchronized after we turn off the container?
    • 1. Exit first to exit the container
    • 2. docker ps check if the container is really stopped
    • 3. On the host, modify the file vim /home/ceshi/test.java
    • 4. docker start starts the container
    • 5. View the content of the file in the container cat /home/test.java
  • It can be found that the content of the file is the same as the previous modification, indicating that the file has been synchronized

  • The advantage of this method is that future modifications only need to be modified locally, and the container will be automatically synchronized.
  • docker inspect view mount information

MySQL sync data

  • Get the mysql mirror here to use version 5.7
docker pull mysql:5.7
  • Run the container to mount data and configuration files. Note that mysql startup requires a configuration password
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
  • Parameter Description:
    • -d run in the background
    • -p port mapping
    • -V volume mount
    • -e environment configuration
    • --name container name
  • After the startup is successful, you can use navicat to connect to mysql or docker exec -it to enter the container mysql -p to enter the mysql console
  • Create a database, and then view the mapped path on the host, you can view the newly created database file

  • Delete and adjust this container docker rm -f mysql01
  • Go to the host to view the files under the path cd /home/mysql/data
  • As a result, the file still exists and is not lost, which realizes the function of container data persistence

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/108555192