Debugging a Dockerfile

Sometimes creating a Dockerfile may not start with everything working. A Dockerfile does not always build images and sometimes it does, but starting a container would crash on startup.

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. The following example explains this:

  1. Create a Dockerfile using your favorite editor:
      FROM busybox 
RUN ls -lh
CMD echo Hello world
  1. Now, build the image by executing the following command:
      $ docker build .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM busybox
latest: Pulling from library/busybox
56bec22e3559: Pull complete
Digest: sha256:29f5d56d12684887bdfa50dcd29fc31eea4aaf4ad3bec43daf19026a7ce69912
Status: Downloaded newer image for busybox:latest
---> e02e811dd08f
Step 2 : RUN ls -lh
---> Running in 7b47d3c46cfa
total 36
drwxr-xr-x 2 root root 12.0K Oct 7 18:18 bin
dr-xr-xr-x 130 root root 0 Nov 27 01:36 proc
drwxr-xr-x 2 root root 4.0K Oct 7 18:18 root
dr-xr-xr-x 13 root root 0 Nov 27 01:36 sys
drwxrwxrwt 2 root root 4.0K Oct 7 18:18 tmp
---> ca5bea5887d6
Removing intermediate container 7b47d3c46cfa
Step 3 : CMD echo Hello world
---> Running in 490ecc3d10a9
---> 490d1c3eb782
Removing intermediate container 490ecc3d10a9
Successfully built 490d1c3eb782

$

Notice the ---> Running in 7b47d3c46cfa line. 7b47d3c46cfa is a valid image and can be used to retry the failed instruction and see what's happening

To debug this image, we need to create a container and then log in to analyze the error. 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 fails 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.

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

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