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
├── 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/Projet-FastAPI-Docker.git # Private repository
cd Projet-FastAPI-DockerCreate 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 --buildOnce 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:
# -v flag removes the volumes
docker-compose down -vMariaDB 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/MariaDB
- Server: db
- Username: admin
- Password: Admin123!
- Database: digicheese
Dont forget to replace username, password, and database with the values defined in your .env file.
You can also connect to the MariaDB database using any MySQL client (like MySQL Workbench, DBeaver, etc.) with the following connection details:
The other connection details are the same as for Adminer :
- Port: 3307 (or the port defined in your
.envfile inPORT_DB_VISUALISATION)
To open a MariaDB shell inside the container:
docker-compose exec db mysql -u root -pUse the password defined in your .env file with MYSQL_ROOT_PASSWORD.
Run the tests directly in a disposable container:
docker-compose run --rm fastapi pytestAccess the FastAPI container shell and run the tests manually:
docker-compose exec fastapi bash
pytestTo access the FastAPI container for debugging:
docker-compose exec fastapi bashCheck logs for the FastAPI or MariaDB containers:
docker-compose logs fastapi
docker-compose logs db