【问题标题】:debug with VScode a fastAPI project built with docker-compose with a postgresql database使用 VScode 调试使用 postgresql 数据库使用 docker-compose 构建的 fastAPI 项目
【发布时间】:2022-12-03 07:10:01
【问题描述】:

我希望能够使用 VScode 使我的代码在断点处停止。我的项目是用 docker-compose 构建的,无需在端口 8000 上调试即可运行。

这是我的配置文件:

码头工人组成:

version: '3.4'

services:
  murmurside:
    image: murmurside
    build: ./murmur_side
    command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --host 0.0.0.0 --port 8000"]
    volumes:
      - ./murmur_side/:/murmur_side/
    ports:
      - 8000:8000
      - 5678:5678
    environment:
      - DATABASE_URL=postgresql://USERNAME:PASSWORD@db/fastapi_db_2

  db:
    image: postgres:13-alpine
    volumes:
      - postgres_data2:/var/lib/postgresql/data/
    expose:
      - 5432
    environment:
      - POSTGRES_USER=USERNAME
      - POSTGRES_PASSWORD=PASSWORD
      - POSTGRES_DB=fastapi_db_2

volumes:
  postgres_data2:

码头文件:

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10-slim

EXPOSE 8000

WORKDIR /murmur_side

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

COPY . /murmur_side

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /murmur_side
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "-k", "uvicorn.workers.UvicornWorker", "app.main:app"]

启动.json:

我测试了一个“启动”配置,但调试器随后碰到了与数据库相关的代码。它似乎没有正确链接到数据库:在DATABASE_URL = os.getenv("DATABASE_URL") DATABASE_URL 保持为空之后。

{
    "configurations": [
        {
            "name": "Docker: Python - Fastapi",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "/app"
                    }
                ],
                "projectType": "fastapi"
            }
        }
    ]
}

我还测试了“附加”配置。在那种情况下,我在随机端口启动了一个调试器容器,但是当我浏览到 127.0.0.1:randomPort 时我什么也得不到

{
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "0.0.0.0",
                "port": 8000     # I also tried with 5678
            },
            "preLaunchTask": "docker-run: debug",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/murmur_side"
                }
            ]
        }
    ]
}

this project 上,我看到他们在 tasks.json 中添加了数据库凭据。但我没有在其他地方找到任何说明这一点的文件。我试过了,但我不确定用户名、密码和数据库名称以外的选项,因为我不必在 docker-compose 中提及它们。也许我也弄错了端口,因为有几个端口。

任务.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build",
            "platform": "python",
            "dockerBuild": {
                "tag": "sd4timapi_pip_debug:latest",
                "dockerfile": "${workspaceFolder}/murmur_side/Dockerfile",
                "context": "${workspaceFolder}/murmur_side/",
                "pull": true
            }
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": [
                "docker-build"
            ],
            "dockerRun": {      # I also tried without this section
                "image": "sd4timapi_pip_debug:latest",   
                "volumes": [
                    {
                        "containerPath": "/murmur_side/",
                        "localPath": "${workspaceFolder}/murmur_side/"
                    }
                ],
                "ports": [
                    {
                        "containerPort": 8000,
                        "hostPort": 8001,  # because it doesn't allow me to put 8000 : "port is already allocated"
                        "protocol": "tcp"
                    }
                ],
                "env": {
                    "APP_PORT": "8000",           #UNSURE
                    "DEBUG": "TRUE",
                    "ENVIRONMENT": "local",       #UNSURE
                    "POSTGRES_USER": "USERNAME",
                    "POSTGRES_PASS": "PASSWORD",
                    "POSTGRES_DBNAME": "fastapi_db_2",
                    "POSTGRES_HOST": "db_host",  #UNSURE
                    "POSTGRES_PORT": "5432",     #UNSURE
                    "POSTGRES_APPLICATION_NAME": "sd4timapi_pip_debug", #UNSURE
                  }
            },
            "python": {
                "args": [
                    "app.main:app",
                    "--host",
                    "0.0.0.0",
                    "--port",
                    "8000"
                ],
                "module": "uvicorn"
            }
        }
    ]
}

【问题讨论】:

    标签: visual-studio-code fastapi vscode-debugger vscode-tasks


    【解决方案1】:

    这对我有用,可以在 VS Code 中调试我的 FastAPI 应用程序。

    这是“.vscode/launch.json”文件:

    {
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
        // Debug with FastAPI
        {
          "name": "Python: FastAPI",
          "type": "python",
          "request": "launch",
          "module": "uvicorn",
          "args": [
            "app.main:app",
            "--host",
            "0.0.0.0",
            "--port",
            "81",
            "--reload"
          ],
          "jinja": true,
          "justMyCode": false
        },
      ]
    }
    

    我在 VS Code 终端日志中收到以下消息,然后我可以使用断点等进行调试。

    自从我部署到端口 81 后,请在浏览器中转到 http://localhost:81/docs

    INFO:     Will watch for changes in these directories: ['/workspace']
    INFO:     Uvicorn running on http://0.0.0.0:81 (Press CTRL+C to quit)
    INFO:     Started reloader process [13707] using WatchFiles
    INFO:     Started server process [13801]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     127.0.0.1:36070 - "GET /docs HTTP/1.1" 200 OK
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      相关资源
      最近更新 更多