简体   繁体   中英

What is the best and easy way to remotely debug a python code running in a docker container?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM