Docker container data volume

Docker container data volume

1. What is a container volume

data? If the data is in the container, then we delete the container and the data will be lost! Requirements: data can be persisted

MySQL, the container is deleted, and the database is deleted! Requirements: MySQL data can be stored locally!

There can be a data sharing technology between containers! The data generated in the Docker container is synchronized to the local!

This is the volume technique! Mount the directory, mount the directory in our container to Linux!

Purpose: Persistence and synchronization of containers, data sharing between containers
Insert picture description here

Second, use the data volume

Method 1: directly use the command to mount

-v, --volume list                    Bind mount a volume

docker run -it -v 主机目录:容器内目录  -p 主机端口:容器内端口
➜ ~ docker run -it -v /home/ceshi:/home centos /bin/bash
#通过 docker inspect 容器id 查看

Insert picture description here
Synchronization of test files

1. Stop the container

2. Host modify files

3. Start the container

4. The data in the container is still synchronized

5. Delete the container, the host still has data

In the future, you only need to modify it locally, and the container will be automatically synchronized!

Three, actual combat: install mysql

MySQL data persistence commands

# 运行容器,需要做数据挂载 #安装启动mysql,需要配置密码的,这是要注意点!
# 参考官网hub 
docker search mysql

# 拉取
docker pull mysql:5.7

# 挂载
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 mysql5.7

-d 后台运行
-p 端口映射
-v 卷挂载
-e 环境配置 安装启动mysql需要配置密码
--name 容器名字

# 在本地测试创建一个数据库,查看一下我们映射的路径是否ok!

We deleted the container and found that the data volume we mounted to the local was still not lost, which realized the container data persistence function.

Four, named and anonymous mounting

# 匿名挂载
-v 容器内路径!
docker run -d -P --name nginx01 -v /etc/nginx nginx

# 查看所有的volume的情况
➜  ~ docker volume ls    
DRIVER              VOLUME NAME
local               9948f0d3d123c9d45c442ac7728cb85599c2657e50d
local            
# 这里发现,这种就是匿名挂载,我们在 -v只写了容器内的路径,没有写容器外的路劲!

# 具名挂载
➜  ~ docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
➜  ~ docker volume ls                  
DRIVER              VOLUME NAME
local               juming-nginx

# 通过 -v 卷名:容器内路径
# 查看一下这个卷

It is found that all the volumes in the docker container are under /var/lib/docker/volumes/xxxx/
_data if the directory is not specified . If the directory is specified, docker volume ls cannot be viewed.

# 三种挂载: 匿名挂载、具名挂载、指定路径挂载
-v 容器内路径			#匿名挂载
-v 卷名:容器内路径		#具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载 docker volume ls 是查看不到的

Expansion:

# 通过 -v 容器内路径: ro rw 改变读写权限
ro #readonly 只读
rw #readwrite 可读可写
docker run -d -P --name nginx05 -v juming:/etc/nginx:ro nginx
docker run -d -P --name nginx05 -v juming:/etc/nginx:rw nginx
# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作!

Five, first get to know Dockerfile

DockerFile uses to build docker image files

# 创建一个dockerfile文件,名字可以随便 建议Dockerfile
# 文件中的内容 指令(大写) 参数
FROM centos

VOLUME ["volume01","volume02"]

CMD echo "----end----"
CMD /bin/bash
#这里的每个命令,就是镜像的一层!

yum install vim # 编辑文件的,没有装一下

You can run this command after installation

Six, multiple container data sharing

Synchronize data with multiple MySQL!

Named container mount data volume
Insert picture description here

--volumes-from list              Mount volumes from the specified container(s)
# 测试,我们通过刚才启动的

Start docker01

docker run -it --name docker01 latteitcjz/centos:1.0 # 1.0必须写

The current ctrl+p+q does not stop and exit

Start docker02, docker03 in turn

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

docker02 inherits the volumes of docker01

It can be verified that adding a data under docker01 will also appear under docker02

Create docker03, also inherit docker01

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

Create files under volume01 of docker03, and also under volume01 of docker01

That is, data sharing between different containers can be achieved through --volumes-from

Delete docker01, the data is still there

docker rm -f 

As you can see, delete docker01 and enter docker02, the data is still in

Multiple mysql realize data sharing

➜  ~ docker run -d -p 3306: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
➜  ~ docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01  mysql:5.7
# 这个时候,可以实现两个容器数据同步!

in conclusion:

The transfer of configuration information between containers, the life cycle of the data volume container continues until there is no container location

But if it is persisted locally, even if all containers are deleted, the local data will not be deleted

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43803285/article/details/114457484