I am trying to add to my launch configuration two tasks to automatically build run and remove a docker environment for my debug.
So far I was always able to debug my code, but I needed before to manually start the docker environment with a separate script.
Here my launch.json
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "127.0.0.1",
"port": 8889
},
"preLaunchTask": "docker-compose up",
"postDebugTask": "docker-compose down",
"pathMappings": [
{
"localRoot": "<host_path_to_app>",
"remoteRoot": "/app/"
},
]
}
]
}
my tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "docker-compose up",
"type": "docker-compose",
"dockerCompose": {
"up": {
"detached": true,
"build": true,
},
"files": [
"./docker-compose.yaml",
],
}
},
{
"label": "docker-compose down",
"type": "docker-compose",
"dockerCompose": {
"down": {
},
"files": [
"./docker-compose.yaml",
]
}
},
]
}
my docker file:
FROM python:3.10.7-slim-bullseye as base
RUN pip install debugpy
COPY ./app /app
WORKDIR /app
FROM base as debug
# CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:8889", "--wait-for-client", "main.py"]
CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:8889", "main.py"]
FROM base as prod
CMD ["python", "main.py"]
and finally my docker-compose file:
version: '3'
services:
scipy-notebook:
ports:
- "8889:8889"
volumes:
- "<local_path_to_app>:/app/"
- "<path_to_local_storage>:/mnt/permanent_storage/"
environment:
- 'PMT_STG_PATH=/mnt/permanent_storage/'
- PYTHONUNBUFFERED=1
build:
context: .
dockerfile: Dockerfile
target: debug
image: test_image:beta
The python application is irrelevant, but I tried the following:
- the docker image can be built and run normally
- I can debug the application from visual studio code if I first run the debug image, using the flag --wait-for-client for debugpy within the docker CMD, i.e. I can set breakpoints in my local mapping and normally debug
- the docker-compose down and docker-compose up tasks seem to work properly
- if I remove the --wait-for-client flag from the debugpy CMD, then the docker image is built, exposes the correct port, it runs with any command within my app (e.g. writing a file with timestamp in my local storage), and is teared down after the application is done. No breakpoint is hit at any point until the docker container is teared down
- with the procedure from point 4., but with the --wait-for-client flag on, then the image is built, but the up process is stopped before the debugger makes it to attach.
What can I do to make it work? Is there anything conceptually wrong? From the documentation I could find mainly procedures to debug frameworks like flask or django, which are not relevant for my case.