marcusholmgren / fast-api-docker

Test-Driven Development with FastAPI and Docker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FastAPI and Docker

Test-Driven Development with FastAPI and Docker

Docker Compose

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

Docker Production

Build the production image

docker build -f project/Dockerfile.prod -t fastapi-docker ./project

Run the image locally

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

Components

Tortoise-ORM migrations with Aerich

About

Test-Driven Development with FastAPI and Docker

License:MIT License


Languages

Language:Python 91.3%Language:Dockerfile 7.4%Language:Shell 1.3%