Home>Article>Operation and Maintenance> Docker summarizes and shares data volume technology

Docker summarizes and shares data volume technology

WBOY
WBOY forward
2022-01-14 18:44:58 1685browse

This article brings you the relevant knowledge of data volume technology summarized and shared by Docker. I hope it will be helpful to everyone.

Docker summarizes and shares data volume technology

Docker data volume technology

Docker summarizes and shares data volume technology

##What is a container data volume

Docker’s concept review

Package the application and environment into a mirror!

data? If the data is in the container, then if we delete the container, the data will be lost! Requirement: Data can be persisted

MySQL. If the container is deleted, the database will be deleted and run away ---> Requirement: 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 roll technology! Directory mounting, mount the directory in our container to Linux×!

Docker summarizes and shares data volume technology

Summary: persistence and synchronization operations of containers! Data sharing can also be achieved between containers!

Using data volumes

docker run -it -v 宿主机目录: 容器目录 -p 主机端口:容器端口 容器id

Practical exercises

inspect to view synchronization details

Docker summarizes and shares data volume technology

Create a file in the container and see if it is synchronized to the local folder

Docker summarizes and shares data volume technology

When the container is closed, modify the local file and see the modification Will the file be synchronized to the container?

Docker summarizes and shares data volume technology

Mysql actual combat

Command

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

Parameters

-d Background operation

-p Port mapping

-v Data volume mount: synchronize data

-e Set environment variables: The MySQL login password is set here

--name Container name

Docker summarizes and shares data volume technology##Test result: Connection successful

Docker summarizes and shares data volume technologyProblems encountered

The latest version of mysql used first: 8.0----When using navicat to connect, an inexplicable error is always reported

Solution: Switch to version 5.7, there is no problem

When we run the mirror mysql, we do not add -e MYSQL_ROOT_PASSWORD=123456 to set the password. The container will always be closed, even if it is restarted. It will be turned on

Add the -e MYSQL_ROOT_PASSWORD=123456 parameter to turn it on perfectly

Even if we delete the container, the data volume we mounted to the local is still not lost, which realizes the container data Persistence function

Named mount and anonymous mount

Anonymous mount-v Path within the container!

docker run -d --name nginx01 -v /etc/nginx nginx

Here we found that this is an anonymous mount. We only wrote the path inside the container after -v, but did not write the path outside the container.

View the situation of all volumes

docker volume ls

Docker summarizes and shares data volume technology

Named mount-v Name of the mounted volume: path within the container

docker run -d --name nginx01 -v 具名挂在名称:/etc/nginx nginx

Here we find that this is an anonymous mount. We write the path inside the container after -v, and there is no path outside the container, but there is a name

Docker summarizes and shares data volume technologyDocker’s content directory:/var/lib/docker

All volumes in the docker container, if no directory is specified, are in:/var/lib/docker/volume /xx/_data

We can easily find one of our volumes through named mounting, and the most commonly used method when using volumes is named mounting

Docker summarizes and shares data volume technology

Additional knowledgeHow to distinguish between named mounting and anonymous mounting, specify the path to mount

-v path inside the container # Anonymous mount

-v Volume name: path inside the container #Named mount

-v /path outside the container: path inside the container #Specify path mount

Extension

-v Path within the container: ro rw Change read and write permissions

ro readonly Read only

rw readwrite Readable and writable

一旦设置了这个容器权限,容器对我们挂载出来的内容就有限定了

docker run -d --name nginx01 -v 具名挂在名称:/etc/nginx:ro nginx docker run -d --name nginx01 -v 具名挂在名称:/etc/nginx:rw nginx

Ro 只要看到ro就说明这个路径只能通过宿主机来进行操作,容器内是无法操作的!

初识Dockerfile

dockerfile

dockerfile就是用来构建docker镜像文件的!命令脚本!先体验一下

通过这个脚本可以生成镜像,镜像是一层一层的,脚本是一个个的命令,每个命令都是一层!

写一个dockerfile

# 创立一个dockerfile文件,名字可以随便的取,最好叫做dockerfile # 文件中的内容 指令(大写) 参数 FROM centos VOLUME ["volme01","volume02"] CMD echo "--------end----------" CMD /bin/bash #这里每个指令就是镜像的一层

使用dockerfile生成镜像

docker build -f dockerfile文件位置 -t 镜像名称和版本 镜像生成的位置

Docker summarizes and shares data volume technology

进入镜像查看详情

Docker summarizes and shares data volume technology

查看卷的同步目录

docker ps -a

docker inspect 容器id

Docker summarizes and shares data volume technology

最后测试两个文件夹中是不是同步

同步成功

总结

使用dockerfile构建镜像的方式在我们未来的使用中非常的多,因为我们通常会构建自己的镜像

假设构建镜像时候没有挂载卷,要手动镜像挂载-v

数据卷容器

实际上即使保证的容器之间的数据共享的问题

Docker summarizes and shares data volume technology

数据卷容器实际上就是被拷贝数据的容器(A- volumes-from ->B,B是数据卷容器)

测试两个镜像之间同步

首先先开三个容器

创建docker01

Docker summarizes and shares data volume technology

创建docker02 --volumes-from docekr01

Docker summarizes and shares data volume technology

创建docker03 --volumes-from docekr02

Docker summarizes and shares data volume technology

查看docker01和docker03之间的数据共享

Docker summarizes and shares data volume technology

接着我们将docker02删除,查看docker01和docker03之间的数据共享

Docker summarizes and shares data volume technology

总结:

Docker summarizes and shares data volume technology

实现多个mysql之间的数据共享

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

总结:

容器之间配置信息的传递。数据卷容器的声明周期一直持续到没有容器使用位置

理解:容器之间只要是共享就会数据copy,即使有的容器被删除,数据依然存在,直到所有共享 的容器都删除,数据才会被彻底删除

但是一旦你持久化到了本地,这个时候,本地的数据是不会删除的。

推荐学习:《docker视频教程

The above is the detailed content of Docker summarizes and shares data volume technology. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete