Zum Hauptinhalt springen

Docker Remote Debugging with PyCharm

To connect to python remote interpreter inside docker, you have to make sure first, that PyCharm is aware of your docker.

Go to Settings > Build, Execution, Deployment > Docker. If you are on Linux, you can use docker directly using its socket unix:///var/run/docker.sock. If you are on Windows or Mac, make sure that you have docker-machine installed, then you can simply Import credentials from Docker Machine.

Docker Settings

Configure Remote Python Interpreter

This repository comes with already prepared "Run/Debug Configurations" for docker.

Run/Debug Configurations

But as you can see, at the beginning there is something wrong with them. They have red X on django icon, and they cannot be used, without configuring remote python interpreter. To do that, you have to go to Settings > Build, Execution, Deployment first.

Next, you have to add new remote python interpreter, based on already tested deployment settings. Go to Settings > Project > Project Interpreter. Click on the cog icon, and click Add Remote.

Add Remote Interpreter

Switch to Docker Compose and select local.yml file from directory of your project, next set Service name to django.

Docker Compose Settings

Having that, click OK. Close Settings panel, and wait few seconds...

Waiting for Configuration

After few seconds, all Run/Debug Configurations should be ready to use.

Ready Configurations

Available Features

Things you can do with provided configuration:

Run and Debug Python Code

Run Python Code

Run and Debug Tests

Run Tests Debug Tests

Run and Debug Migrations or Different Django Management Commands

Run Management Commands

And many others...

Known Issues

PyCharm Hangs on "Connecting to Debugger"

Connecting to Debugger Issue

This might be fault of your firewall. Take a look on this ticket - https://youtrack.jetbrains.com/issue/PY-18913

Modified Files in .idea Directory

Most of the files from .idea/ were added to .gitignore with a few exceptions, which were made, to provide "ready to go" configuration. After adding remote interpreter some of these files are altered by PyCharm:

Modified .idea Files

In theory you can remove them from repository, but then, other people will lose a ability to initialize a project from provided configurations as you did. To get rid of this annoying state, you can run command:

git update-index --assume-unchanged fermentrack.iml