Debugging techniques

Traditional debugging of errors during image builds often require a developer to place various print commands through out the build commands to help reason about the state of the system before the failure occurs. This can be slow and cumbersome.

Earthly provides an interactive mode which gives you access to a root shell when an error occurs, which we'll cover in this guide.

Let's consider a test example that prints out a randomly generated phrase:

# Earthfile

VERSION 0.8
FROM python:3
WORKDIR /code

test:
  RUN curl https://raw.githubusercontent.com/jsvine/markovify/master/test/texts/sherlock.txt > /sherlock.txt
  COPY generate_phrase.py .
  RUN pip3 install markovify
  RUN python3 generate_phrase.py

and our python code:

# generate_phrase.py

import markovify
text = open('sherlock.txt').read()
text_model = markovify.Text(text)
print(text_model.make_sentence())

Now we can run it with earthly +test, and we'll see a failure has occurred:

Why can't it find the sherlock.txt file? Let's re-run earthly with the --interactive (or -i) flag: earthly -i +test

This time we see a slightly different message:

This time rather than exiting, earthly will drop us into an interactive root shell within the container of the build environment. This root shell will allow us to execute arbitrary commands within the container to figure out the problem:

Ah ha! the corpus text file was located in the root directory rather than under /code. We can try moving it manually to see if that fixes the problem:

At this point we know what needs to be done to fix the test, so we can type exit (or ctrl-D), to exit the interactive shell.

Note that even though we fixed the problem during debugging, the image will not have been saved, so we must go back to our Earthfile and fix the problem there:

Debugging integration tests

Let's consider a more complicated example where we are running integration tests within an embedded docker setup:

and our server.py code:

Let's fire up our integration test with earthly -P -i +test:

There was a failure checking that the server output contained the string hello; let's see what is going on:

The good news is our server container is running; let's see what happens when we try to connect to it:

Ah ha! The problem is our test is expecting a lowercase h, so we can fix our grep to look for an uppercase H:

Then when we re-run our test we get:

With the use of the interactive debugger; we were able to examine the state of the embedded containerized

Demo

asciicast

Final tips

If you ever want to jump into an interactive debugging session at any point in your Earthfile, you can simply add a command that will fail such as:

and run earthly with the --interactive (or -i) flag.

Hopefully you won't run into failures, but if you do the interactive debugger may help you discover the root cause more easily. Happy coding.

Last updated

Was this helpful?