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
🌐
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.
🌐
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/

Discussions

How to remote debug python code in a Docker Container with VS Code - Stack Overflow
If you want a nice step by step walkthrough of how to attach a remote debugger for VS code in a container you could check out the youtube video "Debugging Python in Docker using VSCode". More on stackoverflow.com
🌐 stackoverflow.com
debugging - debug python code running in docker container from host using vscode - Stack Overflow
Both examples need the Dockerfile to expose the port 5678. Now, after running the container, the debugger will "pause" and await attachment. To attach the debugger, go back to Run & Debug section on the activity bar in VSCode and select Python: Remote Attach from the dropdown menu and press play. More on stackoverflow.com
🌐 stackoverflow.com
Debugging a Python script executed in a Docker container with VSCode? - Stack Overflow
Let's say I have a Docker image called my_image and a Python script called my_code.py. That is, normally I would run something like this command to run the file: docker run my_image /usr/bin/python3 More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › debug-common
Debug containerized apps
November 3, 2021 - The Container Tools extension provides a docker debug configuration provider that manages how VS Code will launch an application and/or attach a debugger to the application in a running container. This provider is configured via entries within launch.json, with configuration being specific to each application platform supported by the provider. The Container Tools extension currently supports debugging Node.js, Python, and .NET applications within containers.
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
}
🌐
Python Engineer
python-engineer.com › posts › debug-python-docker
How to debug Python apps inside a Docker Container with VS Code - Python Engineer
In VS Code, click on Run -> Add Configuration -> Remote Attach and then leave the default configuration as localhost and port 5678. This creates a launch.json file inside the .vscode folder that tells the debugger to attach to port 5678 where ...
🌐
Rednafi
rednafi.com › python › debug_dockerized_apps_in_vscode
https://rednafi.com/python/debug-dockerized-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.
🌐
Rednafi
rednafi.com › python › debugging dockerized python apps in vscode
Debugging dockerized Python apps in VSCode | redowan's reflections
December 22, 2023 - Set up VSCode debugger for containerized Python applications using debugpy. Step-by-step guide with Docker Compose and launch configurations.
Find elsewhere
Top answer
1 of 1
4

Since I do not know what python process you have running, I'll use FastAPI as an example.

  1. First, you need to define a new configuration in the file <project_root>/.vscode/launch.json. To access this file, you can go to the Run & Debug section on the activity bar, and press the cog/wheel at the top.

Add a new item in the configurations like shown below.

{
    "version": "0.2.0",
    "configurations": [
        {
        "name": "Python: Remote Attach",
        "type": "python",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 5678
        },
        "pathMappings": [
            {
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "."
            }
        ]
        }
    ]
}
  1. Now you need to include debugpy. If you are using a requirements file, you can just add it there. If you use Poetry, you can run poetry add debugpy in your terminal (the Dockerfile example shows how you can use Poetry in a docker image).

  2. First option - In your Dockerfile you need to run debugpy and make it listen for port 5678. Now the debugger will start every time you run the container and await attachment.

FROM python:3.10.0

# Set the working directory:
WORKDIR /usr/src

# Copy the pyproject.toml file (or requirements.txt) to cache them in the docker layer:
COPY [ "./src/pyproject.toml", "./"]

# Set the python path if needed.
# For example, if main.py is not located in the project root:
ENV PYTHONPATH="$PYTHONPATH:${PWD}"

# Upgrade pip and install Poetry:
RUN pip install --upgrade pip && pip install poetry==1.1.12

# Project initialization using Poetry:
RUN poetry config virtualenvs.create false \
    && poetry install --no-interaction --no-ansi

