赞
踩
将应用和环境打包成一个镜像!如果数据都在容器中,那么我们容器删除,数据就会丢失!比如MySQL,容器删了,删库跑路!那就需要保证MySQL数据可以存储在本地!
容器之间可以有一个数据共享技术!Docker容器中产生的数据,同步到本地!这就是卷技术,目录的挂载,将我们容器内的目录挂载到 linux 目录上面!
总结: 容器的持久化和同步操作!容器间数据也是可以共享的!
方式一:直接命令挂载:docker run -it -v 主机目录:容器内目录 -p 主机端口:容器内端口
docker run -it -v /home/ceshi:/home centos /bin/bash
也可以用docker inspect 容器id
查看挂载信息
方式二:具名和匿名挂载
[root@VM-12-16-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 43c0eba85449 tomcat "catalina.sh run" 6 days ago Up 6 days 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 f594e620f359 nginx "/docker-entrypoint.…" 6 days ago Up 6 days 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01 4d241f98e11a centos "/bin/bash -c 'while…" 7 days ago Up 7 days nice_dewdney [root@VM-12-16-centos ~]# docker run -d -P --name nginxtest -v /etc/nginx nginx f467d3c292c0c2ad6567eda3a13eb2556eb7a9b6b3e50f7325a4352a32b9238f [root@VM-12-16-centos ~]# docker volume ls DRIVER VOLUME NAME local 5ed968a63ef344d47b5b95dc00210a8ff3fab1d2fd2308c4db0266db89deebe8 [root@VM-12-16-centos ~]# docker run -d -P --name nginxtest02 -v jumingnginx:/etc/nginx nginx 2f35d38119ab35da0886862fbc470d49db47403969a2a368fa3118583f98f846 [root@VM-12-16-centos ~]# docker volume ls DRIVER VOLUME NAME local 5ed968a63ef344d47b5b95dc00210a8ff3fab1d2fd2308c4db0266db89deebe8 local jumingnginx [root@VM-12-16-centos ~]# docker volume inspect jumingnginx [ { "CreatedAt": "2022-01-09T09:22:22+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/jumingnginx/_data", "Name": "jumingnginx", "Options": null, "Scope": "local" } ]
方式三:指定路径挂载
[root@VM-12-16-centos ~]# docker run -d -P --name nginxtest03 -v /home/nginx03:/etc/nginx.conf.d nginx
5ffc1114eaa0e8ebebdeb83fa1f86104e27fd464564d617e74b4d908f628ca9c
[root@VM-12-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ffc1114eaa0 nginx "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:49155->80/tcp, :::49155->80/tcp nginxtest03
2f35d38119ab nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginxtest02
f467d3c292c0 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginxtest
43c0eba85449 tomcat "catalina.sh run" 6 days ago Up 6 days 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01
f594e620f359 nginx "/docker-entrypoint.…" 6 days ago Up 6 days 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
4d241f98e11a centos "/bin/bash -c 'while…" 7 days ago Up 7 days nice_dewdney
[root@VM-12-16-centos ~]# docker volume ls
DRIVER VOLUME NAME
local 5ed968a63ef344d47b5b95dc00210a8ff3fab1d2fd2308c4db0266db89deebe8
local jumingnginx
所有的docker容器内的卷,没有指定目录的情况下都是在 /var/lib/docker/volumes/xxxx/_data
下,如果指定了目录,docker volume ls
是查看不到的。
三种挂载: 匿名挂载、具名挂载、指定路径挂载
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:#容器内路径 指定路径挂载 docker volume ls 是查看不到的
Dockerfile 就是用来构建docker镜像的构建文件!命令脚本!
#创建一个dockerfile文件,名字可以随便 建议Dockerfile
#文件中的内容 指令(大写) 参数
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
#这里的每个命令,就是镜像的一层!
生成镜像步骤:
1、编写镜像脚本
[root@VM-12-16-centos home]# mkdir docker-test-volume
[root@VM-12-16-centos home]# ls
ceshi docker-test-volume lighthouse nginx03
[root@VM-12-16-centos home]# cd docker-test-volume/
[root@VM-12-16-centos docker-test-volume]# vi dockerfile01 # 编写镜像脚本
[root@VM-12-16-centos docker-test-volume]# cat dockerfile01
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
2、构建镜像
[root@VM-12-16-centos docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile01 -t kuangshen/centos:1.0 . #不要忘记这个点 Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 5d0da3dc9764 Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in 9e122b475efb Removing intermediate container 9e122b475efb ---> c798ffb17302 Step 3/4 : CMD echo "----end----" ---> Running in 87baa832d5b1 Removing intermediate container 87baa832d5b1 ---> beea4615b125 Step 4/4 : CMD /bin/bash ---> Running in 507aea869366 Removing intermediate container 507aea869366 ---> 45bcf6025466 Successfully built 45bcf6025466 Successfully tagged kuangshen/centos:1.0 [root@VM-12-16-centos docker-test-volume]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE kuangshen/centos 1.0 45bcf6025466 28 seconds ago 231MB
3、运行这个镜像
[root@VM-12-16-centos docker-test-volume]# docker run -it 45bcf6025466 /bin/bash [root@42661e3916a9 /]# ls -l total 56 lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 360 Jan 9 01:45 dev drwxr-xr-x 1 root root 4096 Jan 9 01:45 etc drwxr-xr-x 2 root root 4096 Nov 3 2020 home lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64 drwx------ 2 root root 4096 Sep 15 14:17 lost+found drwxr-xr-x 2 root root 4096 Nov 3 2020 media drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt drwxr-xr-x 2 root root 4096 Nov 3 2020 opt dr-xr-xr-x 132 root root 0 Jan 9 01:45 proc dr-xr-x--- 2 root root 4096 Sep 15 14:17 root drwxr-xr-x 11 root root 4096 Sep 15 14:17 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Nov 3 2020 srv dr-xr-xr-x 13 root root 0 Jan 9 01:45 sys drwxrwxrwt 7 root root 4096 Sep 15 14:17 tmp drwxr-xr-x 12 root root 4096 Sep 15 14:17 usr drwxr-xr-x 20 root root 4096 Sep 15 14:17 var drwxr-xr-x 2 root root 4096 Jan 9 01:45 volume01 #这个目录就是我们生成镜像的时候自动挂载的,数据卷目录 drwxr-xr-x 2 root root 4096 Jan 9 01:45 volume02 [root@VM-12-16-centos docker-test-volume]# docker inspect 42661e3916a9 #可以查看一下
假设构建镜像时候没有挂载卷,要手动镜像挂载 -v 卷名:容器内路径!
多个容器之间实现数据共享
[root@VM-12-16-centos /]# docker run -it --name docker01 kuangshen/centos:1.0
[root@49ee0ba1633e /]# [root@VM-12-16-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49ee0ba1633e kuangshen/centos:1.0 "/bin/sh -c /bin/bash" 19 seconds ago Up 18 seconds docker01
5ffc1114eaa0 nginx "/docker-entrypoint.…" 45 minutes ago Up 45 minutes 0.0.0.0:49155->80/tcp, :::49155->80/tcp nginxtest03
2f35d38119ab nginx "/docker-entrypoint.…" 52 minutes ago Up 52 minutes 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginxtest02
f467d3c292c0 nginx "/docker-entrypoint.…" 54 minutes ago Up 54 minutes 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginxtest
43c0eba85449 tomcat "catalina.sh run" 6 days ago Up 6 days 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01
f594e620f359 nginx "/docker-entrypoint.…" 6 days ago Up 6 days 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
4d241f98e11a centos "/bin/bash -c 'while…" 7 days ago Up 7 days nice_dewdney
[root@VM-12-16-centos /]# docker run -it --name docker02 --volumes-from docker01 kuangshen/centos:1.0
测试:可以删除docker01,查看一下docker02和docker03是否可以访问这个文件
测试依旧可以访问
多个mysql实现数据共享
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
这个时候,可以实现两个容器数据同步!
结论:
容器之间的配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。但是一旦你持久化到了本地,这个时候,本地的数据是不会删除的!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。