Yes, this is possible - when the Python app is running in a Docker container, you can treat it like a remote machine.

In your Docker image, you'll need to make the remote debugging port available (e.g. EXPOSE 3000 in the Dockerfile), include the ptvsd setup in your Python app, and then publish the port when you run the container, something like:

docker run -d -p 3000:3000 my-image

Then use docker inspect to get the IP address of the running container, and that's what you use for the host in the launch file.

Answer from Elton Stoneman on Stack Overflow
Top answer
1 of 3
15

Yes, this is possible - when the Python app is running in a Docker container, you can treat it like a remote machine.

In your Docker image, you'll need to make the remote debugging port available (e.g. EXPOSE 3000 in the Dockerfile), include the ptvsd setup in your Python app, and then publish the port when you run the container, something like:

docker run -d -p 3000:3000 my-image

Then use docker inspect to get the IP address of the running container, and that's what you use for the host in the launch file.

2 of 3
3

works with vscode 1.45.0 & later. for reference files https://gist.github.com/kerbrose/e646aaf9daece42b46091e2ca0eb55d0

1- Edit your docker.dev file & insert RUN pip3 install -U debugpy. this will install a python package debugpy instead of the deprecated one ptvsd because your vscode (local) will be communicating to debugpy (remote) server of your docker image using it.

2- Start your containers. however you will be starting the python package that you just installed debugpy. it could be as next command from your shell.

docker-compose run --rm -p 8888:3001 -p 8879:8069 {DOCKER IMAGE[:TAG|@DIGEST]} /usr/bin/python3 -m debugpy --listen 0.0.0.0:3001 /usr/bin/odoo --db_user=odoo --db_host=db --db_password=odoo

3- Prepare your launcher file as following. please note that port will be related to odoo server. debugServer will be the port for the debug server

{
    "name": "Odoo: Attach",
    "type": "python",
    "request": "attach",
    "port": 8879,
    "debugServer": 8888,
    "host": "localhost",
    "pathMappings": [
        {
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/mnt/extra-addons",
        }
    ],
    "logToFile": true
}
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › debug-python
Debug Python within a container
November 3, 2021 - How to configure and troubleshoot debugging of Python apps running in a container, using Visual Studio Code.
Discussions

Debugging dockerized Python apps in VSCode
I find it easiest to just call breakpoint() in my code and use the docker attach command to drop into the command line debugger. No extra setup. And you can improve the command line experience with pdbpp or ipdb. More on reddit.com
🌐 r/Python
20
126
December 23, 2023
Python Docker Remote Debugging VS Code - Stack Overflow
trying to get remote debugging for my python flask API I have. I'm able to docker-compose up and have postman successfully call the running container, but when I try to attach the debugger, it never More on stackoverflow.com
🌐 stackoverflow.com
python - PyCharm remote debug in a docker container - Stack Overflow
I'm having an hard time trying to figure out how to setup a remote debug of a python app (Flask) running in a Docker container. Specifically I'm using docker-compose, PyCharm professional and pytho... More on stackoverflow.com
🌐 stackoverflow.com
Anyone has a python remote (docker) debugger setup?
Check out remote-pdb and pdbpp if you want to continue using terminal debugers. More on reddit.com
🌐 r/neovim
4
5
June 18, 2023
🌐
Docker
docker.com › blog › containerized-python-development-part-3
Containerized Python Development - Part 3 | Docker
November 21, 2024 - Second, and the more serious approach is by using a debugger. When we have a containerized process, we need to run a debugger inside the container and then connect to that remote debugger to be able to inspect the instance data.
🌐
Martin Heinz
martinheinz.dev › blog › 99
Remote Interactive Debugging of Python Applications Running in Kubernetes | Martin Heinz | Personal Website & Blog
June 19, 2023 - # debugger.Dockerfile FROM python:3.11.4-slim-buster RUN apt-get update && apt install -y gdb RUN pip install debugpy ENV DEBUGPY_LOG_DIR=/logs
🌐
Reddit
reddit.com › r/python › debugging dockerized python apps in vscode
r/Python on Reddit: Debugging dockerized Python apps in VSCode
December 23, 2023 -

Finally, set aside the time to configure VScode debugger to peek into web apps running inside docker containers.

I use the debugger with pretty much everything but containers. Not sure why I didn’t bother to do it earlier. Huge productivity boost. TIL:

https://rednafi.com/python/debug_dockerized_apps_in_vscode/

🌐
GitHub
github.com › DonJayamanne › vscode-python-samples › tree › master › remote-debugging-docker
vscode-python-samples/remote-debugging-docker at master · DonJayamanne/vscode-python-samples
Type the following command in the terminal window docker run -it -p 3000:3000 remote-debugging-docker · Confirm the following is displayed in the terminal window Waiting to attach · This step will ensure you have setup the program to be debugged ...
Author   DonJayamanne
Top answer
1 of 1
4

I finally got it working with remote debugging. I had to pip3 install ptvsd==3.0.0 on my local, and make sure that the requirements.txt for my docker container had the same version. (note: the latest version 3.2.1 didn't work)

@BrettCannon had the right link for a good tutorial https://code.visualstudio.com/docs/python/debugging#_remote-debugging

What I had to do was add some code to the app.py of the flask app. I originally was getting address already in use error when starting the container, so I added the socket code and after the first successful attach of debugger I didn't seem to need it anymore (strange I know, but that's why I left it in in case someone else gets that error)

try:
    import ptvsd
    # import socket
    # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # sock.close()
    ptvsd.enable_attach(secret=None,address=('0.0.0.0',5050))
    ptvsd.wait_for_attach()
except Exception as ex:
    print('Not working: ')

