Search code examples
pythondockerdebuggingvisual-studio-coderemote-debugging

Running a Python debugger in a Docker Image


I recently followed the following tutorial to try to debug python code in a Docker container using VSCode:

https://www.youtube.com/watch?v=qCCj7qy72Bg&t=374s

My Dockerfile looks like this:

FROM ubuntu as base

#Do standard image stuff here


#Python Debugger

From base as debugger

RUN pip3 install debugpy

ENTRYPOINT ["python3","-m","debugpy","--listen","0.0.0.0:5678","--wait-for-client"]

I have alternately tried copying the tutorial exactly and using the following ENTRYPOINT instead:

ENTRYPOINT ["python3","-m","debugpy","--listen","0.0.0.0:5678","--wait-for-client","-m"]

I have also configured a VSCode remote attach debug instance to launch.json:

{"name":"Python: Remote Attach","type":"python","request":"attach","connect":{"host":"5678","port":5678},"pathMappings":[{"localRoot":"${workspaceFolder}","remoteRoot":"."}]}, 

I want the debugger to either debug the current file alone in isolation, or run a file I use to run the entire project, called init.py with the debugger in the docker container.

Currently, when I build and run the docker container with

docker run -p 5678:5678 CONTAINERNAME python3 /home/init.py

It hangs and times out on the Visual Studio side.

In the video, he uses this to run the python unittest module, which is why I tried taking out the -m from the end of the command in my modified version. However, it looks like debugpy doesn't know what to do. I have tried running the docker instance before the remote debugger, or the remote debugger after the docker instance, but the error remains and the debug does not work. How can I remote debug into a docker instance using VSCode?

EDIT:

Thank you FlorianLudwig for pointing out that my original code used commas for the IP rather than the periods required.

I have edited the question to reflect this change. It removed issues where python complained about a malformed address, but it seems I am still having some sort of connection issue to the debugger.

EDIT2:

I think I figured out what caused the connection issue. It appears the visual studio default is to use the same host as the port number in question. I changed my host to 0.0.0.0 and I was able to debug by running the container then connecting to it via Visual Studio Debugging.


Solution

  • In your Dockerfile:

    "0,0,0,0:5678" should be "0.0.0.0:5678"

    To make it a valid ip address. 0.0.0.0 basically means "any" ip address.