docker-compose up is a foreground starting (stdin capturing, stdout printing ... and waiting for the exiting command/signal)

For you case, more suitable is background starting (`docker compose up -d', see d(detached) flag). This command starts the container and give the control to next command (attaching).

UPDATE:

If background running does not help, try run in background and this solution.

Answer from Nick Vee 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

launch - VS Code run and debug Python in Docker using docker-compose - Stack Overflow
I've tried different ways to start ... && docker-compose start... Looks like vscode needs some exit code on finishing preLaunchTask. 2020-03-30T11:29:56.08Z+00:00 ... All programs return some code. 0 for normal execution, positive for errored. See the first answer update 2020-03-30T12:03:19.593Z+00:00 ... Finally I was able to launch and attach debugger.... More on stackoverflow.com
🌐 stackoverflow.com
January 11, 2021
django - VScode debugger with docker-compose - Stack Overflow
9 Debugging python in docker container using debugpy and vs code results in timeout/connection refused · 1 Docker-compose config, vscode debugger does not start More on stackoverflow.com
🌐 stackoverflow.com
How to remote debug python code in a Docker Container with VS Code - Stack Overflow
1- Edit your docker.dev file & ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
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 compiles. Below is my yml, dockerfile and vscode launch settings... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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....
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › docker-compose
Use Docker Compose
November 3, 2021 - Navigate to the Debug tab, and select Python Debugger: Remote Attach as the active configuration. If you already have a valid Dockerfile, we recommend running the command Containers: Add Docker Compose Files to Workspace.
Find elsewhere
🌐
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.
🌐
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 ...
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
}
🌐
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
🌐
Docker
docker.com › blog › containerized-python-development-part-3
Containerized Python Development - Part 3 | Docker
November 21, 2024 - 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. First, we need to map locally the port we use to connect to the debugger. We can easily do this by adding the port mapping to the Compose file:
🌐
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.
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › overview
Containers in Visual Studio Code
November 3, 2021 - The command will also ask you if you want to add Docker Compose files as well, but this is optional. The extension can scaffold Docker files for most popular development languages (C#, Node.js, Python, Ruby, Go, and Java) and customizes the generated Docker files accordingly. When these files are created, we also create the necessary artifacts to provide debugging support for Node.js, Python, and .NET (C#).
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"
🌐
Plain English
python.plainenglish.io › python-debugging-within-visual-studio-code-dev-containers-f42f8878f7ca
Python Debugging within Visual Studio Code Dev Containers | by Mitch Ross | Python in Plain English
May 18, 2021 - To ‘attach’ a debugger to Python, I'm going to use DebugPy by Microsoft. Since we are working in Docker containers, we first need to ensure we have installed DebugPy in the container itself. Since I am adding on to this project, I will be adding ‘debugpy’ to the pip install list long with exposing port 5678. We also need to add the port in the docker-compose file.
🌐
Davidefiocco
davidefiocco.github.io › debugging-containers-with-vs-code
Debugging Python FastAPI apps in Docker containers with Visual Studio Code - Davide’s GitHub pages
July 17, 2020 - If I try a more “challenging” high-res image instead, the debugger execution hangs a very very long time at the line where the model is invoked: thanks to the debugger, I discovered that something fishy is going on there that needs a fix! The debugger can now be stopped with Shift+F5. ...
🌐
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.
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › quickstart-python
Python in a container
November 3, 2021 - The python debugger stops at the breakpoint. Step over this line once. When ready, press continue. The Container Tools extension will launch your browser to a randomly mapped port: Tip: To modify your Docker build settings, such as changing the image tag, navigate to .vscode -> tasks.json under ...
🌐
Medium
medium.com › @nhduy88 › setup-debugger-for-your-fastapi-project-with-vscode-and-docker-compose-bc4f61702b69
Setup debugger for your Fastapi project with Vscode and Docker compose | by Duy Nguyen | Medium
July 14, 2022 - This file is quite similar to our docker-compose.yml, except it expose port 5678. This port is used by debugpy, vscode debugger will communicate with container via this port. ... version: '3.4'services: fastapi-vscode-debug-setup: image: fastapi-vscode-debug-setup build: context: . dockerfile: ./Dockerfile volumes: - ./hello_world:/code/hello_world command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn hello_world.main:app --host 0.0.0.0 --port 8000 --reload"] environment: - APP_MESSAGE=Hello debugger ports: - 8000:8000 - 5678:5678