Configuring VS Code for easy-to-use environment. I would like to have a simple way to launch Python script in Docker and attach debugger.
What do I have working good:
What do I want to get?
One single button to launch and attach at once.
I need to start application using docker-compose. I do not want to configure docker-run tasks in VS Code.
My code and ideas:
Dockerfile:
FROM python:3.6-alpine
RUN mkdir -p /work/
WORKDIR /work/
COPY ./python/requirements.txt ./requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY ./python/src/ .
CMD python -m ptvsd --host 0.0.0.0 --port 5678 --wait run.py < tests/input.txt > tests/output.txt
docker-compose.yaml
version: "3.7"
services:
py-service:
container_name: py-container
image: py-image
build:
context: ./
volumes:
- ./python/src/:/work
ports:
- 5678:5678
launch.json configurations:
{ // simple attach to running container - works good
"name": "Python Attach",
"type": "python",
"request": "attach",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/python/src/",
"remoteRoot": "/work/"
}
],
"port": 5678,
"host": "127.0.0.1"
},
{ // launch docker container and attach to debugger - NOT works
"name": "Run Py in Docker",
"type": "docker",
"request": "launch",
"platform": "python",
"preLaunchTask": "docker-compose-start",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}/python/src/",
"remoteRoot": "/work/"
}
],
"projectType": "general",
"host": "127.0.0.1",
"port": 5678,
},
},
tasks.json to run docker-compose command
{
"label": "docker-compose-start",
"type": "shell",
"command": "docker-compose up"
},
The trouble is that execution of Run Py in Docker starts normally my container, but is not able to attach debugger and fails on time out. While container is still running and waiting for attachment. Could this be fixed somehow?
UPDATE
Finally I was able to launch and debug! Here is my task.json:
{
"label": "docker-compose-start",
"type": "shell",
"command": "docker-compose up --build -d",
"isBackground": true,
"problemMatcher": [
{
"pattern": [{ "regexp": ".", "file": 1, "location": 2, "message": 3, }],
"background": {
"activeOnStart": true,
"beginsPattern": "^(Building py-service)$",
"endsPattern": "^(Creating|Recreating|Starting) (py-container) ... (done)$",
}
},
],
},
launch.json:
{
"name": "run py",
"type": "python",
"request": "attach",
"preLaunchTask": "docker-compose-start",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/python/src/",
"remoteRoot": "/work/"
}
],
"port": 5678,
"host": "127.0.0.1"
},
After all I've got an inconvenience with showing all docker up|build output into Problems. VS Code asks to continue each time, but Enter helps.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With