数据卷(DockerFile添加)
DockerFile:
对镜像的一种源码级的描述文件
1、根目录下新建mydocker文件夹并进入
2、可在DockerFile中使用VOLUME指令来给镜像添加一个或多个数据卷
VOLUME["/dataVolumeContainer","/dataVolumeContainer2","/dataVolumeContainer3"]
说明:
出于可移植和分享的考虑,用-v主机目录:容器月录这种方法不能够直接在Dockerfile中实现。
由于宿主机目录是依赖于特定宿主机的,并不能够保证在所有的宿主机上都存在这样的特定目录。
3、File构建
[root@localhost /]# mkdir mydocker
[root@localhost /]# cd /mydocker/
[root@localhost mydocker]# vim Dockerfile
[root@localhost mydocker]# cat Dockerfile
# volume test
FROM centos
VOLUME ["/dataVolumeContainer1" ,"/dataVolumeContainer2"]
CMD echo "finished,==================success1"
CMD /bin/bash
4、build后生成镜像
最后有个 . 不要忘记了
[root@localhost mydocker]# docker build -f /mydocker/Dockerfile -t zhaoyoung/centos .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 75835a67d134
Step 2/4 : VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
---> Running in d475b69377ad
Removing intermediate container d475b69377ad
---> 8ee6e2a7e7ff
Step 3/4 : CMD echo "finished,==================success1"
---> Running in 37d6c8534064
Removing intermediate container 37d6c8534064
---> eed50be084b1
Step 4/4 : CMD /bin/bash
---> Running in 57583c68b3b0
Removing intermediate container 57583c68b3b0
---> 76582507c918
Successfully built 76582507c918
Successfully tagged zhaoyoung/centos:latest
成功生成镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhaoyoung/centos latest 76582507c918 About a minute ago 200MB
zhaoyoung/nodocstomcat 1.0 84498728984a 21 hours ago 463MB
centos latest 75835a67d134 37 hours ago 200MB
tomcat latest 41a54fe1f79d 4 weeks ago 463MB
hello-world latest 4ab4c602aa5e 4 weeks ago 1.84kB
5、run容器
容器终端
[root@localhost ~]# docker run -it zhaoyoung/centos
[root@b27b7b555902 /]# ll
total 12
-rw-r--r--. 1 root root 12030 Oct 6 19:15 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Oct 6 19:14 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Oct 11 07:06 dataVolumeContainer1
drwxr-xr-x. 2 root root 6 Oct 11 07:06 dataVolumeContainer2
drwxr-xr-x. 5 root root 360 Oct 11 07:06 dev
drwxr-xr-x. 1 root root 66 Oct 11 07:06 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Oct 6 19:14 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Oct 6 19:14 lib64 -> 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. 114 root root 0 Oct 11 07:06 proc
dr-xr-x---. 2 root root 114 Oct 6 19:15 root
drwxr-xr-x. 10 root root 130 Oct 6 19:15 run
lrwxrwxrwx. 1 root root 8 Oct 6 19:14 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Oct 9 09:14 sys
drwxrwxrwt. 7 root root 132 Oct 6 19:15 tmp
drwxr-xr-x. 13 root root 155 Oct 6 19:14 usr
drwxr-xr-x. 18 root root 238 Oct 6 19:14 var
容器终端
新建文件 container01.txt
[root@b27b7b555902 /]# cd dataVolumeContainer1/
[root@b27b7b555902 dataVolumeContainer1]# touch container01.txt
[root@b27b7b555902 dataVolumeContainer1]# ls
container01.txt
[root@b27b7b555902 dataVolumeContainer1]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 11 07:25 container01.txt
宿主机终端
可以看到容器内绑定的宿主机对应的文件夹,也就是可以进行相应的数据共享了,同上一篇博文
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b27b7b555902 zhaoyoung/centos "/bin/sh -c /bin/bash" 10 minutes ago Up 10 minutes vibrant_ganguly
[root@localhost ~]# docker inspect b27b7b555902
"Mounts": [
{
"Type": "volume",
"Name": "d46170d581a51392f47697441a103e3e05907106672c3c8db8bc7dbaf89241eb",
"Source": "/var/lib/docker/volumes/d46170d581a51392f47697441a103e3e05907106672c3c8db8bc7dbaf89241eb/_data",
"Destination": "/dataVolumeContainer1",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "8b2ab63cd912e61c3fac8627a6a55fd0a9eb929d6264b4b68451a07611550fe3",
"Source": "/var/lib/docker/volumes/8b2ab63cd912e61c3fac8627a6a55fd0a9eb929d6264b4b68451a07611550fe3/_data",
"Destination": "/dataVolumeContainer2",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
宿主机终端
[root@localhost ~]# cd /var/lib/docker/volumes/d46170d581a51392f47697441a103e3e05907106672c3c8db8bc7dbaf89241eb/_data
[root@localhost _data]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 11 15:25 container01.txt
宿主机终端
新建文件host.txt
[root@localhost _data]# touch host.txt
[root@localhost _data]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 11 15:25 container01.txt
-rw-r--r--. 1 root root 0 10月 11 15:28 host.txt
容器终端
[root@b27b7b555902 dataVolumeContainer1]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 11 07:25 container01.txt
-rw-r--r--. 1 root root 0 Oct 11 07:28 host.txt
如果出现以下错误
Docker挂载主机目录Docker访问出现cannot open directory .: Permission denied
解决办法:在挂载目录后多加一-个--privileged=true参数即可
如原来的命令:
[root@localhost ~]# docker run -it -v /myDataVolume:/dataVolumeContainer centos
修改为:
[root@localhost ~]# docker run -it -v /myDataVolume:/dataVolumeContainer --privileged=true centos