015、除錯Dockerfile(2019-01-04 週五)

三角形發表於2019-01-05
 
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 /]#
 
 

相關文章