如何 debug Dockerfile
通过 Dockerfile 构建镜像的过程
- 从 base 镜像运行一个容器
- 执行命令对容器做修改
- 执行类似 docker commit 的操作,生成一个新的镜像层
- Docker 再基于刚刚提交的镜像运行一个新容器
- 重复 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 [email protected]:~# cat Dockerfile 2 FROM busybox 3 RUN touch tmpfile 4 RUN /bin/bash -c echo "continue to build ....." 5 COPY testfile / 6 [email protected]:~# 7 [email protected]:~# 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 [email protected]:~# 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
原文地址:https://www.cnblogs.com/gsophy/p/10221885.html
时间: 2024-11-03 22:13:24