Debugging 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#
docker run to start the
myapp container you built in the Build a Dockerfile lab. This time we’ll run the container in detached mode so the prompt will return.
$ docker run -d -p 8000:8000 myapp
Verify the container is running:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6191357d529c myapp "/bin/sh -c 'python …" 2 seconds ago Up 1 second 0.0.0.0:8080->8000/tcp, :::8080->8000/tcp romantic_elion
Make a note of your container’s unique name! You will need to substitute it for
romantic_elion in future commands.
Connect to the Container#
We can create a new process in the container with
docker exec. The most useful is
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 romantic_elion /bin/bash root@9dbc31635eda:/mysite#
Notice that the prompt changed!
Get Set for Debugging#
The Python container is based on a Debian image and has
apt. Let’s use
apt to install
root@9dbc31635eda:/mysite# apt update root@9dbc31635eda:/mysite# apt install vim
Now that you have
vim, use it to edit
root@9dbc31635eda:/mysite# vim mysite/urls.py
There’s no need to make a change. Just note that any changes you make are only to this running container. No future containers made with
docker run will have this change unless you also edit your source code or
To stop and remove your container use
$ docker rm -f romantic_elion