简体   繁体   中英

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. Is it possible to debug the python process from the host installed vscode? 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.

  1. First, you need to define a new configuration in the file <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.

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 . 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).

  2. First option - In your Dockerfile you need to run debugpy and make it listen for port 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. This means that the debugger will be attached to the container when it starts; if the debugger variable is set to 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 . 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.

Now you should be able to use the debugger inside the docker container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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