also i took the debug kwarg off the app.run() in app.py for the flask app. This all gave me the ability to connect the debugger, but the breakpoints where "Unverified", so the last thing that had to happen was a path to the app.py in the launch.json for the remoteRoot. I will say I created a small test api to get this working, and it only need the first level of pathing (ie. /app and not /app/app/app.py)Here is a github of the test api I made (https://github.com/tomParty/docker_python). So if the debugger is attaching, but your breakpoints are unverified, play around with the path of the remoteRoot

"remoteRoot": "/nomz/nomz/app.py"
Find elsewhere
🌐
Vinta
vinta.ws › code › remotely-debug-a-python-app-inside-a-docker-container-in-visual-studio-code.html
Remotely debug a Python app inside a Docker container in Visual Studio Code – I Failed the Turing Test
February 24, 2018 - Visual Studio Code with Python extension has "Remote Debugging" feature which means you could attach to a real remote host as well as a container on localhost. NOTE: While you trace Python code, the "Step Into" functionality is your good friend.
🌐
Python Engineer
python-engineer.com › posts › debug-python-docker
How to debug Python apps inside a Docker Container with VS Code - Python Engineer
// For more information, visit: ... "." } ], "justMyCode": true } ] } You can now set breakpoints and then click on Run -> Start Debugging to start the debugging session....
🌐
Bell-sw
docs.bell-sw.com › alpaquita-linux › latest › containers › remote-debug
Remote debugging applications running in Docker remotely, with JetBrains and VSCode: Java, C, and Python
It is important that both local dev environment, where we debug the python code, and the container, where the application is running, have the debugpy module available. We will install it in the debugging image and also instruct docker to run debugpy as a default command when the container starts. Copy the contents of the following Dockerfile into a file Dockerfile.debug. # Base image FROM fastapi-app WORKDIR /src # Install debugpy for vscode remote debugging RUN pip3 install debugpy # Append debugpy to the uvicorn command so it will be executed in a debugging # mode CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:5678", \ "-m", "uvicorn", "main:app", "--host", "0.0.0.0"]
🌐
Towards Data Science
towardsdatascience.com › home › latest › debugging for dockerized ml applications in python
Debugging for Dockerized ML applications in Python | Towards Data Science
November 25, 2024 - What worked best in the end for me is a debugger that can connect to a Docker container where your model training application is running, and directly inspect the environment of a given Python script. This is where debugpy comes in! Previously known as ptvsd, this package is developed by Microsoft specifically for use in VSCode with Python. It implements all of the common debugging tools you would expect, as well as allowing for attaching to remote environments, such as Docker containers or even remote machines via SSH.
🌐
GitHub
github.com › paloukari › python-docker-remote-debugging
GitHub - paloukari/python-docker-remote-debugging
This is a simple example to demonstrate how to do Python remote debugging running in a Docker container, in VS Code.
Author   paloukari
🌐
Rednafi
rednafi.com › python › debug_dockerized_apps_in_vscode
Debugging dockerized Python apps in VSCode
December 22, 2023 - Below is a quick benchmark comparing the instantiation times of a mutable dataclass and a frozen one (in Python 3.12): ... Despite using VSCode as my primary editor, I never really bothered to set up the native debugger to step through application code running inside Docker containers.
🌐
pythontutorials
pythontutorials.net › blog › how-to-remote-debug-python-code-in-a-docker-container-with-vs-code
How to Remote Debug Python Code in a Docker Container with VS Code: Step-by-Step Guide — pythontutorials.net
In this guide, we’ll walk through **remote debugging Python code in a Docker container using VS Code**, step by step. By the end, you’ll be able to set breakpoints, inspect variables, and troubleshoot code as if it were running locally—all while leveraging the isolation of Docker.
🌐
Andrewwhipple
andrewwhipple.com › blog › 2023 › 02 › 07 › debug-python-in-vs-code
How To Debug Docker-ized Python Apps in Visual Studio Code
February 7, 2023 - This creates a command for VS Code called "Attach (remote debug)" that will tell it to attach a python debugger to 127.0.0.1:5678. If you've been paying attention, that should mean the debugger is now attaching to the port exposed by the application container that is being listened to by debugpy. Run docker...
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360006874159-Pycharm-Remote-Debugger-for-Docker-Debug-inside-container-launched-from-3rd-party-library
Pycharm Remote Debugger for Docker - Debug inside container launched from 3rd party library – IDEs Support (IntelliJ Platform) | JetBrains
December 18, 2019 - Just need this in the code somewhere and then setup a remote debugger config in pycharm to wait for the connection. import sys sys.path.append("./debug/pydevd-pycharm.egg") import pydevd_pycharm pydevd_pycharm.settrace('host.docker.internal', port=4200, stdoutToServer=True, stderrToServer=True) ... This is great but I have an additional question regarding how to debug contents within a package. For instance, my package is deployed at /opt/python/package/testpackage folder in the remote server and my Dockerfile has an ENTRYPOINT ["test"]. I've added above configuration to the relevant .py file in the package.
🌐
Medium
alpha2phi.medium.com › neovim-for-beginners-python-remote-debugging-7dac13e2a469
Neovim for Beginners — Python Remote Debugging | by alpha2phi | Medium
June 29, 2022 - In this article, we will learn how to debug a Python application running inside a Docker container.
🌐
JetBrains
jetbrains.com › help › pycharm › using-docker-as-a-remote-interpreter.html
Configure an interpreter using Docker | PyCharm Documentation
1 month ago - Click the Python Interpreter selector and choose Interpreter Settings. Click the Add Interpreter link next to the list of the available interpreters. Click the Add Interpreter link next to the list of available interpreters and select On Docker.