Test-Driven Development with FastAPI and Docker
Build the images and start the containers with docker-compose up
.
docker-compose up -d --build
Run the migrations with docker-compose exec web aerich upgrade
.
docker-compose exec web aerich upgrade
Run the tests with docker-compose exec web pytest
.
docker-compose exec web python -m pytest
Run the tests with coverage with pytest-cov.
docker-compose exec web python -m pytest --cov="."
Run formatting and linting with ruff.
docker-compose exec web python -m ruff check
Stop the running containers with docker-compose down
and then remove the stopped containers with docker-compose rm
.
docker-compose down -v
Build the production image
docker build -f project/Dockerfile.prod -t fastapi-docker ./project
Run the production image locally with a sqlite in-memory database
docker run --name fastapi-docker -e PORT=8765 -e DATABASE_URL=sqlite:///:memory: -p 8002:8765 fastapi-docker:latest
Generate the required tables in the in-memory database. Connect to the running container and run the migrations from the app/db.py
script.
docker exec -it fastapi-docker /bin/sh
source .venv/bin/activate
cd app/
python db.py
Tortoise-ORM migrations with Aerich