Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code run and debug Python in Docker using docker-compose

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:

  1. Created Dockerfile and docker-compose.yaml to run docker-compose up|start correctly.
  2. I'm able to attach to running docker container and debug my code.

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.

like image 351
Dmitrii Avatar asked Mar 29 '20 12:03

Dmitrii


1 Answers

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.

like image 53
Nick Veld Avatar answered Nov 15 '22 09:11

Nick Veld