1

I have just started working on python. I want to debug python code that is running in a docker container. Can anyone please suggest me what could be the best way to remotely debug this? I am open to use any of the IDE like pycharm or VS Code etc. Thanks in advance.

Note: I'm using Mac OS for the development
Prateek Gupta
  • 1,129
  • 1
  • 11
  • 24
  • Is there any reason why you do not want to debug your python code on your own computer before putting it in a container? – StephaneM Sep 27 '18 at 14:14
  • @StephaneM the code is already running in a container , I want to debug the code, put some breakpoints into the code like we do in Visual Studio. Is it possible in this case ? – Prateek Gupta Sep 27 '18 at 17:21

2 Answers2

2

One technique that I used to debug from within container was to :

  • Comment the entrypoint ENTRYPOINT ["/mydir/mycode.py"] in the Dockerfile
  • Restart the container and do docker exec -it <container name> /bin/bash
  • Then perform pdb /mydir/mycode.py

Other technique could be to expose the remote debugging port on the docker container using docker expose command which will allow pycharm to connect to the code. A similiar approach is discussed here.

https://medium.com/@furkanpur/remote-python-debug-to-docker-container-over-ssh-by-using-pycharm-44a9b6e82206

piy26
  • 1,574
  • 11
  • 21
  • But this technique won't help in debugging the code from any IDE. I specifically want to debug code and put some breakpoints in code from IDE. – Prateek Gupta Sep 27 '18 at 17:23
2

Expanding on what piy26 mentioned regarding exposing a remote docker port, I think pudb is very apt for the task. I wrote a github repository with a simple example and step by step instructions you can clone and try out https://github.com/isaacbernat/docker-pudb

There is more info on this other answer: Running pudb inside docker container

isaacbernat
  • 395
  • 1
  • 5
  • 19
  • Does it get attach to any IDE like VS code or pycharm ? – Prateek Gupta Sep 27 '18 at 17:25
  • Afaik it can not be attached to any IDE. But I didn't understand this was a requirement reading the question. Pudb has a text-based based GUI though, with stack traces, and some IDE-like functionalities. Maybe this already covers your needs? You can get more info from their page https://pypi.org/project/pudb/ – isaacbernat Sep 28 '18 at 09:09
  • Did that work for you @PrateekGupta? Did you manage to make it work with VS code or pycharm? – isaacbernat Oct 05 '18 at 07:37