1. 程式人生 > >第 3 章 鏡像 - 015 - 調試 Dockerfile

第 3 章 鏡像 - 015 - 調試 Dockerfile

comm 運行 touch dia sha256 build 新的 成功 ret

如何 debug Dockerfile

通過 Dockerfile 構建鏡像的過程

  1. 從 base 鏡像運行一個容器
  2. 執行命令對容器做修改
  3. 執行類似 docker commit 的操作,生成一個新的鏡像層
  4. Docker 再基於剛剛提交的鏡像運行一個新容器
  5. 重復 2-4 步,直到 Dockerfile 中的所有指令執行完畢

如果 Dockerfile 由於某種原因執行到某個指令失敗了,我們也將能夠得到前一個指令成功執行構建出的鏡像,可以運行最新的這個鏡像定位指令失敗的原因。

舉個例子

Dockerfile

1 FROM busybox
2 RUN touch tmpfile
3 RUN /bin/bash -c echo
"continue to build ....." 4 COPY testfile /

構建過程如下

 1 root@ubuntu:~# cat Dockerfile 
 2 FROM busybox
 3 RUN touch tmpfile
 4 RUN /bin/bash -c echo "continue to build ....."
 5 COPY testfile /
 6 root@ubuntu:~# 
 7 root@ubuntu:~# docker build -t image-debug .
 8 Sending build context to Docker daemon  23
.04kB 9 Step 1/4 : FROM busybox 10 latest: Pulling from library/busybox 11 57c14dd66db0: Pull complete 12 Digest: sha256:b6e640a3768c460ad6066a003b6da52034c31aaf8500f9263057ddffcd830ef6 13 Status: Downloaded newer image for busybox:latest 14 ---> 3a093384ac30 15 Step 2/4 : RUN touch tmpfile 16 ---> Running in
3ba6dbde130c 17 Removing intermediate container 3ba6dbde130c 18 ---> 3043ba551c41 19 Step 3/4 : RUN /bin/bash -c echo "continue to build ....." 20 ---> Running in a16303c0b2f7 21 /bin/sh: /bin/bash: not found 22 The command /bin/sh -c /bin/bash -c echo "continue to build ....." returned a non-zero code: 127

21行出現錯誤,可以使用 3043ba551c41 進行調試。

1 root@ubuntu:~# docker run -it 3043ba551c41
2 / # /bin/bash -c echo "continue to build ....."
3 sh: /bin/bash: not found
4 / #

手工執行 RUN 指令很容易定位失敗的原因是 busybox 鏡像中沒有 bash。

------------引用來自------------

https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587606&idx=1&sn=656e82adf088ae2652d245dc49b94873&chksm=8d30808fba470999569781cb1f8db769126769717f8899993cab6e4c36c65da0ed4a3205cf99&scene=21#wechat_redirect

第 3 章 鏡像 - 015 - 調試 Dockerfile