pythondockerdocker-composepycharmremote-debugging

Docker/docker-compose and Pycharm debug port conflict


I Am trying to set up the debug environment for Pycharm and Python(Fastapi) in Docker (with docker-compose). But I stuck into the problem that I cannot launch both: the debug server and the docker image.

My setup of the entry point of the app:

# import debugpy
# debugpy.listen(('0.0.0.0', 5678))
# debugpy.wait_for_client()
# print("Debugger is attached!")

import pydevd_pycharm
pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True)

In the Pycharm I set up port 5678 for Python Debug Server. So, if I start debug in Pycharm first, then I get error during docker-compose up:

Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5678 -> 0.0.0.0:0: listen tcp 0.0.0.0:5678: bind: address already in use

If I start docker-compose first, then I get the error in Pycharm while starting debug:

Address already in use

It looks they both want to listen for the same port on my local machine and who listens the first, gets all access.

I tried to google but nothing.

At the same time, working with VSC does have any problems. It connects to the docker container and debugs as intended.

Please advise.


Solution

  • import pydevd_pycharm
    
    pydevd_pycharm.settrace('host.docker.internal', port=5678, stdoutToServer=True, stderrToServer=True)
    

    You might also want to take a look at the last answer here.