Debugging in a Running Container#

It can be difficult to get everything right in your Dockerfile. It’s helpful to be able to get a running shell inside of the container that will let you explore what’s wrong and try some fixes. In this lab you’ll learn how to use docker exec for debugging.

Run an Existing Container#

Use docker run to start the Apache container you built last week. This time we’ll run the container in detached mode so the prompt will return.

$ docker run --name debug -d -p 8080:80 hello-http 

Verify the container is running:

$ docker ps 

Connect to the Container#

We can create a new process in the container with docker exec. The most useful is bash or sh depending on the image. The -it switches are mandatory when you want to run a shell, they enable the connection of keyboard signals (e.g. Ctrl-c) to the process in the container.

$ docker exec -it debug /bin/bash
root@9dbc31635eda:/usr/local/apache2# 

Notice that the prompt changed!

Get Set for Debugging#

The Apache container is based on a Debian image and has apt. Let’s use apt to install vim.

root@9dbc31635eda:/usr/local/apache2# apt update
root@9dbc31635eda:/usr/local/apache2# apt install vim 

Edit index.html#

Now that you have vim, use it to edit index.html:

root@9dbc31635eda:/usr/local/apache2# vim htdocs/index.html

Make a change and reload the browser.

Clean Up#

To stop and remove your container use docker rm:

$ docker rm -f debug