Docker data volume technology

What is a container data volume

Docker concept Review: Packaging apps and environments into an image!

The data? If the data is in the container, then we delete the container, the data will be lost! Requirement: Data can be persisted

MySQL database can be stored locally.

There can be a technology for sharing data between containers! Docker container generated data, synchronization to the local!

This is roll technology! Directory mount, mount our container directory to Linux!

Summary: Container persistence and synchronization operations! Data can also be shared between containers!

Using a Data Volume

  • Docker run -v Host directory: container directory -p Host port: container Port containerCopy the code
    • We practice

      • Inspect for viewing synchronization details
      • Create a file in the container to see if it is synchronized to a local folder
      • With the container closed, make changes to the local file to see if the changes are synchronized to the file in the container

Mysql of actual combat

  • The command
docker run -d -p 3310:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7 
Copy the code
  • parameter
    • -d Background running
    • -p Port mapping
    • -v Data volume mounting: synchronizes data
    • -e Setting environment variables: specifies the MySQL login password
    • –name Name of the container

  • Test result: The connection is successful

Problems encountered

  • When the first to use the latest version of mysql: 8.0 – at the time of using navicat for connection, always puzzling error
    • Solution: Change to version 5.7 without any problems
  • Mysql > MYSQL_ROOT_PASSWORD=123456 mysql > MYSQL_ROOT_PASSWORD=123456 mysql > MYSQL_ROOT_PASSWORD=123456 mysql > MYSQL_ROOT_PASSWORD=123456
    • -e MYSQL_ROOT_PASSWORD=123456

Even if we delete the container, the data volume we mount locally is still not lost, which makes the container data persistent

Named mount and anonymous mount

Anonymous mount

  • -v Path in the container!
docker run -d --name nginx01 -v /etc/nginx nginx 
Copy the code
  • Here we see that this is an anonymous mount, and we just write the path inside the container after -v, not the path outside the container
  • View information about all volumes
docker volume ls 
Copy the code

A named mount

  • -v Name of the volume to be mounted: the path in the container
Docker run -d --name nginx01 -v /etc/nginx nginxCopy the code
  • Here we see that this is a named mount, where we write the path inside the container after -v, not the path outside the container, but a name

Docker content directory: /var/lib/docker

  • All the docker container volume, without a specified directory are: / var/lib/docker/volume/xx / _data while forming
  • We can easily find one of our volumes by named mount, and named mount is the most common way to use volumes

  • How do I distinguish between a named mount and an anonymous mount
    • -v Container path # Mount anonymously
    • -v Volume name: Container path # Named Mount
    • -v/Outside path: Inside path # Specify path to mount
  • extension
    • -v Container path: ro rw Changes the read and write permissions
    • ro readonlyread-only
    • rw readwriteCan read but write

Once the container permission is set, the container has restrictions on what we can mount. Ro Seeing Ro indicates that the path can only be accessed from the host, not from the container

Docker run -d --name nginx01 -v /etc/nginx:ro nginx docker run -d --name nginx01 -v /etc/nginx:rw nginxCopy the code

Data volume container

  • In fact even guaranteed data sharing between containers is a problem

    • A-volumes -from ->B
  • Tests synchronization between two mirrors

    • Let’s start with three containers

      • Create docker01
      • Docker02 — Volumes from docekr01
      • Docker03 — Volumes from Docekr02
      • View data sharing between Docker01 and Docker02
      • View data sharing between Docker01 and Docker03
      • Delete docker02 from docker01 and docker03
    • Conclusion:

Implement data sharing between multiple mysql databases

docker run -d -p 3310:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7 
docker run -d -p 3310:3306 --volumes-from mysql01 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 mysql:5.7 
Copy the code
  • Conclusion:
    • Transfer of configuration information between containers. The data volume container declaration cycle continues until no container is in use

      • Even if some containers are deleted, data will still exist. Data will not be completely deleted until all shared containers are deleted
    • But once you persist it locally, the local data won’t be deleted.