Docker Data Storage Volumes Detailed Explanation

Docker Data Storage Volumes Detailed Explanation

By default, the reading and writing of container data occurs at the container's storage layer. When the container is deleted, the data on it will be lost. Therefore, we should try to ensure that no write operations occur in the container storage layer. In order to achieve persistent storage of data, we need to choose a solution to save data. Currently, there are several ways:

  • Volumes
  • Bind mounts
  • tmpfs mounts

The following diagram illustrates these three techniques:

Volumes

Volumes are special directories on the host that can be used by one or more containers. They have the following characteristics:

  • Data volumes can be shared and reused between containers
  • Writing operations to the data volume will not have any impact on the image
  • By default, the data volume will always exist even if the container is deleted.

The purpose of using data volumes is to persist data in containers so that they can be shared between containers or prevent data loss (data written to the container storage layer will be lost).

The steps to use data volumes are generally divided into two steps:

  1. Create a data volume
  2. Use the -v or --mount parameter to mount the data volume to the specified directory of the container, so that all write operations of the container to the specified directory will be saved in the volume on the host machine.

Volume Management

Create a Volume:

$ docker volume create my-vol

View Volumes:

$ docker volume ls
local my-vol
$ docker volume inspect my-vol
[
 {
  "Driver": "local",
  "Labels": {},
  "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
  "Name": "my-vol",
  "Options": {},
  "Scope": "local"
 }
]

We can see that the created Volume my-vol is saved in the directory /var/lib/docker/volumes/ . All future write data for this Volume will be saved in the directory /var/lib/docker/volumes/my-vol/_data .

To delete a volume:

$ docker volume rm my-vol

Or delete all unused volumes:

docker volume prune

Mount the data volume to the container directory

After creating a Volume, we can use it by specifying the -v or --mount parameter when running the container:

Use the --mount parameter:

$ docker run -d \
 --name=nginxtest \
 --mount source=nginx-vol,destination=/usr/share/nginx/html \
 nginx:latest

Source specifies the volume, and destination specifies the file or folder in the container.

Or use the -v parameter:

$ docker run -d \
 --name=nginxtest \
 -v nginx-vol:/usr/share/nginx/html \
 nginx:latest

After the mount is successful, the container reads or writes data from the /usr/share/nginx/html directory, which actually reads or writes data from the nginx-vol data volume of the host machine. Therefore, Volumes or Bind mounts can also be seen as a way for containers and hosts to share files.

The -v parameter uses a colon to separate source and destination. The first half of the colon is the source, and the second half is the destination.

If you mount a data volume that doesn't exist yet, Docker will automatically create it. (Therefore, creating a data volume is not necessary)

If the directory to be mounted in the container is not an empty directory, the files in the directory will be copied to the data volume. (Under Bind mounts, the directory on the host will always overwrite the directory to be mounted in the container)

The -v parameter and the --mount parameter have almost the same functions. The only difference is that you can only use the --mount parameter to mount a data volume when running a service.

Using read-only data volumes

In some cases, we want a data volume to be read-only for a container, which can be achieved by adding the readonly option:

$ docker run -d \
 --name=nginxtest \
 --mount source=nginx-vol,destination=/usr/share/nginx/html,readonly \
 nginx:latest

Or use the -v parameter:

$ docker run -d \
 --name=nginxtest \
 -v nginx-vol:/usr/share/nginx/html:ro \
 nginx:latest

Volumes usage scenarios

Please refer to this article: Docker Data Storage Summary

References

https://docs.docker.com/storage/volumes/#share-data-among-machines

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Introduction to container data volumes in Docker
  • Two ways to manage volumes in Docker
  • Docker volume deletion operation
  • Docker volumes file mapping method
  • How to implement Docker volume mounting
  • Docker volume usage details and examples
  • Docker writes data to the data volume

<<:  Detailed explanation of two ways to dynamically change CSS styles in react

>>:  Detailed explanation of JDBC database link and related method encapsulation

Recommend

Solve the problem of not finding NULL from set operation to mysql not like

An interesting discovery: There is a table with a...

htm beginner notes (must read for beginners)

1. What is HTML HTML (HyperText Markup Language):...

Common methods and problems of Docker cleaning

If you use docker for large-scale development but...

Discussion on the problem of garbled characters in iframe page parameters

I encountered a very unusual parameter garbled pro...

Detailed explanation of CSS BEM writing standards

BEM is a component-based approach to web developm...

Introduction to HTML basic controls_PowerNode Java Academy

The <input> tag The <input> tag is us...

Deep understanding of the mechanism of CSS background-blend-mode

This article is welcome to be shared and aggregat...

Why Google and Facebook don't use Docker

The reason for writing this article is that I wan...

Detailed explanation of the process of building an MQTT server using Docker

1. Pull the image docker pull registry.cn-hangzho...

How to install redis in Docke

1. Search for redis image docker search redis 2. ...