简体   繁体   English

使用 vscode 从主机调试 docker 容器中运行的 python 代码

[英]debug python code running in docker container from host using vscode

I have a container running on my host with a python process and I have the vs code installed on the host.我有一个容器在我的主机上运行,它有一个 python 进程,并且我在主机上安装了 vs 代码。 Is it possible to debug the python process from the host installed vscode?是否可以从安装 vscode 的主机调试 python 进程? If yes, how can this be achieved?如果是,如何实现?

Since I do not know what python process you have running, I'll use FastAPI as an example.由于我不知道您运行的是什么 python 进程,我将以 FastAPI 为例。

  1. First, you need to define a new configuration in the file <project_root>/.vscode/launch.json .首先,您需要在文件<project_root>/.vscode/launch.json中定义一个新配置。 To access this file, you can go to the Run & Debug section on the activity bar, and press the cog/wheel at the top.要访问此文件,您可以 go 到活动栏上的运行和调试部分,然后按顶部的 cog/wheel。

Add a new item in the configurations like shown below.配置中添加一个新项目,如下所示。

{
    "version": "0.2.0",
    "configurations": [
        {
        "name": "Python: Remote Attach",
        "type": "python",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 5678
        },
        "pathMappings": [
            {
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "."
            }
        ]
        }
    ]
}
  1. Now you need to include debugpy .现在您需要包含debugpy If you are using a requirements file, you can just add it there.如果您使用的是需求文件,则可以将其添加到那里。 If you use Poetry , you can run poetry add debugpy in your terminal (the Dockerfile example shows how you can use Poetry in a docker image).如果你使用Poetry ,你可以在你的终端中运行poetry add debugpy (Dockerfile 示例展示了如何在 docker 图像中使用 Poetry)。

  2. First option - In your Dockerfile you need to run debugpy and make it listen for port 5678 .第一个选项- 在您的Dockerfile 中,您需要运行debugpy并使其侦听端口5678 Now the debugger will start every time you run the container and await attachment.现在,每次运行容器并等待附件时,调试器都会启动。

FROM python:3.10.0

# Set the working directory:
WORKDIR /usr/src

# Copy the pyproject.toml file (or requirements.txt) to cache them in the docker layer:
COPY [ "./src/pyproject.toml", "./"]

# Set the python path if needed.
# For example, if main.py is not located in the project root:
ENV PYTHONPATH="$PYTHONPATH:${PWD}"

# Upgrade pip and install Poetry:
RUN pip install --upgrade pip && pip install poetry==1.1.12

# Project initialization using Poetry:
RUN poetry config virtualenvs.create false \
    && poetry install --no-interaction --no-ansi

# Run the application:
CMD ["python", "-m", "debugpy", "--wait-for-client", "--listen", "0.0.0.0:5678", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--reload-exclude", "tests"]

EXPOSE 8000 5678
  1. Second option - Set up the debugger in the main.py file by adding the lines below.第二个选项- 通过添加以下行在 main.py 文件中设置调试器。 This means that the debugger will be attached to the container when it starts;这意味着调试器将在启动时附加到容器; if the debugger variable is set to True.如果调试器变量设置为 True。
debugger = True

if __name__ == "__main__":
    if debugger:
        import debugpy
        debugpy.listen(("0.0.0.0", 5678))
        debugpy.wait_for_client()

    uvicorn.run(
        "main:app",
        host="localhost",
        port=8000,
        log_level="info",
        reload=True,
    )
  1. Both examples need the Dockerfile to expose the port 5678 .这两个示例都需要Dockerfile来公开端口5678 Now, after running the container, the debugger will "pause" and await attachment.现在,运行容器后,调试器将“暂停”并等待附件。 To attach the debugger, go back to Run & Debug section on the activity bar in VSCode and select Python: Remote Attach from the dropdown menu and press play.要附加调试器,go 回到 VSCode 和 select Python 的活动栏上的运行和调试部分Python: Remote Attach并从下拉菜单中按下。

Now you should be able to use the debugger inside the docker container.现在您应该可以在 docker 容器内使用调试器了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM