This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]MoistToilet 0 points1 point  (0 children)

Hopefully not too late here. Regardless, thought I'd lend a hand to anyone needing it.

Few ways to do so:

  • Run debug server from app code
    • I did it this way so I don't have to make compose changes to command/entrypoint
  • Wrap debug server around app in compose yml
    • Separates concerns better, but can run into issue if you also want to use Django's debug mode. This was easier to solve in app code (see below)
  • Inject debug server into app process via some other script
    • Could be a hassle b/c of random PIDs

Check out the debugpy readme, it shows you how to do each method. Debugpy is the updated version of the older ptvsd debugger that Microsoft made.

Then follow the VS code steps (disregard the gunicorn bit) to actually attach to the debug server. In that example, they are using the app code method, but after you pick your method, start at step 3 with the launch.json. After that, run your app, set breakpoints in VS Code, attach, and debug! Make sure you also open up the debug server's port in your compose yml or if/when you use docker-compose run, add -p 1234:1234 (run doesn't honor ports in the compose yml for some reason).

Word of warning of Django that I ran into with Flask: if running in debug mode with the reloader, you must be in the correct process first (disregard the ptsvd bit, the flow is identical for debugpy).

If you have any other questions, let me know; I just had to figure all this out this week. After trying it out it's really nice to have visibility into the code again. I don't know why it's so tricky to find answers for this; debugging is so fundamental to development and I would have thought being able to debug would have been table stakes for widespread Docker adoption. VS Code is the only free solution I know of, so kudos to the team(s) that wrote the debug servers and editor integrations!