Debug in Docker after code change

I followed the docker-compose tutorial to be able to debug my application.  That works.  However when I change the source code in the PyCharm text editor, Stop and Debug again, the breakpoints are out of sync: the description in the Debug frame stack doesn't match with where execution is being shown in the text editor.

My application is ran as a docker-compose, and I am trying to debug one of the services.  The application structure is like:

  • my_app/
  •     docker-compose.yaml
  •     service1/
  •         Dockerfile
  •         service1_src/

The Dockerfile does a copy of the source code

COPY /service1/service1_src /service1_src

and when I use PyCharm > Services > Docker > Docker-compose: my_app > service1 > /SERVICE1

and look at the Files, I see the code in both /service1_src and /opt/project/my_app/service1/service1_src.

I have a feeling I need to setup Path Mappings, but I am not sure what and where.

There are three places where a copy of my source code lives:

  • on my host PC filesystem where the PyCharm project is
  • in the Docker container at /service1_src
  • in the Docker container at /opt/project/my_app/service1/service1_src

There are three places I see that I can enter mappings:

  • File > Settings > Build, Execution, Deployment > Docker > Path mappings
  • File > Settings > Project: > Python Interpreter > Path mappings
  • Run/Debug Configurations > Python > main > Configuration > Path mappings

One thing I have found that works is to delete the Services > Docker > Images > service1:latest whenever I make changes that moves the source to different line numbers, but that seems like a slow process.

Does anyone have any suggestions on how to best have the local source changes be seen when debugging in the container?

PyCharm 2021.2.2 on Windows 10, Docker Desktop 4.0.1


Comment actions Permalink


What about setting a path mapping of your local PyCharm folder to /service1_src inside the container? That would override the code added by the Dockerfile, which is meant for production and not for deployment, and makes things a bit easier. Otherwise, you would need to start your development code from /opt/project.

If you run your app from /service1_src, but the new code is mounted somewhere else, you could have problems with breakpoints synchronization and such.


Comment actions Permalink

Francesco Frassinelli,

Thank you for the suggestion.  I think you are suggesting to add to the Project Interpreter's Path mappings:

<Project root>/my_app/service1/service1_src  ->  /service1_src

I added that.  To test, I removed the Docker image, and did a PyCharm Debug of (which will rebuild the image).  I made some source code changes and used the PyCharm "Rerun 'main'".

When it runs again, it is running as if I had not made the changes.  Did I misunderstand something?

Comment actions Permalink

Uhm, it does not seem to work, I also tested it on my PC. I would then suggest not to use path mappings and just rely on the default one, which should be /opt/project.

Just set /opt/project to be your working directory and starts your program from there. It seems the simplest option to me.

Comment actions Permalink

service1's Dockerfile originally had

WORKDIR /service1_src

I tried "WORKDIR /opt/project" as well as "WORKDIR /opt/project/my_app/service1/service1_src".  Neither one fixes the issue where the debugger gets out of sync with modified source code.

Thanks for the ideas.

Comment actions Permalink

Working directory should be set in PyCharm and it should be /opt/project by default, where your project is exposed. If you run your program there, there should be no sync issues.

Comment actions Permalink

For's PyCharm Run/Debug Configuration, the default working directory was

<absolute path to project root>/my_app/service1/service1_src

I tried changing that to /opt/project but it did not fix the issue.

Thanks again.


Please sign in to leave a comment.