Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Python in Docker Container

Tags:

python

docker

I have a docker container running a python server, mounted on my local volume (so it gets updated if I restart the container for instance)

However, this gets tremendously hard to debug. Im using PyCharm professional IDEA.

Ive tried following the guides on how to debug inside docker containers, but it only shows how to do it when you start the container inside PyCharm, in my case I got a big Terraform stuff going on to setup all the environment, so I gotta find a way of attaching to the container python interpreter or something like that.

Would any1 have any ideas or guides on this ?

Thanks !

like image 395
Gabriel Slomka Avatar asked Aug 29 '18 13:08

Gabriel Slomka


People also ask

Can you debug a Docker container?

In this articleYou can run and debug your apps in Linux or Windows containers running on your local Windows desktop with Docker installed, and you don't have to restart the container each time you make a code change.


1 Answers

There are many details missing that would be needed to get a full view, but there are generally two ways to debug containers: 1) debug a running container and 2) debug a container image.

Debugging Container Images and Failed Builds

The latter is much easier because you can look at the history of a particular image and run a layer inside it.

First, we take a look at our locally built images:

$ docker images

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
<none>                    <none>              77af4d6b9913        19 hours ago        1.089 GB
committ                   latest              b6fa739cedf5        19 hours ago        1.089 GB

Next, we can pick a particular image and run docker history on it:

$ docker history 77af4d6b9913
    IMAGE               CREATED             CREATED BY                                  SIZE                COMMENT
3e23a5875458        8 days ago          /bin/sh -c #(nop) ENV LC_ALL=C.UTF-8            0 B
8578938dd170        8 days ago          /bin/sh -c dpkg-reconfigure locales &&    loc   1.245 MB
be51b77efb42        8 days ago          /bin/sh -c apt-get update && apt-get install    338.3 MB
4b137612be55        6 weeks ago         /bin/sh -c #(nop) ADD jessie.tar.xz in /        121 MB

Then we can pick a layer anywhere in the history of the image and run that interactively:

$ docker run -it --rm 3e23a5875458 /bin/sh

This will dump you into a shell where you can run whatever the next command in the image-build process would be. This is super useful if your docker build command failed and you need to understand why, but it can also be useful if you just want to look at how things are set-up inside a particular container (such as your Python interpreter, dependencies, PATH, etc.).

Attaching to a Running Container

This can be a little more confusing, but similarly, you can run a command inside a runnning container using exec. For instance, I often want to make sure my environment variables are set correctly, so I'll run something like this:

$ docker exec my_container env

You can use this to create a shell inside the running container as well:

$ docker exec -it my_container /bin/sh

This is generic stuff, but useful broadly for debugging containers.

Note: I am using /bin/sh above because a lot of small base images (like Alpine) don't have bash installed.

like image 126
erewok Avatar answered Nov 02 '22 09:11

erewok