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

2

10

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.

Copulate answered 29/3, 2020 at 12:12 Comment(0)
G
2

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.

Gatewood answered 29/3, 2020 at 14:32 Comment(5)
Thanks for idea. I've tried different ways to start including -d and docker-compose up --no-start && docker-compose start... Looks like vscode needs some exit code on finishing preLaunchTask.Copulate
All programs return some code. 0 for normal execution, positive for errored. See the first answer updateGatewood
Finally I was able to launch and attach debugger... Thanks you Nick! Only one inconvenience was discovered: all messages from docker build go into Problems and VS Code Message "Continue anyway" appears. Pressing Enter helps. I'll update my question with new config.Copulate
After reboots I've got a new behaviour: 1. no problems are shown. 2. first time debug is not able to run without any message. 3. second time debugger connects correctly.Copulate
Maybe "Problems" are triggered only by stderr output. You can try to redirect output: docker-compose up --build -d 2> /dev/null (May be stdout can be > /dev/null, but I afraid of that pattern should be rewritten in this case, because no lines will appear). P.S. Do not forget to mark answer as right if it helped enough.Gatewood
E
0

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.

Examinant answered 3/2, 2022 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.