Introduction to container data volumes in Docker

Introduction to container data volumes in Docker

Docker container data volume

If the data is all in the container, then the data will be lost when we delete the container, so we hope that the data can be persistent.
For example, in a MySQL container, we hope that the data can be stored locally so that the data will not be lost when the MySQL container is deleted.
There can be a data sharing technology between containers. The data generated in the Docker container can be synchronized to the local computer. This is the volume technology. That is the data mounting technology, which mounts the directory in our container onto Linux.

insert image description here
After mounting, the operations we perform in the container will be synchronized to the Linux host.

Using Data Volumes

Method 1: Mount directly using command -v

docker run -it -v host directory: container directory#test[root@sumarua home]# docker run -it -v /home/ceshi:/home centos /bin/bash

# After starting, we can check it through docker inspect container id] 

insert image description here

Example

MySQL data persistence

[root@sumarua home]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=666666 --name mysql mysql5.7

Named and anonymous mounts

#Anonymous mount -v container path docker run -d -p --name nginx01 -v /etc/nginx nginx
#View all volumes docker volume ls

insert image description here

# Named mount# Through -v volume name: path in container docker run -d -p --name nginx02 -v juming-nginx:/etc/nginx nginx
#View docker volume ls 

insert image description here

We can check the specific location where the volume is mounted

docker volume inspect [volume name]

insert image description here

All volumes in Docker containers are in /var/lib/docker/volumes/xxxx/_data if no directory is specified.
By mounting with a name we can easily find the volume.
How to determine whether it is a named mount, an anonymous mount, or a specified path mount

-v path in container #anonymous mount -v volume name: path in container #named mount -v /host path: path in container #specified path mount

expand:

# Change the read and write permissions through -v container path: ro rw ro readonly #read-only rw readwrite #readable and writable #Readable and writable by default #Once the container permissions are set, the container will limit the content we mount #ro This path can only be operated through the host machine, and cannot be operated inside the container.

Method 2: Dockerfile

Dockerfile is a build file and command script used to build a docker image.
This script can be used to generate images. The images are layered, and the scripts are commands one by one, and each command is a layer.

# Create a dockerfile file. The name can be random. Dockerfile is recommended.
# Content command (uppercase) parameter in the file FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
# Each command here is a layer of the image

Data volume container

--volumes-from

# Usage [root@sumarua]#docker run -it --name docker02 --volumes-from docker01 sumarua/centos

Realize data synchronization and data sharing between containers

insert image description here

Deleting files from a data volume container does not affect data access in other containers mounted on this container. It is a copy concept and a backup copy mechanism.

insert image description here

The configuration information is transferred between containers. The life cycle of the data volume container lasts until no container is used.
However, once persisted locally, the local data will not be deleted.

Summarize

This is the end of this article about container data volumes in Docker. For more relevant Docker 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:
  • Two ways to manage volumes in Docker
  • Docker volume deletion operation
  • Docker volumes file mapping method
  • How to implement Docker volume mounting
  • Docker Data Storage Volumes Detailed Explanation
  • Docker volume usage details and examples
  • Docker writes data to the data volume

<<:  40 web page designs with super large fonts

>>:  uni-app WeChat applet authorization login implementation steps

Recommend

JS implements a stopwatch timer

This article example shares the specific code of ...

Implementing file content deduplication and intersection and difference in Linux

1. Data Deduplication In daily work, there may be...

Linux concurrent execution is simple, just do it this way

Concurrency Functions time for i in `grep server ...

Analysis of Vue element background authentication process

Preface: Recently, I encountered a management sys...

Vue's guide to pitfalls using throttling functions

Preface In a common business scenario, we need to...

The difference between br and br/ in HTML

answer from stackflow: Simply <br> is suffic...

Example of how to configure nginx to implement SSL

Environmental Description Server system: Ubuntu 1...

A brief introduction to VUE uni-app core knowledge

Table of contents specification a. The page file ...

How to install setup.py program in linux

First execute the command: [root@mini61 setuptool...

How to fix some content in a fixed position when scrolling HTML page

This article mainly introduces how some content i...

A mobile adaptive web page effect solves the problem of small display page

For work needs, I need to make a mobile phone adap...

Detailed explanation of the life cycle of Angular components (Part 2)

Table of contents 1. View hook 1. Things to note ...

JavaScript realizes the generation and verification of random codes

The generation and verification of random codes i...

Detailed explanation of styles in uni-app

Table of contents Styles in uni-app Summarize Sty...