Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 482
Mario Ishac Avatar asked Dec 11 '25 09:12

Mario Ishac


2 Answers

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.

like image 105
David Maze Avatar answered Dec 14 '25 05:12

David Maze


I don't think this is possible directly - that feature has been discussed and rejected.

What I generally do to debug a Dockerfile is to comment all of the steps after the "breakpoint", then run docker build followed by docker run -it image bash or docker run -it image sh (depending on whether you have bash installed inside the container).
Then, I have an interactive shell, and I can run commands to debug why later stages are failing.

I agree that being able to set a breakpoint and poke around would be a handy feature, though.

like image 29
Nick ODell Avatar answered Dec 14 '25 06:12

Nick ODell