In-depth understanding of Docker container volumes

Docker container volumes are a powerful feature that enables data sharing and persistent storage between containers. This blog will introduce the concept, purpose and operation steps of Docker container volumes to help readers better understand and use container volumes.

1. What is a Docker container volume?

  • Container volumes are a data management mechanism provided by Docker for sharing data between containers and hosts.
  • Container volumes can provide persistent storage, so data remains even if the container is deleted or recreated.

2. The purpose of container volumes

  • Sharing data between containers: Multiple containers can share data through container volumes to achieve data interaction between different containers.
  • Sharing data between containers and hosts: Container volumes can be associated with the host file system to share data inside and outside the container.
  • Data persistence storage uses container volumes to persist data in the container to the host machine to prevent data loss due to container failure or deletion.

3. Container volume related commands

docker volume create <volume_name>                          创建容器卷
docker volume ls                                            查看容器卷
docker volume inspect <volume_name>                         查看容器卷详情
docker volume rm <volume_name>                              删除容器卷
docker run -v <volume_name>:<container_path> <image_name>   关联容器卷

4. Operation steps of Docker container volume

  1. Create a container volume: Use docker volume createthe command to create a container volume, and you can specify the name and options.
  2. To associate volumes at launch time: In docker runthe command, use the -v parameter to associate the created container volume. You can specify the mapping relationship of container host paths.
  3. Using volumes within containers: Within a container's application, container volumes can be used like a file system, reading and writing data.
  4. Manage container volumes: Use the d ocker volumecommand to operate container volumes, such as listing all container volumes, deleting volumes that are no longer used, etc.

5. Demonstration

1. Create a container volume:

`docker volume create 容器卷名称`: 创建默认的 Docker 容器卷,存储在 Docker 管理的卷存储区中,具有持久化存储的特性,适用于容器之间的数据共享,具有较好的移植性

 `docker volume create --opt type=none --opt device=主机地址 --opt o=bind 容器卷名称`:创建与主机地址关联的卷,通过直接关联到主机的路径,实现容器与主机之间的数据共享,更加灵活但不够可移植

2. Associate the volume when starting the container:

`docker run  -v 容器卷名:对应容器内路径 镜像ID`

`docker run -d -p 8889:6379 -v 容器卷名称:/bin/container --name my-redis redis:latest`-d 是后台运行,-p 是匹配主机和容器端口映射,-v 后面跟容器卷路径,--name容器重命名,redis:latest镜像ID和版本

3. Write data into the device:

`docker exec my-redis` : 进入容器

`mkdir /bin/container/a.txt` : 容器中创建一个 a.txt文件

4. Start another container and associate the same volume:

`docker run -d -p 8890:6380-v 容器卷名称:/bin/container --name my-redis2 redis:latest`  创建另一个容器,关联该容器卷

5. Check data synchronization

Check whether the local container volume and the data in the container are synchronized

6. Summary

1. The same container volume can be applied to multiple containers and the data is shared.

Example: Create container volume C. Container A and container B are respectively associated with container volume C. If container A creates a file in container volume C, it will be synchronized to container volume C and container B.

2. After the container is deleted, the container volume data will be retained.

Example: Create container volume C. Container A and container B are respectively associated with container volume C. If container A and container B are deleted later, container volume C will still be retained.

3. Real-time data synchronization

Example: Create container volume C. Container A and container B are respectively associated with container volume C. Whether data or files are added to container volume C, they will be synchronized in real time to the paths associated with container A and container B, and vice versa.

4. When creating a container and associating the container volume, you can also directly associate the host address. (Not recommended)

example:docker run -v 主机地址:对应容器内路径 镜像ID

Analysis of advantages and disadvantages:

docker volume createCreate container volumes for easy portability and container publicity

docker run -v 主机地址:对应容器内路径 镜像IDNot easy to portability and container sharing

reference

Docker Documentation: Manage data in Docker
Docker Documentation: Use volumes

Guess you like

Origin blog.csdn.net/weixin_44183847/article/details/131851628