docker学习(10)数据卷挂载之使用dockerfile

本文介绍了如何使用Dockerfile进行数据卷挂载。通过编写Dockerfile脚本,创建的镜像中包含了指定的挂载目录volume1和volume2。在容器内部创建文件后,外部路径也显示挂载成功,实现了数据持久化。
摘要由CSDN通过智能技术生成

dockerfile就是用来构建docker镜像的构建文件,就是一个命令脚本。由于镜像是一层一层的,脚本也是一个个的命令,代表一层一层的镜像。

dockerfile也可以实现挂载,示例如下:

我们在/home/docker-test-volume的dockerfile1里写入如下脚本

FROM centos

VOLUME ["volume01","volume02"]

CMD echo "------end-----"
CMD /bin/bash
[root@wangstudy docker-test-volume]# docker build -f dockerfile1 -t wang/centos .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 470671670cac
Step 2/4 : VOLUME ["volume01","volume02"]
 ---> Running in afcfd0ed0d77
Removing intermediate container afcfd0ed0d77
 ---> 4936b8e8924a
Step 3/4 : CMD echo "------end-----"
 ---> Running in 5cde4d48d667
Removing intermediate container 5cde4d48d667
 ---> fe92d9708f50
Step 4/4 : CMD /bin/bash
 ---> Running in a28609ac50e3
Removing intermediate container a28609ac50e3
 ---> 531f66453bed
Successfully built 531f66453bed
Successfully tagged wang/centos:latest
[root@wangstudy docker-test-volume]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
wang/centos           latest              531f66453bed        About an hour ago   237MB
tomcat02              1.0                 0b2dd38750b5        20 hours ago        652MB
portainer/portainer   latest              1ec116aed60c        5 days ago          79.1MB
redis                 latest              36304d3b4540        5 days ago          104MB
mysql                 5.7                 a4fdfd462add        13 days ago         448MB
tomcat                latest              1b6b1fe7261e        2 weeks ago         647MB
nginx                 latest              9beeba249f3e        2 weeks ago         127MB
centos                latest              470671670cac        4 months ago        237MB

这样我们的镜像就创建成功了,可以看到我们刚指定的挂载目录volume1和volume2已经存在了

[root@wangstudy docker-test-volume]# docker run -it 531f66453bed
[root@a0b6eb1b256a /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 May 11  2019 bin -> usr/bin
drwxr-xr-x   5 root root  360 Jun  3 10:07 dev
drwxr-xr-x   1 root root 4096 Jun  3 10:07 etc
drwxr-xr-x   2 root root 4096 May 11  2019 home
lrwxrwxrwx   1 root root    7 May 11  2019 lib -> usr/lib
lrwxrwxrwx   1 root root    9 May 11  2019 lib64 -> usr/lib64
drwx------   2 root root 4096 Jan 13 21:48 lost+found
drwxr-xr-x   2 root root 4096 May 11  2019 media
drwxr-xr-x   2 root root 4096 May 11  2019 mnt
drwxr-xr-x   2 root root 4096 May 11  2019 opt
dr-xr-xr-x 113 root root    0 Jun  3 10:07 proc
dr-xr-x---   2 root root 4096 Jan 13 21:49 root
drwxr-xr-x  11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx   1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 May 11  2019 srv
dr-xr-xr-x  13 root root    0 May 30 08:43 sys
drwxrwxrwt   7 root root 4096 Jan 13 21:49 tmp
drwxr-xr-x  12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x  20 root root 4096 Jan 13 21:49 var
drwxr-xr-x   2 root root 4096 Jun  3 10:07 volume01
drwxr-xr-x   2 root root 4096 Jun  3 10:07 volume02

我们查看镜像详细信息,可以发现如下挂载目录

  "Mounts": [
            {
                "Type": "volume",
                "Name": "1c0062d23655e442597411a8b8a3337c86a6a7e59b4b8dca73b5e6804f959037",
                "Source": "/var/lib/docker/volumes/1c0062d23655e442597411a8b8a3337c86a6a7e59b4b8dca73b5e6804f959037/_data",
                "Destination": "volume01",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "73b59c5be43bc6ab8774e3625fa193db024f4b563f767ffc0474505cfef54d22",
                "Source": "/var/lib/docker/volumes/73b59c5be43bc6ab8774e3625fa193db024f4b563f767ffc0474505cfef54d22/_data",
                "Destination": "volume02",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

我们在volume01里面添加一个container.txt文件。

[root@a0b6eb1b256a /]# cd volume01
[root@a0b6eb1b256a volume01]# touch container.txt
[root@a0b6eb1b256a volume01]# ls
container.txt

容器外对应路径查看,发现挂载成功

[root@wangstudy docker-test-volume]# cd /var/lib/docker/volumes/1c0062d23655e442597411a8b8a3337c86a6a7e59b4b8dca73b5e6804f959037/_data
[root@wangstudy _data]# ls
container.txt

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值