Detailed explanation of Docker container data volumes

Detailed explanation of Docker container data volumes

What is

Let’s first look at the concept of Docker:

Package the application and the running environment into a container. The running can be accompanied by the container, but we hope that the data is persistent and that it is possible to share data between containers.

If the data generated by the Docker container is not saved as part of the image by generating a new image through docker commit, then when the container is deleted, the data will naturally disappear.

In order to save data in docker we use volumes.

In a word: It is somewhat similar to RDB and AOF in our Redis

What can I do?

A volume is a directory or file that exists in one or more containers and is mounted to the container by Docker, but does not belong to the Union File System, so it can bypass the Union FileSystem to provide some features for persistent storage or shared data:

The design purpose of the volume is data persistence, which is completely independent of the life cycle of the container. Therefore, Docker will not delete the mounted data volume when the container is deleted.

Features:

  • Data volumes can be used to share or reuse data between containers
  • Changes in the volume can take effect directly
  • Changes to the data volume will not be included in the image update
  • The lifecycle of a data volume container lasts until no container uses it (--volumes from)

Summarize:

  • Persistence of container data
  • Inheritance + shared data between containers

Data Volume

Direct command addition

docker run -it -v /host absolute path directory:/container directory image namedocker run -it -v /host absolute path directory:/container directory:ro image name//with command, specify access rights, ro: read only

Check whether the data volume is mounted successfully:

docker inspect container id 

Add using DockerFile

Create a new mydocker folder in the root directory and enter

You can use the VOLUME instruction in a Dockerfile to add one or more data volumes to the image.

DockerFile Build

For writing DockerFile, you can refer to the DockerFile files of each image in DockerHub, such as tomcat: https://github.com/docker-library/tomcat/blob/300ac03f4696c761a81fa10afbb893f3368061de/8.5/jdk8/openjdk-buster/Dockerfile

#volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,-------success1"
CMD /bin/bash

Generate an image after build

Get a new image zzyy/centos

Run container

Through the above steps, the volume directory address in the container is already known, where is the corresponding host directory

Remark:

Docker mounts the host directory. Docker access appears cannot open directory, Permission denied

Solution: Add a --privileged=true parameter after mounting the directory

Data volume container

What is

The named container mounts the data volume, and other containers share data by mounting this (parent container). The container that mounts the data volume is called a data volume container.

Transitive sharing between containers (--volumes-from)

docker run -it --name dco2 --volumes-from dc01 zzyy/cenos // dc01 is the first created container, dco2 inherits dc01 to achieve data sharing

The data volume is mounted by the parent container (dc01). If dc01 is mounted on dc02 and dc03 and then deleted, the data volume will still be valid.

The configuration information is transferred between containers, and the life cycle of the data volume continues until no container uses it.

Summarize

This is the end of this article about Docker container data volumes. For more information about Docker container data volumes, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker data volumes and data containers detailed introduction and examples
  • Docker data volume, data volume container detailed introduction
  • Introduction to container data volumes in Docker
  • Docker container data volume named mount and anonymous mount issues
  • Two ways to manage volumes in Docker
  • Docker data volume common operation code examples
  • A brief summary of Docker container data volume mounting
  • Docker mounts a volume to save MySQL data

<<:  Analysis on how to solve the problem of Navicat Premium connecting to MySQL 8.0 and reporting error "1251"

>>:  JavaScript to implement input box content prompt and hidden function

Recommend

Solve the problem that ifconfig and addr cannot see the IP address in Linux

1. Install the Linux system on the virtual machin...

Example of using CSS to achieve floating effect when mouse moves over card

principle Set a shadow on the element when hoveri...

MySQL 8.0.16 installation and configuration graphic tutorial under macOS

This article shares the installation and configur...

Example code for implementing raindrop animation effect with CSS

Glass Windows What we are going to achieve today ...

How to use bind to set up DNS server

DNS (Domain Name Server) is a server that convert...

Keep-alive multi-level routing cache problem in Vue

Table of contents 1. Problem Description 2. Cause...

border-radius method to add rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

Implementation steps for enabling docker remote service link on cloud centos

Here we introduce the centos server with docker i...

Analysis of the process of simply deploying nginx in Docker container

1. Deploy nginx service in container The centos:7...

Solution to IDEA not being able to connect to MySQL port number occupation

I can log in to MYSQL normally under the command ...

Convert psd cut image to div+css format

PSD to div css web page cutting example Step 1: F...

js array fill() filling method

Table of contents 1. fill() syntax 2. Use of fill...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

Examples of implementing progress bars and order progress bars using CSS

The preparation for the final exams in the past h...