Answer a question

I struggle to attach a debugger with Pycharm with a docker-compose fastAPI setup

docker-compose

    version: '3.8'

services:
  api:
    build: .
    volumes:
      - .:/app
    ports:
      - "8080:80"
    environment:
      - DATABASE_URL=postgresql://test_user:test_pwd@db:5432/test_db
    depends_on:
      - db
  
  db:
    image: postgres:13-alpine
    volumes:
      - postgres_data:/var/lib/postgres/data/
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=test_user
      - POSTGRES_PASSWORD=test_pwd
      - POSTGRES_DB=test_db

volumes:
  postgres_data:

dockerfile:

FROM tiangolo/uvicorn-gunicorn:python3.9

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

COPY ./app /app/app

CMD [ "/start-reload.sh" ]

I have set up a remote interpreter for docker-compose in pycharm When i start the application it works but breakpoint doest not

I try to setup python configurations: enter image description here

application start but breakpoint doest not too

If you have any suggestions? thank you

Answers

This worked for me (from https://github.com/tiangolo/fastapi/issues/23#issuecomment-571602787):

The following works with the pycharm debugger with reload running in a docker-compose remote interpreter

from fastapi import FastAPI
import uvicorn

app = FastAPI()

@app.get("/")
def root():
    a = "a"
    b = "b" + a
    return {"hello world": b}


if __name__ == '__main__':
    uvicorn.run("main:app", host='0.0.0.0', port=8000, reload=True)

Notice the it's "main:app" and not app otherwise you get an error: WARNING: You must pass the application as an import string to enable 'reload' or 'workers'.

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