dockerfile就是用来构建docker镜像的构建文件
写入命令脚本
通过这个脚本可以生成镜像,镜像是一层一层的,脚本一个个的命令,每个命令都是一层
[root@iZmu0fa4hv0jwzZ home]# mkdir docker-test-volume [root@iZmu0fa4hv0jwzZ home]# ls admin ceshi docker-test-volume mysql [root@iZmu0fa4hv0jwzZ home]# cd docker-test-volume/ [root@iZmu0fa4hv0jwzZ docker-test-volume]# vim dockerfile1 FROM centos VOLUME ["/volume01","/volume02"] CMD echo "---end---" CMD /bin/bash [root@iZmu0fa4hv0jwzZ docker-test-volume]# cat dockerfile1 FROM centos VOLUME ["/volume01","/volume02"] CMD echo "---end---" CMD /bin/bash
通过文件构建镜像
[root@iZmu0fa4hv0jwzZ docker-test-volume]# docker build -f ./dockerfile1 -t test/centos:1.0 . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 300e315adb2f Step 2/4 : VOLUME ["/volume01","/volume02"] ---> Running in 02359ee32d02 Removing intermediate container 02359ee32d02 ---> f41d0aec0515 Step 3/4 : CMD echo "---end---" ---> Running in 549ca07d5fb2 Removing intermediate container 549ca07d5fb2 ---> a72f1ec90a37 Step 4/4 : CMD /bin/bash ---> Running in 40583b2eae36 Removing intermediate container 40583b2eae36 ---> b3b4d08ec5a9 Successfully built b3b4d08ec5a9 Successfully tagged test/centos:1.0
[root@iZmu0fa4hv0jwzZ docker-test-volume]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/centos 1.0 b3b4d08ec5a9 4 minutes ago 209MB tomcat02 1.0 c93f6d59c630 15 hours ago 672MB tomcat 9.0 5505f7218e4d 4 days ago 667MB nginx latest d1a364dc548d 3 weeks ago 133MB mysql 5.7 2c9028880e58 5 weeks ago 447MB centos latest 300e315adb2f 6 months ago 209MB
启动自己写的容器
[root@iZmu0fa4hv0jwzZ docker-test-volume]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/centos 1.0 11bfc295e9b3 15 seconds ago 209MB tomcat02 1.0 c93f6d59c630 15 hours ago 672MB tomcat 9.0 5505f7218e4d 4 days ago 667MB nginx latest d1a364dc548d 3 weeks ago 133MB mysql 5.7 2c9028880e58 5 weeks ago 447MB centos latest 300e315adb2f 6 months ago 209MB [root@iZmu0fa4hv0jwzZ docker-test-volume]# docker run -it 11bfc295e9b3 /bin/bash [root@f8871c5b6c95 /]#
启动成功,查看目录
[root@f8871c5b6c95 /]# ls -l total 0 lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 360 Jun 21 04:29 dev drwxr-xr-x 1 root root 66 Jun 21 04:29 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 2020 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 116 root root 0 Jun 21 04:29 proc dr-xr-x--- 2 root root 162 Dec 4 2020 root drwxr-xr-x 11 root root 163 Dec 4 2020 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 Jun 21 04:29 sys drwxrwxrwt 7 root root 145 Dec 4 2020 tmp drwxr-xr-x 12 root root 144 Dec 4 2020 usr drwxr-xr-x 20 root root 262 Dec 4 2020 var drwxr-xr-x 2 root root 6 Jun 21 04:29 volume01 drwxr-xr-x 2 root root 6 Jun 21 04:29 volume02
可以发现我们自动挂载的数据卷目录,这个卷和外部一定有一个同步的目录
在容器内创建一个container.txt文件
[root@8dded46b27dd /]# cd volume01 [root@8dded46b27dd volume01]# ls [root@8dded46b27dd volume01]# touch container.txt [root@8dded46b27dd volume01]# ls container.txt
在主机中查看镜像挂载目录
[root@iZmu0fa4hv0jwzZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8dded46b27dd 11bfc295e9b3 "/bin/bash" About a minute ago Up About a minute frosty_kilby [root@iZmu0fa4hv0jwzZ ~]# docker inspect 8dded46b27dd
可以看见mounts中有创建的数据卷
"Mounts": [ { "Type": "volume", "Name": "1596e8f6dc06936b2b731603def387e1a64c45f609b61a8658472ad580fbf7c7", "Source": "/var/lib/docker/volumes/1596e8f6dc06936b2b731603def387e1a64c45f609b61a8658472ad580fbf7c7/_data", "Destination": "/volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "96975303ff0d506b1828cb0c1490423e2fc0934f6b73ba0bb30df01b2b28d4f4", "Source": "/var/lib/docker/volumes/96975303ff0d506b1828cb0c1490423e2fc0934f6b73ba0bb30df01b2b28d4f4/_data", "Destination": "/volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],
转到主机的同步目录
[root@iZmu0fa4hv0jwzZ ~]# cd /var/lib/docker/volumes/1596e8f6dc06936b2b731603def387e1a64c45f609b61a8658472ad580fbf7c7/_data [root@iZmu0fa4hv0jwzZ _data]# ls container.txt
可以发现主机中存在容器中创建的文件