Dockerfile数据卷挂载
思诚代码块 人气:0大家好,我们今天分享使用Dockerfile来实现数据卷的挂载
首先呢,要了解一下什么是Dockerfile
Dockerfile就是 一个可以用来构建镜像的文件,可以理解为一个脚本文件
可以通过这个脚本文件自定义生成一个我们想要的镜像,镜像都是一层一层的,里面的每一条命令就是一层
创建一个目录
root@localhost home]# mkdir docker-vl2
编辑这样这样的一个文件
root@localhost docker-vl2]# vim dockerfile01
FROM centos VOLUME ["volume01","volume02"] CMD echo "------end-----" CMD /bin/bash ~ ~ ~ ~
保存退出:
构建成新的镜像,注意看shuaige/centos:2.0有一个小点
[root@localhost docker-vl2]# docker build -f dockerfile01 -t shuaige/centos:2.0 . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 5d0da3dc9764 Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in 96445950bbef Removing intermediate container 96445950bbef ---> ffcf0eb8973b Step 3/4 : CMD echo "------end-----" ---> Running in a4fa6fce633b Removing intermediate container a4fa6fce633b ---> 5416582373e0 Step 4/4 : CMD /bin/bash ---> Running in 7d7df79e0b36 Removing intermediate container 7d7df79e0b36 ---> d8e0f44b8da3 Successfully built d8e0f44b8da3 Successfully tagged shuaige/centos:2.0
看这个过程,一共有4步,对应着我们刚刚写的dockerfile 文件
也是一层一层的
查看镜像:
这样,我们的这个dockerfile 文件就构建成功了
进入我们自己写的容器
[root@localhost docker-vl2]# docker run -it d8e0f44b8da3 /bin/bash [root@b36bc0c1e4a9 /]#
查看容器内部的文件结构:
[root@b36bc0c1e4a9 /]# ls -l total 0 lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x. 5 root root 360 Mar 8 14:09 dev drwxr-xr-x. 1 root root 66 Mar 8 14:09 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 Sep 15 14:17 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. 265 root root 0 Mar 8 14:09 proc dr-xr-x---. 2 root root 162 Sep 15 14:17 root drwxr-xr-x. 11 root root 163 Sep 15 14:17 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 Mar 4 07:14 sys drwxrwxrwt. 7 root root 171 Sep 15 14:17 tmp drwxr-xr-x. 12 root root 144 Sep 15 14:17 usr drwxr-xr-x. 20 root root 262 Sep 15 14:17 var drwxr-xr-x. 2 root root 6 Mar 8 14:09 volume01 drwxr-xr-x. 2 root root 6 Mar 8 14:09 volume02
可以看见这俩行:(这就是我们刚才使用dockerfile 文件做的数据卷挂载)
drwxr-xr-x. 2 root root 6 Mar 8 14:09 volume01 drwxr-xr-x. 2 root root 6 Mar 8 14:09 volume02
看dockerfile 文件的内容:
FROM centos VOLUME ["volume01","volume02"] CMD echo "------end-----" CMD /bin/bash ~
可以看出,它是匿名挂载:
但凡是挂载,就是一定与外部有一个对应的目录
查看正在运行的容器
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b36bc0c1e4a9 d8e0f44b8da3 "/bin/bash" 14 minutes ago Up 14 minutes compassionate_brown 6590da464b6e nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx03 d8e9b9084cf8 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx02 6d6fa6a4c6d7 5d0da3dc9764 "/bin/bash" 33 hours ago Up 33 hours nice_panini
看容器的系统信息
[root@localhost ~]# docker inspect b36bc0c1e4a9
这就是我们刚才数据卷挂载的位置
"Mounts": [ { "Type": "volume", "Name": "155842f6ab8581ad5d04fa25d6a1979647f481aad9532306834e577f88daa2a1", "Source": "/var/lib/docker/volumes/155842f6ab8581ad5d04fa25d6a1979647f481aad9532306834e577f88daa2a1/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "1c6664c1368e4a11d36fd0b629c5e7b7d40d1d2f88322f9188383e92e52aa2f1", "Source": "/var/lib/docker/volumes/1c6664c1368e4a11d36fd0b629c5e7b7d40d1d2f88322f9188383e92e52aa2f1/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],
在容器内部新建一个文件
[root@b36bc0c1e4a9 /]# cd volume01 [root@b36bc0c1e4a9 volume01]# touch hsud.txt [root@b36bc0c1e4a9 volume01]#
看系统信息:
[root@localhost _data]# docker inspect b36bc0c1e4a9
在Linux主机上看容器中看
[root@localhost ~]# cd /var/lib/docker/volumes/155842f6ab8581ad5d04fa25d6a1979647f481aad9532306834e577f88daa2a1/_data [root@localhost _data]# ls hsud.txt [root@localhost _data]#
好了,这样我们就做好了,有关于使用Dockerfile实现数据卷的挂载就到这里了,谢谢大家了
加载全部内容