“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Why use data volumes

It is convenient for the host to directly access the files in the container. The files in the container are not persisted. After the container is deleted, the file data will also disappear.

Data volume characteristics

  • Data volumes exist in the host’s file system, independent of the container and separate from the lifecycle of the container.
  • Data volumes can be directories or files. Containers can use data volumes to share data with hosts, realizing data sharing and exchange between containers.
  • When the container starts initialization, if the image used by the container contains data, the data is copied to the data volume.
  • The container makes changes to the data volume in real time.
  • Data volume changes do not affect mirror updates. The data volume is independent of the federated file system and the mirror is based on the federated file system. Mirroring and data volumes do not interact.

Three ways to mount a Docker data volume

Bind mounts: mounts a file or directory on a host to a container

Volumes: Created and managed by Docker. Use the docker volume command

TMPFS mounts: TMPFS is a temporary file system based on memory. Data is not stored on disks. The virtualized TMPFS file system is virtualized

Bind mounts a data volume

Command parameters: docker run/create -v

Specific usage:

  1. -v Host file or folder path: specifies the path of a file or folder in a container
  2. –mount type=bind, SRC = host file or folder path, DST = file or folder path in the container

Note: When using method 2, the folder or file after SRC must be created in advance

Volumes Used to mount data volumes

Command parameters: docker run/create -v

Specific usage:

  1. -v volume-name: indicates the file or folder in a container
  2. –mount type=volume, SRC = volume-name, DST = path of files or folders in the container

Volume object management commands:

Docker volume Commands manage volume volume objects docker volume create Create volume objects docker volume inspect View details about data volumes docker volume ls View created volume objects Docker Volume Prune Delete Unused data volume objects Docker volume rm Delete one or more data volume objectsCopy the code

TMPFS Mount data volumes

Docker run/create -v

  1. –mount type=tmfps,dst=PATH

How do I share data volumes from other containers?

Docker run/create –volumes-from

Command format :docker run/create –volumes-from CONTAINER

Precautions for using container data volumes

Docker data volumes will use volumes more.

Note when using:

  • If you mount an empty data volume to a non-empty directory in the container, files in that directory are copied to the data volume.
  • If you mount a non-empty data volume to a directory in the container, the directory in the container displays the data in the data volume. If there is data in the directory in the original container, the raw data is hidden.

The first rule helps us initialize the contents of the data volume. The second rule ensures that the data you mount is always what you expect.