til

Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as an open source contributor and product manager

View project on GitHub

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 ; false
ENTRYPOINT ["cat", "/tmp/hello_world"]

Build it

$ docker build -t smelly_container .

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"]

Now you can inspect the container

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

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

References