preface

Previous articles have detailed the three core concepts of good containers: image, container, and repository. Containers are better for stateless applications. If we want to persist data, in order to keep data in Docker, we can use volumes! Let the data mount to our local! This way the data won’t be deleted because of the container.

knowledge

What is a container data volume

A volume is essentially a file or directory that exists in one or more containers and is mounted to the container by a Docker, but is not part of a federated file system. The volume concept solves not only the problem of data persistence, but also the problem of sharing data between containers.

Why use container volumes

(1) Data persistence. When the container is restarted, data will not be lost. For example, we can use the volume to persist the MySQL directory, so that the database data will not be lost when the container is restarted.

(2) If data is stored in a mirror, other processes on the host cannot easily access the data.

What are the characteristics of container volumes

(1) Data volumes can share or reuse data between containers

(2) Changes in the data volume are not included in the update of the mirror

(3) Changes in the volume can take effect directly

(4) The life cycle of a data volume continues until no container can use it

The operation of the volume

Creating a Data Volume

Commands for creating data volumes

docker volume create myvolume

Tip: By default, the data volume created by Docker is in local mode and can only be accessed by the container of the local host.

Run the -v command

Docker run-it -v Absolute path of the host directory: specifies the image name of the directory in the container

Tip: Use -v to specify the path to be persisted in the container. Docker will automatically create volumes for us and bind them to the container.

Testing:

docker run -it -v /home/ceshi:/home centos /bin/bash

Check whether the data volume is successfully mounted.

Docker inspect Container ID

 

Test data sharing between the container and the host: you can see that in the container, the created data is seen in the host!

 ​​​​​

Tip: After the container stops exiting, the host changes are also synchronized to the container!

Deleting a Data Volume

Command for deleting a data volume

docker volume rm myvolume

Tip: Deleting a container does not automatically delete data volumes that have been created. Therefore, you need to manually delete data volumes that are no longer used.

Install mysql using Docker

(1) Pull mirror image

Docker pull mysql: 5.7

(2) Start container

docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD = 123456 – name mysql1 mysql: 5.7

Tip: Use your local Navicat connection to test it out.

(3) Check the local /home/mysql directory

(4) Delete mysql container

docker rm -f mysql1

Note: After deleting the mysql database, you can still see that the mysql database is stored locally.

conclusion

At this point, I believe you have a certain understanding of Docker container volume to do persistent storage, but also understand the common operations of Docker volume, and the implementation principle of volume has a clearer understanding.

If this blog has been helpful to you, please leave a comment + like + favorites. Welcome to the docker learning journey!