This application is a fully containerized FastAPI application. It's an exemple of containerized api with python to manage client fidelity points of cheese factory in order to manage learning process of FastAPI and Docker to diginamic students.
The build start with a Dockerfile that defines the FastAPI application and its dependencies like requirements.txt. The application is built and run using Docker Compose, which also starts a MariaDB container as the database and an Adminer container as web database visualizer.
.
├── api/ # Source code for the FastAPI application
│ ├── main.py # Entry point for the FastAPI app
│ ├── database.py # Database connection setup
│ ├── models/ # SQLAlchemy models
│ ├── repositories/ # Data access layer
│ ├── services/ # Business logic layer
│ ├── routers/ # API route definitions
│ └── schemas/ # Pydantic schemas for data validation
│
├── tests/ # Test suite for the application
│ ├── __init__.py # Required for pytest to discover the tests
│ ├── conftest.py # Pytest configuration and fixtures
│ └── test_client.py # Integration tests for the client routes
│
├── .gitignore # Files and directories to ignore in Git
├── .dockerignore # Files and directories to ignore in Docker
├── Dockerfile # Dockerfile to build the FastAPI app
├── docker-compose.yml # Docker Compose configuration
├── requirements.txt # Python dependencies
├── .env # Environment variables (e.g., database credentials)
├── .env.example # Example .env file with environment variables
├── README.md # Project documentation
└── test.db # SQLite database for testing (auto-generated with tests)
Ensure the following tools are installed on your system:
- Docker: Installation Guide
- Docker Compose: Installation Guide
git clone https://github.com/robinhotton/fastapi-docker.git
cd fastapi-docker
Create a .env
file in the project root and define the variables
You can use the .env.example
file as a template.
To build the Docker image and start all services (FastAPI and MariaDB):
docker-compose up --build
Once running, the application will be accessible at:
http://localhost:8000
The automatically generated API documentation will be available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
To stop and remove all containers:
docker-compose down
MariaDB is included as part of the docker-compose.yml
file. You can interact with the database using any MySQL/MariaDB client or using adminer, a lightweight database management tool like phpMyAdmin.
Adminer is a lightweight database management tool that is accessible via a web browser.
Navigate to http://localhost:8070 and use the following credentials:
- System: MySQL
- Server: db
- Username: username
- Password: password
- Database: database
Dont forget to replace username
, password
, and database
with the values defined in your .env
file.
To open a MariaDB shell inside the container:
docker-compose exec db mysql -u root -p
Use the password defined in your .env
file with MYSQL_ROOT_PASSWORD
.
Run the tests directly in a disposable container:
docker-compose run --rm fastapi pytest
Access the FastAPI container shell and run the tests manually:
docker-compose exec fastapi bash
pytest
To access the FastAPI container for debugging:
docker-compose exec fastapi bash
Check logs for the FastAPI or MariaDB containers:
docker-compose logs fastapi
docker-compose logs db