# Run the application:
CMD ["python", "-m", "debugpy", "--wait-for-client", "--listen", "0.0.0.0:5678", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--reload-exclude", "tests"]

EXPOSE 8000 5678
  1. Second option - Set up the debugger in the main.py file by adding the lines below. This means that the debugger will be attached to the container when it starts; if the debugger variable is set to True.
debugger = True

if __name__ == "__main__":
    if debugger:
        import debugpy
        debugpy.listen(("0.0.0.0", 5678))
        debugpy.wait_for_client()

    uvicorn.run(
        "main:app",
        host="localhost",
        port=8000,
        log_level="info",
        reload=True,
    )
  1. Both examples need the Dockerfile to expose the port 5678. Now, after running the container, the debugger will "pause" and await attachment. To attach the debugger, go back to Run & Debug section on the activity bar in VSCode and select Python: Remote Attach from the dropdown menu and press play.

Now you should be able to use the debugger inside the docker container.

🌐
YouTube
youtube.com › watch
How to Run and Debug Python Inside Docker Containers Using VSCode - YouTube
You don't need to have Python installed on your system in order to run it. Replace Python with almost any other language and keep your computer free from all...
Published   April 25, 2021
🌐
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 ... to the port exposed by the application container that is being listened to by debugpy. Run docker-compose up to start up your application like normal....
🌐
YouTube
youtube.com › watch
Debugging Python in Docker using VSCode - YouTube
Subscribe to show your support! https://goo.gl/1Ty1Q2 . Patreon 👉🏽http://patreon.com/marceldempersGood day folks! In this video we'll take a look at debugg...
Published   September 30, 2019
🌐
InterSystems
community.intersystems.com › post › how-do-you-debug-embedded-python-docker-container-vscode
How do you debug embedded python (Docker Container) in VSCode | InterSystems DC
Hi @John.McBrideDev. We don't currently support formal debugging for embedded Python, so you won't be able to set breakpoints, etc. We're working on this high priority functionality. ... As Raj suggested, we don't currently support debugging in vscode.
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › quickstart-python
Python in a container
November 3, 2021 - Create a Dockerfile file describing a simple Python container. Build, run, and verify the functionality of a Django, Flask, or General Python app. Debug the app running in a container.
🌐
Stack Overflow
stackoverflow.com › questions › 72119749 › debugging-a-python-script-executed-in-a-docker-container-with-vscode
Debugging a Python script executed in a Docker container with VSCode? - Stack Overflow
Not unless you can install some debugger agent in the image. However, you can always do this: python3 -m pdb my_code.py. ... We can use command Docker: Add Docker Files to Workspace...
🌐
GitHub
github.com › navarmn › Python-debugger-Docker-VScode
GitHub - navarmn/Python-debugger-Docker-VScode: A tutorial how to debug your dockerized code remotely on vscode
A tutorial how to debug your dockerized code remotely on vscode - navarmn/Python-debugger-Docker-VScode
Starred by 3 users
Forked by 2 users
Languages   Python 42.5% | Makefile 33.6% | Dockerfile 23.9% | Python 42.5% | Makefile 33.6% | Dockerfile 23.9%
🌐
Docker
docker.com › blog › containerized-python-development-part-3
Containerized Python Development - Part 3 | Docker
November 21, 2024 - Another interesting case to exercise is the interactive debugging where we place breakpoints in the code and do a live inspect. For this we need an IDE with Python and remote debugging support. If we choose to rely on Visual Studio Code to show how to debug Python code running in containers we need to do the following to connect to the remote debugger directly from VSCode.
🌐
GitHub
github.com › microsoft › vscode-docker › discussions › 3750
Unable to run debug code inside container · microsoft/vscode-docker · Discussion #3750
December 2, 2022 - How is the debugger supposed to communicate with VSCode? ... # For more information, please refer to https://aka.ms/vscode-docker-python FROM python:3.8-slim # Keeps Python from generating .pyc files in the container ENV PYTHONDONTWRITEBYTECODE=1 # Turns off buffering for easier container logging ENV PYTHONUNBUFFERED=1 # Install pip requirements COPY requirements.txt .
Author   microsoft
🌐
Readthedocs
vscode-docs-arc.readthedocs.io › en › latest › containers › debug-python
Debug Python within a container - vscode-docs-arc
Start the container by right-clicking on a docker-compose.yml file and selecting Compose Up or doing docker run from the command line. Set a breakpoint in the chosen file. Navigate to Run and Debug and select the Python: Remote Attach launch configuration.
🌐
The Chaotic Engineer
chaoticengineer.hashnode.dev › debugging-python-apps-in-vscode
Debugging Python Apps in VSCode - The Chaotic Engineer
June 7, 2024 - Enter the running Docker container by doing the following: Press CTRL + Shift + P key combination. Select the "Dev Containers: Attach to Running Container..." option. Select the debugging container started in step 2. Open the /code directory, ...