web123456

Docker Notes Data Volumes for Containers

Hello everyone, I'm the positive Hunan Wok 💪💪💪💪💪💪💪💪💪💪💪.

1. Data volume concept

Reflections:

  • After a Docker container is deleted, is the data generated in the container still there?
    在这里插入图片描述

The answer is no, the data is stored in the container, and if the container is deleted, the data is deleted along with it

  • Can Docker containers and external machines exchange files directly?
    在这里插入图片描述

The answer is no, because the external machine has no connection to thedockerInternals are not interoperable with each other, but external machines are interoperable with the host machine

  • Want to interact with data between containers?

Interaction can be done by means of data volumes

data volume

  • A data volume is a directory or file on the host machine
  • When the container directory and the data volume directory are bound, the other side's modifications will be immediately synchronized, very similar to the linux mount, like mounting a USB drive
  • A data volume can be mounted by multiple containers at the same time
  • A container can also mount multiple data volumes
    在这里插入图片描述

Data Volume Role:

  • Container Data Persistence
  • External machine and inter-container communication
  • Inter-container data exchange

2. Configuring data volumes

  • When creating a boot container, use the -v parameter to set the data volume
docker run ... -v Host directory (files): In-container directory (files) ...
  • 1

Caveats:

  1. The directory must be an absolute path
  2. If the directory does not exist, it is automatically created
  3. Multiple data volumes can be mounted

3. Data volume container

There are two main ways to exchange data across multiple containers:

  • Multiple containers mounting the same data volume
  • Data Volume Container, using a container as a carrier for indirect mounts
    在这里插入图片描述
    Configuring the Data Volume Container
  1. Create a boot c3 data volume container and set the data volume with the -v parameter
docker run -it --name=c3 -v /volume centos:7 /bin/bash
  • 1
  1. Create the c1 and c2 containers and set the data volumes with the -volumes-from parameter.
docker run -it --name=c1 --volumes-from c3 centos:7 /bin/bash
docker run -it --name=c2 --volumes-from c3 centos:7 /bin/bash
  • 1
  • 2

4. Data volume summary

  1. Data Volume Concept
    - A directory or file on the host

  2. Data Volume Role
    - containersData persistence
    - Client and container data exchange
    - Inter-container data exchange

  3. Data Volume Container
    - Create a container, mount a directory, and let other containers inherit it (-volume-from)
    - Data Volume Configuration in a Simple Way