dockerpycharmfastapi

Unable to Connect PyCharm Debugger to Dockerized FastAPI Application


I’m struggling to set up remote debugging with PyCharm Professional for my FastAPI application running inside a Docker container. Despite numerous attempts and troubleshooting steps, I can’t get the debugger to connect.

IDE host Name: host.docker.internal
Port: 5678
Path Mapping: {project_path} =/app

My python script

try:
    import pydevd_pycharm
    pydevd_pycharm.settrace('host.docker.internal', port=5678, stdoutToServer=True, stderrToServer=True)
    logger.info("Debugger attached")
    logger.info("Starting program service")
except Exception as e:
    logger.info(f"Exception during debugger setup: {e}")

Docker compose

app:
  <<: *common_config
  environment:
    <<: *env_vars
  ports:
    - 8000:8000

Steps I Followed:

1.  I run the debugger in PyCharm first.
2.  Then I run docker compose up and start the container.

However, all I see in my logs is:

app-1 | 2024-11-26T15:58:15.177832Z [info ] Importing pydev_pycharm [code.api.main] dd.span_id=0 dd.trace_id=0

What could be going wrong?


Solution

  • Try to disable python.debug.use.single.port in Help | Find Action | Registry. Has it helped? Relevant issue in PyCharm's issue tracker

    https://youtrack.jetbrains.com/issue/PY-77357/Python-Debug-Server-with-pydevd-pycharm-stopped-working-in-2024.3

    reddit