赞
踩
Dockerfile就是用来构建docker镜像的命令脚本文件,通过这个脚本就可以生成镜像,每一个命令都是一层;
1、创建一个dockerfile文件,名字可以随机 建议dockerfile
这里我创建好了直接cat查看
[root@liuyang home]# cat dockerfile1
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "========成功!========="
CMD /bin/bash
[root@liuyang home]#
2、执行docker bulid
[root@liuyang home]# docker build -f /home/dockerfile1 -t liuyang_centos/centos:1.0 .
解释:
#-f 宿主机dockerfile文件位置
#-t centos的镜像别名
#:后面是TAG版本号
3、运行:
[root@liuyang home]# docker build -f /home/dockerfile1 -t liuyang_centos/centos:1.0 . Sending build context to Docker daemon 225MB Step 1/4 : FROM centos ---> 300e315adb2f Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in 6caa2c9415b9 Removing intermediate container 6caa2c9415b9 ---> 3239dac9b228 Step 3/4 : CMD echo "========成功!=========" ---> Running in 39c000f95d53 Removing intermediate container 39c000f95d53 ---> 3479dadac365 Step 4/4 : CMD /bin/bash ---> Running in 5c8908bd226d Removing intermediate container 5c8908bd226d ---> 6bb3a414dd02 Successfully built 6bb3a414dd02 Successfully tagged liuyang_centos/centos:1.0 [root@liuyang home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE liuyang_centos/centos 1.0 6bb3a414dd02 6 seconds ago 209MB [root@liuyang home]#
4、查看我们自己挂载的volumn01和02
[root@liuyang home]# docker run -it 6bb3a414dd02 /bin/bash
[root@1afdee48f435 /]# ls
bin etc lib lost+found mnt proc run srv tmp var (这里)volume02
dev home lib64 media opt root sbin sys usr (这里)volume01
[root@1afdee48f435 /]#
5、找到容器中我们新建的两个volumn在宿主机中对应位置
这里的:d9ae340d2845是我们的生成的镜像的容器ID
[root@liuyang home]# docker inspect d9ae340d2845
#此时,我们可以清楚的看到,两个column的source源在宿主机哪个位置,并且路径规则完美符合规范:/var/lib/docker/volumes/XXXXXX/_data" "Mounts": [ { "Type": "volume", "Name": "0df791aed03217591e9bceca25079c1127d7ede2ed23c5503690004593f74de9", "Source": "/var/lib/docker/volumes/0df791aed03217591e9bceca25079c1127d7ede2ed23c5503690004593f74de9/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "15c545d52c85498b2a13190c6cfb8a73ac08811f717b17e3f3ad39ae0bfe1f6d", "Source": "/var/lib/docker/volumes/15c545d52c85498b2a13190c6cfb8a73ac08811f717b17e3f3ad39ae0bfe1f6d/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ]
6、测试宿主机与容器数据联通
容器内部新建文件
[root@d9ae340d2845 volume01]# touch liuyang.java
宿主机可查看容器新建的内容
[root@liuyang home]# cd /var/lib/docker/volumes/0df791aed03217591e9bceca25079c1127d7ede2ed23c5503690004593f74de9/_data
[root@liuyang _data]# ls
liuyang.java
总结:这种方式我们未来使用的非常多,因为我们通常会构建自己的镜像!
那假设我们构建镜像的时候没有挂载卷,要手动镜像挂载 -v 卷名:容器内路径!
那么,如何实现容器和容器之间数据共享并同时映射到本地?
columes-from 实现容器间数据共享
1、查看自己建立的镜像
[root@liuyang _data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
liuyang_centos/centos 1.0 6bb3a414dd02 29 minutes ago 209MB
2、运行第一个容器:
[root@liuyang _data]# docker run -it 6bb3a414dd02
[root@e44d9d4fb165 /]# [root@liuyang _data]#
[root@liuyang _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e44d9d4fb165 6bb3a414dd02 "/bin/sh -c /bin/bash" 6 seconds ago Up 6 seconds eager_solomon
3、运行第二个容器:
[root@liuyang _data]# docker run -it --name docker02 --volumes-from eager_solomon 6bb3a414dd02
[root@liuyang _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
696b9dbb9a77 6bb3a414dd02 "/bin/sh -c /bin/bash" 10 seconds ago Up 9 seconds docker02
e44d9d4fb165 6bb3a414dd02 "/bin/sh -c /bin/bash" 52 seconds ago Up 52 seconds eager_solomon
4、运行其中一个容器,在volumn01中新建文件,测试看另一个容器以及宿主机是否同步更新了volumn01中新建的文件:
容器1:
[root@liuyang ~]# docker attach 696b9dbb9a77
[root@696b9dbb9a77 /]# cd volume01
[root@696b9dbb9a77 volume01]# ls
[root@696b9dbb9a77 volume01]# touch liuyang.java
容器2:
[root@liuyang ~]# docker attach e44d9d4fb165
[root@e44d9d4fb165 /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
[root@e44d9d4fb165 /]# cd volume01
[root@e44d9d4fb165 volume01]# ls
liuyang.java
宿主机:
[root@liuyang _data]# docker inspect e44d9d4fb165
找到mounts 路径:
"Source": "/var/lib/docker/volumes/0b319872335342bd7b6aa0eb35affc1e11df27685087154ed084a43ca23e7645/_data",
[root@liuyang _data]# cd /var/lib/docker/volumes/0b319872335342bd7b6aa0eb35affc1e11df27685087154ed084a43ca23e7645/_data
[root@liuyang _data]# ls
liuyang.java
总结:通过columes-from,生成多个容器,实现了容器间数据共享,删除某一个容器,并不会使得容器内指定的数据被破坏,实现了数据的持久化。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。