---
title: "使用Dockerfile创建数据卷"
author: "秋风扫落叶"
date: "2019-07-04T09:01:42Z"
tags: []
categories: ["开源软件"]
slug: "dockerfile"
draft: false
lastmod: "2019-07-04T09:01:42Z"
id: 2442386
url: "https://loginleft.iteye.com/blog/2442386"
---

原文地址：[http://www.hxstrive.com/article/642.htm](http://www.hxstrive.com/article/642.htm)

 

在当前目录下面创建Dockerfile文件，然后在VOLUME指令来给镜像添加一个或多个数据卷。VOLUME语法如下：  
VOLUME\[“/dataVolumeContainer1”,  “/dataVolumeContainer2”,  “/dataVolumeContainer3”]  
说明：出于可移植和分享的考虑，用“-v主机目录:容器目录”这种方法不能够直接在Dockerfile中显示。由于宿主机目录是依赖于特定宿主机的，并不能够保证在所有的宿主机上都存在这样的特定目录。

 

实例：根据定义的Dockerfile去创建镜像。

(1) 在当前秒目录下面创建Dockerfile，内容如下：  
\# volume test  
FROM centos  
VOLUME \["/dataVolumeContainer1", "/dataVolumeContainer2"]  
CMD echo "finished......success"  
CMD /bin/bash  
如下图：  
\[root@localhost ~]# vim Dockerfile  
\[root@localhost ~]# cat Dockerfile  
\# volume test  
FROM centos  
VOLUME \["/dataVolumeContainer1", "/dataVolumeContainer2"]  
CMD echo "finished......success"  
CMD /bin/bash

(2) 根据上面创建好的Dockerfile文件构建一个新的镜像。执行“docker build -f ./Dockerfile -t mycentos .”命令去创建镜像。如下：  
\[root@localhost ~]# docker build -f ./Dockerfile -t mycentos .  
Sending build context to Docker daemon  28.67kB  
Step 1/4 : FROM centos  
 ---&gt; 9f38484d220f  
Step 2/4 : VOLUME \["/dataVolumeContainer1", "/dataVolumeContainer2"]  
 ---&gt; Using cache  
 ---&gt; 7d440f43e039  
Step 3/4 : CMD echo "finished......success"  
 ---&gt; Using cache  
 ---&gt; 4814b80b83b4  
Step 4/4 : CMD /bin/bash  
 ---&gt; Using cache  
 ---&gt; 1b896063f266  
Successfully built 1b896063f266  
Successfully tagged mycentos:latest  
我们可以使用“docker images”命令查看我们刚刚创建的镜像（镜像ID为1b896063f266）：  
\[root@localhost ~]# docker images  
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE  
mycentos            latest              1b896063f266        3 days ago          202MB  
tomcat              latest              5377fd8533c3        2 weeks ago         506MB  
centos              latest              9f38484d220f        3 months ago        202MB  
hello-world         latest              fce289e99eb9        6 months ago        1.84kB

(3) 使用“docker run -it 镜像”启动镜像。如下：  
\[root@localhost ~]# docker run -it mycentos  
\[root@7fe9af3b60b9 /]# ll  
total 12  
\-rw-r--r--.   1 root root 12082 Mar  5 17:36 anaconda-post.log  
lrwxrwxrwx.   1 root root     7 Mar  5 17:34 bin -&gt; usr/bin  
drwxr-xr-x.   2 root root     6 Jun 30 14:10 dataVolumeContainer1  
drwxr-xr-x.   2 root root     6 Jun 30 14:10 dataVolumeContainer2  
drwxr-xr-x.   5 root root   360 Jun 30 14:10 dev  
drwxr-xr-x.   1 root root    66 Jun 30 14:10 etc  
drwxr-xr-x.   2 root root     6 Apr 11  2018 home  
lrwxrwxrwx.   1 root root     7 Mar  5 17:34 lib -&gt; usr/lib  
lrwxrwxrwx.   1 root root     9 Mar  5 17:34 lib64 -&gt; usr/lib64  
drwxr-xr-x.   2 root root     6 Apr 11  2018 media  
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt  
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt  
dr-xr-xr-x. 125 root root     0 Jun 30 14:10 proc  
dr-xr-x---.   2 root root   114 Mar  5 17:36 root  
drwxr-xr-x.  11 root root   148 Mar  5 17:36 run  
lrwxrwxrwx.   1 root root     8 Mar  5 17:34 sbin -&gt; usr/sbin  
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv  
dr-xr-xr-x.  13 root root     0 Jun 24 13:42 sys  
drwxrwxrwt.   7 root root   132 Mar  5 17:36 tmp  
drwxr-xr-x.  13 root root   155 Mar  5 17:34 usr  
drwxr-xr-x.  18 root root   238 Mar  5 17:34 var  
上图中，我们可以看见“dataVolumeContainer1”和“dataVolumeContainer2”目录，这就是Dockerfile创建数据卷。

 

问题：我们怎样查看上面“dataVolumeContainer1”和“dataVolumeContainer2”两个目录，对应宿主机对应的目录呢？  
\[root@localhost ~]# docker ps  
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES  
7fe9af3b60b9        mycentos            "/bin/sh -c /bin/bash"   58 seconds ago      Up 57 seconds                           mystifying\_villani  
\[root@localhost ~]# docker inspect 7fe9af3b60b9  
[  
{  
    ...  
        "Mounts": [  
            {  
                "Type": "volume",  
                "Name": "0b92644824def28ba0d88ef0b8ed169631305a4a91502b64cda70153e116cac6",  
                "Source": "/var/lib/docker/volumes/0b92644824def28ba0d88ef0b8ed169631305a4a91502b64cda70153e116cac6/\_data",  
                "Destination": "/dataVolumeContainer2",  
                "Driver": "local",  
                "Mode": "",  
                "RW": true,  
                "Propagation": ""  
            },  
            {  
                "Type": "volume",  
                "Name": "931c1d7a053f23b3292893b35d46a6fe9fae789451fa1ff84e3aad1f255b1cc5",  
                "Source": "/var/lib/docker/volumes/931c1d7a053f23b3292893b35d46a6fe9fae789451fa1ff84e3aad1f255b1cc5/\_data",  
                "Destination": "/dataVolumeContainer1",  
                "Driver": "local",  
                "Mode": "",  
                "RW": true,  
                "Propagation": ""  
            }  
        ],  
        ...  
    }  
]  
\[root@localhost ~]#  
上面描述了“dataVolumeContainer1”和“dataVolumeContainer2”目录映射到宿主主机的目录。

 

注意：如果向数据卷写入数据抛出“cannot open directory .: Permission denied”错误。解决办法：在挂载的目录后多加一个“--privileged=true”参数即可。

 

privileged  
/'pr?v?l?d?d/  
adj. 享有特权的；有特别恩典的  
v. 给予…特权；免除（privilege的过去分词）