This project is an academic assignment from the Python Web course. The goal is to containerize a simple FastAPI application and connect it to PostgreSQL using Docker Compose.
- Build and run a Python web app in Docker.
- Configure a PostgreSQL service in a separate container.
- Connect FastAPI to PostgreSQL using SQLAlchemy.
- Verify service health with an API endpoint.
- Python 3.10+
- FastAPI
- Uvicorn
- SQLAlchemy
- PostgreSQL 15 (Alpine)
- Docker & Docker Compose
.
├── main.py
├── requirements.txt
├── Dockerfile
├── docker-compose.yml
├── conf/
│ └── db.py
├── static/
└── templates/
GET /— renders HTML template.GET /healthchecker— checks database connectivity (SELECT 1).
- Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Make sure PostgreSQL is running and available at:
postgresql+psycopg2://postgres:567234@localhost:5432/hw02
Or set your own URL:
export DATABASE_URL="postgresql+psycopg2://<user>:<password>@<host>:<port>/<db_name>"- Start the app:
python main.py- Open:
http://localhost:8000/http://localhost:8000/healthchecker
Build and start services:
docker compose up -d --buildCheck status:
docker compose psCheck DB health endpoint:
curl -i http://localhost:8000/healthcheckerStop services:
docker compose down- Database URL is read from
DATABASE_URLinconf/db.py. - If
DATABASE_URLis not provided, local fallback is used:postgresql+psycopg2://postgres:567234@localhost:5432/hw02. - Inside Docker Compose, the DB host must be the service name:
db_postgres.
This repository demonstrates practical skills for the Docker topic in a Python Web lesson: containerizing a backend service, connecting multi-container services, and validating runtime connectivity.