17-25-docker-panel-mount-data volume

17-docker-visualization-mounting and data volumes:

Visual portainer:

docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

Docker image principle

1. What is it?

A mirror image is a lightweight, executable independent software package used to package the software operating environment and software developed based on the operating environment. It contains everything needed to run a certain software, including code, runtime, library, environment Variables and configuration files, etc.


UnionFs (union file system): union file system (UnionFs) is a layered, lightweight and high-performance file system, which supports the modification of the file system as a submission to superimpose layer by layer, while different The directory is mounted under the same virtual file.

The Union file system is the basis of the Docker image, and the image can be inherited through layers. Based on the basic image, various specific application images can be made.

Features: Load multiple file systems at the same time, but from the outside, only one file system can be seen. Joint loading will superimpose the file systems of all layers, so that the final file system will contain all the underlying files and directories.

2.Docker image loading principle

Docker's image is actually composed of layer-by-layer file systems. This layered file system UnionFs

Bootfs (boot-file system) mainly includes bootloader and kernel. The bootloader is mainly to bootload the kernel. When Linux starts, it will load the bootfs file system. The bottom layer of the Docker image is the bootfs. This layer is similar to our typical Linux/unix system. It is the same, including the boot loader and the kernel. When the boot loading is complete, the entire kernel can be in the memory. At this time, the right to use the memory has been transferred from the bootfs to the kernel, and the system will also uninstall the bootfs at this time.

Rootfs (root-file system), on top of bootfs, contains standard directories and files such as /dev, /proc, /bin, /etc in a typical Linux system. rootfs is the distribution version of various operating systems, such as Ubuntu, Centos, etc.

For a streamlined OS, the rootfs can be very small, and only needs to include the most basic commands, tools and programs. Because the underlying layer directly uses the host’s kernel, you only need to provide the rootfs, so it can be seen that for unused For Linux distributions, the bootfs is basically the same, but the rootfs is different, so different distributions can share the same bootfs.

⒊Why does Docker adopt a layered structure?

-Share resource

Multiple images are built from the same parent image, so the host only needs to save one parent image on the disk, and at the same time, it only needs to load one parent image in memory to serve all containers, and each image of the image Layers can all be shared.

⒋Docker image features

①Docker image read-only

②When the image instance is a container, only the outermost layer is writable.

Source: https://www.cnblogs.com/fanqisoft/

layered understanding

All Docker images start from a basic image layer. When modifications or new content are added, a new image layer will be created on top of the current image layer.
To give a simple example, if a new image is created based on Ubuntu Linux 16.04, this is the first layer of the new image; if you add a Python package to the image,
A second image layer will be created on top of the base image layer; if you continue to add a security patch, a third image layer will be created.
The image currently contains 3 image layers, as shown in the figure below (this is just a very simple example for demonstration).

It is important to understand that while adding additional image layers, the image always remains a combination of all current images. A simple example is given in the figure below, each image
The layer contains 3 files, while the mirror contains 6 files from the two mirror layers.

The mirror layer in the above picture is slightly different from the previous picture, the main purpose is to facilitate the display of files.
The figure below shows - a slightly complicated three-layer image. From the outside, the entire image has only 6 files. This is because file 7 in the top layer is an updated version of file 5.

In this case, the files in the upper mirror layer overwrite the files in the bottom mirror layer. This causes the updated version of the file to be added to the image as a new image layer.
Docker implements the image layer stack through the storage engine (the new version adopts the snapshot mechanism), and ensures that multiple image layers are displayed as a unified file system.
The storage engines available on Linux. are AUFS. Overlay2. Device Mapper. Btrfs and ZFS. As the name implies, each storage engine is based on the corresponding
file system or block device technology, and each storage engine has its unique performance characteristics.
Docker only supports windows fiter-a storage engine on Windows, which implements layering and CoW[1] based on the NTFS file system.
The figure below shows the same three-layer mirror as the system displays. All mirror layers are stacked and merged to provide a unified view to the outside world.

Docker images are all read-only, and when the container starts, a new writable layer is loaded on top of the image!
This layer is what we usually call the container layer, and everything under the container is called the mirror layer!

commit mirror

docker commit Submit the container as a new copy

docker commit -m="commit information" -a="author" container id target image name: [TAG]


