Remote debugging Django app in a Docker container

I have 2 Docker containers that are running: divesite_app_1 (app container running Django) and divesite_db_1 (database container running MySQL).

I would like to set up remote debugging in PyCharm, and I was previously able to do this successfully via an "SSH Interpreter", but we have recently removed SSH from the Docker containers.

I am attempting to set up a "Docker Interpreter" to connect to the existing container (divesite_app_1) and debug my application using breakpoints. However, the Run/Debug configuration is such that a new container is created every time I click the Debug button. Previously, with my "SSH Interpreter", I was able to debug the existing container and PyCharm did not create a new container when I clicked the Debug button.

The problem with PyCharm creating a new container is that it is unable to connect to my database container (divesite_db_1) to retrieve data.

Here are the port forwarding settings on my containers:
divesite_app_1: 0.0.0.0:8000->8000/tcp
divesite_db_1:   33060/tcp, 0.0.0.0:3307->3306/tcp

How can I resolve this problem?

0
3 comments

Hi,

>The problem with PyCharm creating a new container is that it is unable to connect to my database container (divesite_db_1) to retrieve data.

Does it connect successfully if you run `docker run` or `docker-compose up` from the command line?

Since you have two services (app and db) I think it would make sense to run them via docker-compose, have you tried that?

For docker-compose, you could also run services from the terminal, and in run/debug configuration use the `exec` option to connect to existing containers, avoiding need for container restart every time.

0

Andrey, thanks for the suggestion, but I would like clarification on where to use the `exec` option in the Run/Debug configuration.

 

The configuration screenshot below is what I have working so far, because I am using an SSH-enabled container (by using a different Dockerfile that I wrote that installs openssh inside the container) when developing locally. You can see that my interpreter is using SFTP to sync my files (which requires SSH to be enabled in the container)

 

However, this is a hack because the Git-versioned Dockerfile that our team uses does not create a container with SSH enabled.

 

I would LOVE it if I could `exec` into the running container and debug my application that way.

0

Sorry, I'm afraid I have bad news which I forgot to mention - debugging is not available when using the `exec` option, because PyCharm cannot map dynamic ports that way which are needed for debugging. I'm sorry for the false hope.

Though this information is probably useless to your now, still, here's how you can execute the code in the running container using the exec  option:

It works like this:

1. Write a docker-compose.yml config

2. Create docker-compose interpreter based on this config.

3. Prepare the python file with your code. Make sure to map project root to the container by adding volume bindings to your docker-compose.yml (usually PyCharm uses an override config where it does bindings automatically, but in this case we must do this manually).

4. Run your services from the terminal with `docker-compose up`.

5. Create a regular python run/debug config (not the Docker config). Make sure it uses your new docker-compose interpreter.

6. Add the `exec` option and run.

0

Please sign in to leave a comment.