Detailed explanation of Bind mounts for Docker data storage

Detailed explanation of Bind mounts for Docker data storage

Before reading this article, I hope you have a preliminary understanding of Volumes. For details, please refer to this article:

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:

Bind mounts

The Bind mounts mode is very similar to Volumes. The difference is that the Bind mounts mode mounts any file or folder on the host to the container, while Volumes essentially mounts an area managed by the Docker service (the default is a folder under /var/lib/docker/volumes) to the container.

The use of bind mounts is similar to that of volumes, and the host files are mounted in the container through -v or --mount parameter. Here is an example:

When using the --mount parameter, you need to specify type=bind :

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

The above example mounts the /usr/local/web folder on the host to the /usr/share/nginx/html folder in the container.

Or use the -v parameter:

$ docker run -d \
 --name=nginxtest \
 -v /usr/local/web:/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 /usr/local/web directory of the host. Therefore, Volumes or Bind mounts can also be seen as a way for containers and hosts to share files.

If you use Bind mounts to mount a host directory to a non-empty directory in a container, the files in the non-empty directory in the container will be hidden, and the files that the container can access when accessing this directory are all from the host directory. This is also the biggest behavioral difference between Bind mounts mode and Volumes mode.

Bind mounts usage scenarios

Please refer to this article: Docker Data Storage Summary

References

https://docs.docker.com/storage/bind-mounts/

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:
  • How to modify the storage location of Docker default images and containers
  • Detailed explanation of Docker's persistent storage and data sharing
  • Docker Data Storage Volumes Detailed Explanation
  • Docker storage driver introduction
  • Summary of Docker Data Storage
  • Docker data storage tmpfs mounts detailed explanation
  • Solution to the problem of insufficient storage resource pool of Docker server

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

>>:  JavaScript to implement login slider verification

Recommend

Summary of Nginx load balancing methods

To understand load balancing, you must first unde...

Introduction to building a DNS server under centos7

Table of contents 1. Project environment: 2: DNS ...

In-depth analysis of MySQL lock blocking

In daily maintenance, threads are often blocked, ...

Analysis of MySQL data backup and recovery implementation methods

This article uses examples to describe how to bac...

Detailed explanation of the pitfalls of Apache domain name configuration

I have never used apache. After I started working...

Vue implements simple calculator function

This article example shares the specific code of ...

MySQL tutorial DML data manipulation language example detailed explanation

Table of contents 1. Data Manipulation Language (...

MySQL 8.0.11 Installation Tutorial under Windows

This article records the installation tutorial of...

How to install Graphviz and get started tutorial under Windows

Download and installConfigure environment variabl...

Detailed tutorial on configuring nginx for https encrypted access

environment: 1 CentOS Linux release 7.5.1804 (Cor...

43 Web Design Mistakes Web Designers Should Watch Out For

This is an article about website usability. The a...