015、调试Dockerfile(2019-01-04 周五)
Dockerfile构建镜像的过程
1、从base镜像运行一个容器
2、执行一条指令,对容器进行修改
3、执行类似 docker commit的操作,生成一个新的镜像层
4、Docker在基于刚刚提交的镜像层运行一个新的容器
5、重复步骤 2-4 ,直到Dockerfile中所有指令执行完毕
root@docker-lab:~/docker# ls
Dockerfile
root@docker-lab:~/docker# cat Dockerfile
FROM centos
RUN uptime
RUN touch tmpfile
RUN ls /mnt2
root@docker-lab:~/docker# docker build -t centos-t1 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
latest: Pulling from library/centos
a02a4930cb5d: Already exists
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for centos:latest
---> 1e1148e4cc2c
Step 2/4 : RUN uptime
---> Running in f05e49595929
06:44:46 up 204 days, 22:07, 0 users, load average: 0.02, 0.02, 0.00
Removing intermediate container f05e49595929
---> be8d26615578
Step 3/4 : RUN touch tmpfile
---> Running in ada319df9d43
Removing intermediate container ada319df9d43
---> acaccd0c45d3
Step 4/4 : RUN ls /mnt2 # 第一次build报错, ls /mnt2 执行失败
---> Running in 590fc8e2360c
ls: cannot access /mnt2: No such file or directory
The command '/bin/sh -c ls /mnt2' returned a non-zero code: 2
root@docker-lab:~/docker# docker build -t centos-t1 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 1e1148e4cc2c # 第二次build直接使用了之前成功build镜像层的缓存
Step 2/4 : RUN uptime
---> Using cache
---> be8d26615578 # 第二次build直接使用了之前成功build镜像层的缓存
Step 3/4 : RUN touch tmpfile
---> Using cache
---> acaccd0c45d3 # 第二次build直接使用了之前成功build镜像层的缓存
Step 4/4 : RUN ls /mnt2 # 第二次build报错, ls /mnt2 执行失败
---> Running in b17908bf7059
ls: cannot access /mnt2: No such file or directory
The command '/bin/sh -c ls /mnt2' returned a non-zero code: 2
root@docker-lab:~/docker# docker images -a # 此处可以看到build前三个成功步骤生成的镜像层
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> be8d26615578 34 seconds ago 202MB
<none> <none> acaccd0c45d3 34 seconds ago 202MB
centos latest 1e1148e4cc2c 4 weeks ago 202MB
root@docker-lab:~/docker# docker run -it acaccd0c45d3 # 进入最后一次成功的镜像层,查看失败原因,没有 /mnt2 目录
[root@9ae841c96c62 /]# ls /mnt2
ls: cannot access /mnt2: No such file or directory
[root@9ae841c96c62 /]# ls /
anaconda-post.log dev home lib64 mnt proc run srv tmp usr
bin etc lib media opt root sbin sys tmpfile var
[root@9ae841c96c62 /]#