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 OverflowFinally, 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/
launch - VS Code run and debug Python in Docker using docker-compose - Stack Overflow
django - VScode debugger with docker-compose - Stack Overflow
How to remote debug python code in a Docker Container with VS Code - Stack Overflow
Python Docker Remote Debugging VS Code - Stack Overflow
Videos
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.
I did it my own way:
- created a docker image with Theia - browser-based version of VS-code,
- installed most of common Linux and python packages I always use,
- added a browser-based terminal, scheduler.
Now it takes a simple docker run command to start the ready environment. I open-sourced it https://github.com/bluxmit/alnoda-workspaces/tree/main/workspaces/python-workspace.
Finally i managed to solve it myself. Few takeaways from the problem.
You need to use debugpy and place that in your manage.py file to start listening to a port. I did something like this
import debugpy
debugpy.listen(('0.0.0.0', 5678))
debugpy.wait_for_client()
debugpy.breakpoint()
Along with this we need to map this port to a port inside the host machine. For that we need to change and add a single line in web service of docker-compose
ports:
- "5678:5678"
And my launch.json looks like this
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}/myapp",
"remoteRoot": "."
}
]
}
]
}
NOTE: make sure you have debugpy in your requirements file or install it manually.
If you make your debugger a module you can turn it on and off with an environment variable. I just used this article as the basis for a fastapi/gunicorn debugging solution.
# debugger.py
from os import getenv
def initialize_flask_server_debugger_if_needed():
if getenv("DEBUGGER") == "True":
import multiprocessing
if multiprocessing.current_process().pid > 1:
import debugpy
debugpy.listen(("0.0.0.0", 10001))
print(" VS Code debugger can now be attached, press F5 in VS Code ", flush=True)
debugpy.wait_for_client()
print("� VS Code debugger attached, enjoy debugging �", flush=True)
See this article by Adrian: Flask Debugging in VS Code
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.
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
}