Docker build hangs. How can I see what is going on?

At some point in the last couple of years, Buildkit has become the default Docker backend. Buildkit does not write out intermediate layers as images, as a performance optimization. Therefore, if you need to debug a hanging docker build, comment out the line that the build is hanging at and all subsequent lines. You now have a Dockerfile that you can execute $ docker build . with. Once the build is finished, you can launch the image, bash into the container, run the command that is causing the build to hang and then inspect the state of the container to debug the situation.


First list the "layers" of your finished or incomplete image. Each layer typically corresponds to an instruction in your Dockerfile.

Identify the image ID using

docker image ls

then list the layers inside the image using

docker image history <ID>

You will see something like this:

IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
6c32fe3da793        2 days ago          /bin/sh -c #(nop) COPY file:c25ef1dcc737cb59…   635B                
4c1309db9b9c        2 days ago          /bin/sh -c #(nop) COPY dir:30506cf0fc0cdb096…   8.64kB              
5f5ae40b5fd5        3 weeks ago         /bin/sh -c apk update && apk add --no-cache …   164MB               
989d78741d0b        3 weeks ago         /bin/sh -c #(nop)  ENV DOCKER_VERSION=18.03.…   0B                  
6160911711fc        3 weeks ago         /bin/sh -c #(nop)  CMD ["python3"]              0B                  
... etc

Then create a container from any point inside your image. From there you can perform the next instruction that would cause a problem in your Dockerfile.

Eg:

docker run -it --rm 4c1309db9b9c sh

Tags:

Docker