#Start a default tomcat
docker run -it -p 8080:8080 tomcat
#By default, tomcat has no webapps and requires cp. Officially no.
docker exec -it container id /bin/bash
#I cp into the basic files by myself, which is equivalent to modifying them myself.
cp -r webapps.dist/* webapps/

commit,pass commit Commit as a new mirror. Mirror image modified by myself. A layer is added and packaged into a new image. like snapshot
[root@hadoop106 ~]# docker commit -a="zh" -m="add webapps app" 01c4581c446e tomcat02:1.0
[root@hadoop106 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
tomcat02              1.0       3145febb79b9   6 seconds ago   672MB
nginx                 latest    4cdc5dd7eaad   11 days ago     133MB
tomcat                9.0       36ef696ea43d   2 weeks ago     667MB
tomcat                latest    36ef696ea43d   2 weeks ago     667MB
mysql                 latest    5c62e459e087   3 weeks ago     556MB
portainer/portainer   latest    580c0e4e98b0   4 months ago    79.1MB
hello-world           latest    d1165f221234   4 months ago    13.3kB
centos                latest    300e315adb2f   7 months ago    209MB
[root@hadoop106 ~]#

container data volume

Data, if in the container, container deletion will lose data. Data should be persisted. There is a data sharing technology between containers. The data generated in the Docker container is synchronized locally. Directory mount, mount the directory in the container to linux.

The persistence and synchronization operations of the container, the container can also share data.

Use data volume:

Method 1: Directly use the command to mount -v (directories on both sides will be synchronized)

docker run -it -v host directory:container directory


the container's home The directory is mounted to the local machine/home/ceshi/Down
[root@hadoop106 home]# docker run -it -v /home/ceshi:/home centos /bin/bash

Check whether the mount is successful docker inspect bad2c7c52c42

[root@hadoop106 ceshi]# docker ps
CONTAINER ID   IMAGE                 COMMAND        CREATED         STATUS         PORTS                                       NAMES
bad2c7c52c42   centos                "/bin/bash"    3 minutes ago   Up 3 minutes                                               kind_spence
1788da34dc7d   portainer/portainer   "/portainer"   16 hours ago    Up 16 hours>9000/tcp, :::8088->9000/tcp   flamboyant_shamir
[root@hadoop106 ceshi]# docker inspect bad2c7c52c42

Test: install mysql

? ? ? mysql data persistence problem.

1,start up
docker pull mysql:5.7
2,start mount, install mysql Need configuration password
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=123456 --name mysql01 mysql:5.7
3,use other mysql Tool connection test, the database created locally will be synchronized to the path mapped by the container and the path of the server. ok
4,Delete the container, the local data will not be lost
docker rm -f mysql01
Named mount, anonymous mount:
#mount anonymously
-v container path -P Randomly mapped ports
docker run -d -P --name nginx01 -v /etc/nginx nginx
#View the status of all volumes. Only the path of the container is written, not the local path.
docker volume ls

docker run -d -P --name nginx03 -v juming-nginx:/etc/nginx nginx

View the location of the volume docker volume inspect named mount name

All volumes in the docker container are in /var/ib/docker/voiumes/xxxx/ if no directory is specified

#Change the read and write permissions through the path in the -V container, ro rw
readonly #read only
readwrite Single readable and writable
#Once the container permissions are set, the container has restrictions on the content we mount!
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -V juming-nginx:/etc/nginx:rw nginx
# As long as ro sees ro, it means that this path can only be operated through the host machine, and it cannot be operated inside the container!

Method 2 (DockerFile):

dockerfile, the build file used to build the docker image! command script! Start execution.

The image is produced by script, the image is layer by layer, the script is a command, and each command is a layer.

#Create a dockerfile with a random name, Dockerfile is recommended
#write in file

[root@hadoop106 docker-test-volume]# vim dockerfile01
[root@hadoop106 docker-test-volume]# cat dockerfile01
FORM centos

VOLUME ["volume01","volume02"]

CMD echo "==========end========"
CMD /bin/bash
#Each command, and mirrored layer

#create mirror
[root@hadoop106 docker-test-volume]# docker build -f dockerfile01 -t zh-docker-test/centos:1.0 .

start your own container

docker run -it imageid /bin/bash

If the volume is not mounted when building the image, you need to manually mount the image at startup -v volume name: path inside the container

Data volume container:

Multiple mysql data synchronization! --volumes-from is equivalent to subclasses and parentclasses


1. docker run -it --name docker01 image: version

#bind to docker01

2,docker run -it --name docker02 --volumes-from docker01

#Delete the parent container, the data of other child containers is still there. backup.

mysql data sharing test

[root@hadoop106 ~]#docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
[root@hadoop106 ~]#docker run -d -p 3310:3306  -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from102 mysql:5.7

mysql data sharing test

[root@hadoop106 ~]#docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
[root@hadoop106 ~]#docker run -d -p 3310:3306  -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from102 mysql:5.7

Tags: Linux Operation & Maintenance Java Docker

Posted by BizBoy on Sun, 20 Nov 2022 04:27:40 +0530