Container data volume

Time:2021-10-12

What is a container data volume

Review of docker’s concept
Package the application and environment into an image!
data If the data is in the container, the data will be lost if we delete the container! Requirement: data can be persisted
For example, MySQL: the container is deleted and the data is lost! Requirement: MySQL data can be stored locally!

There can be a data sharing technology between containers! Synchronize the data generated in docker container to the local!

This is volume technology! Mount the directory in our container on Linux!

Container data volume

To sum up: container persistence and synchronization! Data can also be shared between containers

Using data volumes

Method 1: directly use the command to mount – V

$docker run - it -- name = "container name" -v directory in host: directory in container - P host port: port image name in container

#Mount the / home directory in the container to ~ / file / CentOS of the host_ Under the test directory, internal and external files will be automatically synchronized with each other
$ docker run -it -v ~/File/centos_test:/home centos /bin/bash

Take a look at the container information

$docker inspect container ID

Container data volume

Test file synchronization steps and effects
Container data volume

Test again
1. Stop the container
2. Modify the file on the host
3. Start the container
4. The data in the container is still synchronized
Container data volume

Benefits: we only need to modify locally later, and the container will be synchronized automatically.

Actual test: install MySQL

Problem: MySQL persistence problem!

#Get image
$ docker pull mysql

#To run the container, you need to mount the data! You need to configure the password to install and start mysql. This is a point to pay attention to!
#Official test: docker run -- name some MySQL - e MySQL_ ROOT_ PASSWORD=my-secret-pw -d mysql:tag

#Start
#- D background operation
#- P port mapping
#- V volume mount
#- E environment configuration
#-- name container name
$ docker run -d -p 3310:3306 -v ~/File/docker_mysql/conf:/etc/mysql/conf.d -v ~/File/docker_mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name=mysql01 mysql

After it is started, the external uses Navicat to test. Navicat is connected to the 3310 of the server, the 3310 of the server and the 3306 mapping of the container. At this time, we can connect!

Container data volume
Create a database locally

Container data volume

Container data volume

This is a test. Delete the MySQL container and find that the data volume mounted locally is still saved locally.

Container data volume

Named mount and anonymous mount

  • Anonymous mount
#- V path in container
$ docker run -d -p --name nginx -v /etc/nginx nginx
#View all volumes
$ docker volume ls
DRIVER              VOLUME NAME
local               a7ea06ddbf53d1e879f304470b4775abdf9e455d2fba0a589588a6f50ba22137
#It is found here that this is anonymous mount. When we - V, we only write the path inside the container, not the path outside the container!
  • Named mount
#Via -v volume name: path within container
$ docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx

$ docker volume ls

$docker volume inspect volume name

Container data volume

All volumes in the docker container are stored in the/var/lib/docker/volumes/xxxx/_data

However, there is a problem. There is no such directory in the Mac. You can use itscreen Library/Containers/com.docker.docker/Data/vms/0/tty(the second half of the path may change) enter the Linux kit virtual machine. Through other linux commands, you can see that there is a customized Linux system running background processes such as dockerd and containerd. Note that it can be usedCtrl+A+DThe shortcut key exits the screen window.
aboutscreenFor more specific orders, see me aboutscreenMy blog.

Container data volume

After entering, you can see that the actual files are saved in the Linux kit virtual machine. G.docker runs the Linux virtual machine based on specific virtualization technology on Mac OS or windows, and does not share the host kernel.

Container data volume

We can easily find one of our volumes through named mount, which is used in most casesNamed mount

#How to determine whether a named mount or an anonymous mount or a specified path mount
-V path # anonymous mounting in container
-V volume name: path # named mount in container
-V / host path: the path # specified in the container is mounted
  • expand

Add afterroOrrw, change read and write permissions

#Once the container permission is set, the container limits the content we mount!
$ docker run -d -P --name nginx02 -v jumping-nginx:/etc/nignx:ro nginx

$ docker run -d -P --name nginx02 -v jumping-nginx:/etc/nignx:rw nginx

#Change read and write permissions through - V path in container: RO RW
#Ro readonly is read-only. It can only be operated by the host. It cannot be changed in the container
#RW readwrite

First acquaintance with dockerfile

Dockerfile is the build file used to build the docker image! Command script.
Images can be generated through this script. Images are layer by layer. Scripts are commands one by one, and each command is a layer.

Create a dockerfile
Contents of the file, instruction (uppercase) parameters

FROM centos

VOLUME ["volume01","volume02"]

CMD echo "-----end-----"
CMD /bin/bash

#Each command here is a layer of mirroring
$ docker build -f ~/File/docker_test_volume/dockerfile1 -t hudu/centos:1.0 .

Container data volume

Start your own mirror test

Container data volume

This volume must have a synchronized directory with the outside!
1. View the specific information of the container through the command
2. See the specific external directory of the mounted volume

Container data volume

Container data volume

Container data volume

Container data volume
We will use this method a lot in the future, because we usually build our own image!
Assuming that the volume is not mounted when the image is built, the path in the mounted -v container should be manually mirrored

Data volume container

Data synchronization between two MySQL

Container data volume

Start three containers and start through our own write image just now
Ctrl+P+Q, exit without closing the container

$ docker run -it --name docker01 hudu/centos:1.0 /bin/bash

$ docker run -it --name docker02 --volumes-from docker01 hudu/centos:1.0

$ docker run -it --name docker03 --volumes-from docker01 hudu/centos:1.0

Container data volume

Container data volume

Container data volume

Test: delete docker01 and check that docker02 and docker03 still have this file.

adoptDocker inspect container IDIt can be found that they are all mapped to the same directory of the host.
Container data volume

Data sharing with multiple MySQL

$ run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name=mysql01 mysql

$ run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name=mysql02 --volumes-from mysql01 mysql

Realize data sharing and synchronization between two containers

In conclusion, information transfer can be configured between containers, and the life cycle of data volume containers continues until no containers are used.

However, once it is persisted locally, the local data will not be deleted.

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]