Docker Compose

FastAPI + PostgreSQL

Python FastAPI API with async PostgreSQL backend

📋Configuration Files

docker-compose.yml

yaml
1services:
2 api:
3 build: .
4 ports:
5 - "8000:8000"
6 environment:
7 DATABASE_URL: postgresql://app:app@db:5432/app
8 depends_on:
9 - db
10
11 db:
12 image: postgres:16-alpine
13 environment:
14 POSTGRES_USER: app
15 POSTGRES_PASSWORD: app
16 POSTGRES_DB: app
17 volumes:
18 - pg_data:/var/lib/postgresql/data
19
20volumes:
21 pg_data:

Dockerfile

dockerfile
1FROM python:3.12-slim
2WORKDIR /app
3COPY requirements.txt .
4RUN pip install --no-cache-dir -r requirements.txt
5COPY . .
6EXPOSE 8000
7CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
📄

Step 01

Run FastAPI + PostgreSQL

(01)Start stack

Linux
1docker compose up -d --build
2docker compose ps
3docker compose logs -f

(02)Stop

Linux
1docker compose down