0

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?

3
  • When you say "if I start it...", what action exactly are you taking? Is the Docker setup involved here at all? Commented Jul 31 at 16:17
  • maybe previous debugger is still running. And sometimes system is blocking port for short time after closing connection and it needs to wait a bit. Commented Jul 31 at 16:20
  • Thankyou, should be clearer now @DavidMaze Commented Jul 31 at 16:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.