Debugging a Dockerfile

Every instruction we set in the Dockerfile is going to be built as a separate, temporary image for the other instruction to build itself on top of the previous instruction.

There is a Dockerfile in the repo at /bootcamp/ch apter06/debu g:

FROM alpine
RUN ls -lha /home
RUN ls -lha /vars
CMD echo Hello world

Building the image using the following command:

docker image build

Gives you the following output:

Debugging a Dockerfile

So, there is an error in our Docker file. You may notice there is a line in the output which says --->5f828f86eaa4this is actually an image file which was built following the successful execution of the RUN ls -lha / home line.

Debugging a Dockerfile

This means that we can launch a container using this image:

docker container run -it --name=debug 5f828f86eaa4 /bin/sh

Note

Notice that as we are using Alpine Linux as our base we are using /bin/sh rather than /bin/bash

We can then debug our application, which in this case is simple:

Debugging a Dockerfile

Debugging is a process of analyzing what's going on and it's different for every situation, but usually the way we start debugging is by trying to manually make the instruction that fail work manually and understand the error. When I get the instruction to work, I usually exit the container, update my Dockerfile and repeat the process until I have something working.

Notice that when the line which is causing the error is corrected (by supplying the correct line RUN ls -lha /var) and we try the build again that Docker doesn't create a new image for the one step which was successful:

Debugging a Dockerfile

Once it has built the temporary image is removed and we are left with our final image:

Debugging a Dockerfile

That was quite a simple example, but it should give you an idea of how to debug a more complex Dockerfile.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset