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

Steps to deploy Docker project in IDEA

Now most projects have begun to be deployed on Do...

MySQL learning database operation DML detailed explanation for beginners

Table of contents 1. Insert statement 1.1 Insert ...

How to hide rar files in pictures

You can save this logo locally as a .rar file and...

React Routing Link Configuration Details

1. Link's to attribute (1) Place the routing ...

Graphical introduction to the difference between := and = in MySQL

The difference between := and = = Only when setti...

html option disable select select disable option example

Copy code The code is as follows: <select> ...

Advantages of MySQL covering indexes

A common suggestion is to create indexes for WHER...

Implementation of vertical centering with unknown height in CSS

This article mainly introduces the implementation...

How to make your own native JavaScript router

Table of contents Preface Introduction JavaScript...

The difference between html Frame, Iframe and Frameset

10.4.1 The difference between Frameset and Frame ...

Solve the installation problem of mysql8.0.19 winx64 version

MySQL is an open source, small relational databas...

Summary of 11 amazing JavaScript code refactoring best practices

Table of contents 1. Extracting functions 2. Merg...

React example showing file upload progress

Table of contents React upload file display progr...