Setup:
- FastAPI running inside Docker container, python3.12 image.
- Connection to it via VSCode remote attach debugger
This works if the application is started via
python -m debugpy --listen 0.0.0.0:5680 --wait-for-client main.py
docker run --mount type=bind,source=$(pwd),target=/home/app -p 5680:5680 -p 8080:8080 my_image
However if I add the following lines to the main.py:
debugpy.listen(("0.0.0.0", 5680))
debugpy.wait_for_client()
and then start the server with python main.py (so the CMD field in the Dockerfile reads CMD ["python", "main.py"]:
I get that debugpy can't listen on 5680 as the address is already in use
debugpy.listen(("0.0.0.0", 5680))
File "/usr/local/lib/python3.12/site-packages/debugpy/public_api.py", line 31, in wrapper
return wrapped(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/debugpy/server/api.py", line 133, in debug
log.reraise_exception("{0}() failed:", func.__name__, level="info")
File "/usr/local/lib/python3.12/site-packages/debugpy/server/api.py", line 131, in debug
return func(address, settrace_kwargs, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/debugpy/server/api.py", line 260, in listen
raise RuntimeError(str(endpoints["error"]))
RuntimeError: Can't listen for client connections: [Errno 98] Address already in use
It'd be really great if I could do things this way -- multiple use cases and less code change for development environments.
What am I doing wrong with this?