Search code examples
dockerdockerfilebreakpointsdocker-imagedocker-build

How to set breakpoint in Dockerfile itself?


Searching up the above shows many results about how to set breakpoints for apps running in docker containers, yet I'm interested in setting a breakpoint in the Dockerfile itself, such that the docker build is paused at the breakpoint. For an example Dockerfile:

FROM ubuntu:20.04

RUN echo "hello"
RUN echo "bye"

I'm looking for a way to set a breakpoint on the RUN echo "bye" such that when I debug this Dockerfile, the image will build non-interactively up to the RUN echo "bye" point, exclusive. After then, I would be able to interactively run commands with the container. In the actual Dockerfile I have, there are RUNs before the breakpoint that change the file system of the image being built, and I want to analyze the filesystem of the image at the breakpoint by being able to interactively run commands like cd / ls / find at the time of the breakpoint.


Solution

  • You can't set a breakpoint per se, but you can get an interactive shell at an arbitrary point in your build sequence (between steps).

    Let's build your image:

    Sending build context to Docker daemon  2.048kB
    Step 1/3 : FROM ubuntu:20.04
     ---> 1e4467b07108
    Step 2/3 : RUN echo "hello"
     ---> Running in 917b34190e35
    hello
    Removing intermediate container 917b34190e35
     ---> 12ebbdc1e72d
    Step 3/3 : RUN echo "bye"
     ---> Running in c2a4a71ae444
    bye
    Removing intermediate container c2a4a71ae444
     ---> 3c52993b0185
    Successfully built 3c52993b0185
    

    Each of the lines that says ---> 0123456789ab with a hex ID has a valid image ID. So from here you can

    docker run --rm -it 12ebbdc1e72d sh
    

    which will give you an interactive shell on the partial image resulting from the first RUN command.

    There's no requirement that the build as a whole succeed. If a RUN step fails, you can use this technique to get an interactive shell on the image immediately before that step and re-run the command by hand. If you have a very long RUN command, you may need to break it into two to be able to get a debugging shell at a specific point within the command sequence.