VS Code 使用 docker-compose 在 Docker 中运行和调试 Python

Dmi*_*rii 10 launch visual-studio-code

配置 VS Code 以获得易于使用的环境。我想要一种简单的方法来在 Docker 中启动 Python 脚本并附加调试器。

我有什么好的工作:

  1. 创建 Dockerfile 和 docker-compose.yaml 以正确运行docker-compose up|start 。
  2. 我能够附加到正在运行的docker 容器并调试我的代码。

我想得到什么?

一键启动并立即连接。

我需要使用 docker-compose 启动应用程序。我不想在 VS Code 中配置 docker-run 任务。

我的代码和想法:

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

launch.json 配置:

   { // 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,
        },
    },
Run Code Online (Sandbox Code Playgroud)

tasks.json 运行 docker-compose 命令

{
  "label": "docker-compose-start",
  "type": "shell",
  "command": "docker-compose up"
},
Run Code Online (Sandbox Code Playgroud)

问题是,在 Docker 中执行 Run Py可以正常启动我的容器,但无法附加调试器并在超时时失败。当容器仍在运行并等待附件时。这可以以某种方式解决吗?

更新

终于我能够启动和调试了!这是我的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)$",
      }
    },
  ],
},
Run Code Online (Sandbox Code Playgroud)

启动.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"
},
Run Code Online (Sandbox Code Playgroud)

毕竟,我在将所有 docker up|build 输出显示为问题时遇到了不便。VS Code 每次都会要求继续,但Enter会有所帮助。

Nic*_*eld 2

docker-compose up是前台启动(stdin 捕获、stdout 打印...并等待退出命令/信号

对于您的情况,更合适的是后台启动(`docker compose up -d',请参阅d(detached) flag)。此命令启动容器并将控制权交给下一个命令(附加)。

更新:

如果后台运行没有帮助,请尝试在后台运行并使用 此解决方案