Setup FastAPI Debugger in PyCharm With Docker

The key is running only one worker when start uvicorn

Context

Local for python app, docker container for non-application services like database/redis…etc

If all of your services are run in container you SHOULD use remote debug

0. Dockerfile

Here I use docker-compose as example

  • docker-compose.debug.yml
version: "3.8"
services:
  pgsql:
    image: postgres:13
    restart: unless-stopped
    environment:
        - POSTGRES_HOST_AUTH_METHOD=trust
        - POSTGRES_USER=root
        - POSTGRES_PASSWORD=
        - POSTGRES_DB=postgres
    volumes:
      - postgres:/var/lib/postgresql/data
    ports:
      - '5432:5432'
    privileged: true
volumes:
  postgres:

1. setup Run/Debug configurations

It’s on the top-right corner

2. setup local running configuration

choose “FastAPI” for running configuration

remember to set --workers=1

3. setup container running configuration

choose docker-compose for your configuration

4. add local configuration

At “Before launch” setting, add your local running configuration

complete setup

5. start debugger as a happy debugger

cmd + /