容器数据卷
数据卷就是容器的数据和主机相互映射,在容器添加的数据会同步到主机,主机加的数据也会同步到容器,相当于双向绑定
使用数据卷的方式
方式一 :直接使用命令来挂载
docker run -it -v 主机目录:容器内目录 #测试 [root@zhang ~]# docker run -it -v /home/test:/home centos /bin/bash [root@zhang ~]# cd /home [root@zhang home]# ls redis test test.java www [root@zhang home]# docker inspect aa02f734fe86 #使用上面的命令进入到容器查看以下内容 "Mounts": [ { "Type": "bind", "Source": "/home/test", #主机内的地址 "Destination": "/home", #容器内的地址 "Mode": "", "RW": true, "Propagation": "rprivate" } ],
#容器内部创建一个test01 [root@aa02f734fe86 /]# ls bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr [root@aa02f734fe86 /]# cd /home [root@aa02f734fe86 home]# ls [root@aa02f734fe86 home]# touch test01 [root@aa02f734fe86 home]# ls test01 #主机的目录 [root@zhang home]# cd /home [root@zhang home]# ls redis test test.java www [root@zhang home]# cd test [root@zhang test]# ls test01 #在本地主机上修改也会同步到主机
实战:安装MySQL
#官方启动代码 docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag # -d 后台运行 -p 端口映射 -v 卷挂载 -e 环境配置 --name 容器名字 [root@zhang home]# docker run -it -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 7ebbb2b4a4a4a96934b908c247e39c90750a6a76539c700339427a8fd5b20408 #mysql容器创建了一个test数据库 主机也同步到了本地 [root@zhang ~]# cd /home/mysql [root@zhang mysql]# ls conf data [root@zhang mysql]# cd data [root@zhang data]# ls 7ebbb2b4a4a4.err ca.pem ibdata1 mysql.ibd sys auto.cnf client-cert.pem ib_logfile0 performance_schema undo_001 binlog.000001 client-key.pem ib_logfile1 private_key.pem undo_002 binlog.000002 '#ib_16384_0.dblwr' ibtmp1 public_key.pem binlog.index '#ib_16384_1.dblwr' '#innodb_temp' server-cert.pem ca-key.pem ib_buffer_pool mysql server-key.pem [root@zhang data]# ls 7ebbb2b4a4a4.err ca.pem ibdata1 mysql.ibd sys auto.cnf client-cert.pem ib_logfile0 performance_schema test binlog.000001 client-key.pem ib_logfile1 private_key.pem undo_001 binlog.000002 '#ib_16384_0.dblwr' ibtmp1 public_key.pem undo_002 binlog.index '#ib_16384_1.dblwr' '#innodb_temp' server-cert.pem ca-key.pem ib_buffer_pool mysql server-key.pem
就算删掉容器本地的数据也不会删除
具名挂载和匿名挂载
#这种就算匿名挂载 docker run -it -d -P --name nginx01 -v /etc/nginx nginx [root@zhang home]# docker run -it -d -P --name nginx01 -v /etc/nginx nginx [root@zhang home]# docker volume ls DRIVER VOLUME NAME local a070a2c0fddbcd16171898d5bb23c9815366f28c90cc90944ab2935abfad758e local daf13cc2aed16e892b49512bd590ae0d916a2f42de2134e9d125b273cfaa7764 #具名挂载 [root@zhang home]# docker run -it -d -P --name nginx02 -v nginx_juming:/etc/nginx nginx 3509bcdc9fb47f1edfef1111754d910c3220b7f7b81e6bba339c056e156747af [root@zhang home]# docker volume ls DRIVER VOLUME NAME local a070a2c0fddbcd16171898d5bb23c9815366f28c90cc90944ab2935abfad758e local daf13cc2aed16e892b49512bd590ae0d916a2f42de2134e9d125b273cfaa7764 local nginx_juming #具名 查看具名挂载的目录 [root@zhang home]# docker volume inspect nginx_juming [ { "CreatedAt": "2021-05-11T01:04:01+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/nginx_juming/_data", #地址 "Name": "nginx_juming", "Options": null, "Scope": "local" } ]
所有的docker容器内的卷,没有指定目录的情况下都是在主机的/var/lib/docker/volumes/xxx/_data
[root@zhang home]# cd /var/lib/docker [root@zhang docker]# ls buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes [root@zhang docker]# cd volumes [root@zhang volumes]# ls a070a2c0fddbcd16171898d5bb23c9815366f28c90cc90944ab2935abfad758e metadata.db backingFsBlockDev nginx_juming daf13cc2aed16e892b49512bd590ae0d916a2f42de2134e9d125b273cfaa7764
#如何确定是具名挂载还是匿名挂载,还是指定路径挂载 -v 容器内路径 #匿名挂载 -v 卷名:容器内路径 #具名挂载 -v 宿主机路径:容器内路径 #指定路径挂载 宿主机路径以/开头
ro rw 改变读写权限
#通过 -v 容器内路径:ro rw 改变读写权限 #ro readonly 只读 这个只能通过宿主机改变 #rw readwrite 可读可写 默认 #一旦设置了这个容器权限,容器对我们挂载出来的内容就有限定了 docker run -it -d -P --name nginx02 -v nginx_juming:/etc/nginx:ro nginx docker run -it -d -P --name nginx02 -v nginx_juming:/etc/nginx:rw nginx
Dockerfile
Dockerfile就是用来构建docker镜像的构建文件!命令脚本
方式二:通过Dockerfile
#创建一个dockerfile文件,名字可以自定义 #文件的内容,指令 [root@zhang test]# cat dockerfile01 FROM centos VOLUME ["volume01","volume02"] CMD echo "-----end-----" CMD /bin/bash [root@zhang test]# docker build -f dockerfile01 -t zhang/centos . Sending build context to Docker daemon 14.85kB Step 1/4 : FROM centos ---> 300e315adb2f Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in eae7a27e89b8 Removing intermediate container eae7a27e89b8 ---> 3c0da2adcdbd Step 3/4 : CMD echo "-----end-----" ---> Running in f7551f3717f5 Removing intermediate container f7551f3717f5 ---> b02313d6d29b Step 4/4 : CMD /bin/bash ---> Running in dfea21172e13 Removing intermediate container dfea21172e13 ---> a4d2e2c302b2 Successfully built a4d2e2c302b2 Successfully tagged zhang/centos:latest #启动我们生成的镜像 [root@zhang test]# docker run -it a4d2e2c302b2 /bin/bash [root@2261a44dac3f /]# ls -l total 0 lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 360 May 11 03:44 dev drwxr-xr-x 1 root root 66 May 11 03:44 etc drwxr-xr-x 2 root root 6 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 6 Dec 4 17:37 lost+found drwxr-xr-x 2 root root 6 Nov 3 2020 media drwxr-xr-x 2 root root 6 Nov 3 2020 mnt drwxr-xr-x 2 root root 6 Nov 3 2020 opt dr-xr-xr-x 162 root root 0 May 11 03:44 proc dr-xr-x--- 2 root root 162 Dec 4 17:37 root drwxr-xr-x 11 root root 163 Dec 4 17:37 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 6 Nov 3 2020 srv dr-xr-xr-x 13 root root 0 May 11 03:44 sys drwxrwxrwt 7 root root 145 Dec 4 17:37 tmp drwxr-xr-x 12 root root 144 Dec 4 17:37 usr drwxr-xr-x 20 root root 262 Dec 4 17:37 var drwxr-xr-x 2 root root 6 May 11 03:44 volume01 #这两个目录就是刚刚生成镜像所挂载的 drwxr-xr-x 2 root root 6 May 11 03:44 volume02
使用匿名挂载,挂载的路径在主机的 /var/lib/docker/volumes/xxx/_data`
数据卷容器
docker run -it --name 子容器名 --volumes-from 父容器 镜像id
--volumes-from 后面可以跟容器id也可以跟容器名
docker run -it --name docker02 --volumes-from fccb6019146e 26a565d399d2
#开启第一个容器 [root@zhang home]# docker run -it 26a565d399d2 /bin/bash [root@fccb6019146e /]# [root@zhang home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fccb6019146e 26a565d399d2 "/bin/bash" 21 seconds ago Up 20 seconds nostalgic_lamport #进入第一个容器并在挂载路径创建test.py [root@zhang ~]# docker attach fccb6019146e [root@fccb6019146e /]# ls -l total 0 lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 360 May 11 04:03 dev drwxr-xr-x 1 root root 66 May 11 04:03 etc drwxr-xr-x 2 root root 6 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 6 Dec 4 17:37 lost+found drwxr-xr-x 2 root root 6 Nov 3 2020 media drwxr-xr-x 2 root root 6 Nov 3 2020 mnt drwxr-xr-x 2 root root 6 Nov 3 2020 opt dr-xr-xr-x 171 root root 0 May 11 04:03 proc dr-xr-x--- 2 root root 162 Dec 4 17:37 root drwxr-xr-x 11 root root 163 Dec 4 17:37 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 6 Nov 3 2020 srv dr-xr-xr-x 13 root root 0 May 11 04:03 sys drwxrwxrwt 7 root root 145 Dec 4 17:37 tmp drwxr-xr-x 12 root root 144 Dec 4 17:37 usr drwxr-xr-x 20 root root 262 Dec 4 17:37 var drwxr-xr-x 2 root root 6 May 11 04:03 volume01 drwxr-xr-x 2 root root 6 May 11 04:03 volume02 [root@fccb6019146e /]# cd volume01 [root@fccb6019146e volume01]# touch test.py #开启第二个容器可以看到第一个容器挂载目录的数据 [root@zhang home]# docker run -it --name docker02 --volumes-from fccb6019146e 26a565d399d2 [root@2bf50c130c1a /]# cd volume01 [root@2bf50c130c1a volume01]# ls test.py
学习参考狂神说java