DEV Community

Jonas Brømsø
Jonas Brømsø

Posted on

TIL: Debugging a b0rken Docker build step

Debugging a b0rken Docker build step

docker is great! - but if your build will not work it can be a pain.

This nifty little trick can be used to inspect an image where a layer is not playing nicely, it does you have access to some sort of shell in the container.

I am emulating this with the shell builtin command false, but it could any build step, where the integrity of your container is relying on build steps to succeed

# Dockerfile
FROM       alpine:latest

LABEL      maintainer="jonasbn"
RUN        echo "hello world" > /tmp/hello_world
ENTRYPOINT ["cat", "/tmp/hello_world"]
Enter fullscreen mode Exit fullscreen mode

Build it

$ docker build -t smelly_container .
Enter fullscreen mode Exit fullscreen mode

So in order to get past the bad step you append: ; exit 0 to the difficult RUN step and will build not matter the return value of the previous shell command

# Dockerfile
FROM       alpine:latest

LABEL      maintainer="jonasbn"
RUN        echo "hello world" > /tmp/hello_world ; false; exit 0
ENTRYPOINT ["cat", "/tmp/hello_world"]
Enter fullscreen mode Exit fullscreen mode

Now you can inspect the container

$ docker run -it --entrypoint /bin/sh smelly_container
/ # cat /tmp/hello_world 
hello world
Enter fullscreen mode Exit fullscreen mode

And you can of course run it, your mileage might vary depending on the severity of the b0rkedness of your difficult build step, which we chose to ignore

$ docker run -it  smelly_container
hello world
Enter fullscreen mode Exit fullscreen mode

Source: StackOverflow

Originally posted in my TIL collection

Oldest comments (2)

Collapse
 
russellbateman profile image
Russell Bateman

Thanks for this short article. This is a little like murdering someone, then cutting him open to see if all his organs are in the right place. I wish there were a better, more sophisticated way.

Collapse
 
jonasbn profile image
Jonas Brømsø

I understand your point and I agree, but sometimes "Desperate Times Call for Desperate Measures".