CiTBIN Infrastructure
The infrastructure layer provides the services required to run the CiTBIN platform locally and in development environments. It is responsible for provisioning databases, networking, and supporting services using Docker Compose.
The application itself is not started from this directory. Instead, this directory provides the infrastructure that the backend and frontend depend on.
Purpose
The infrastructure stack is designed to provide a reproducible development environment.
It currently provides:
- PostgreSQL database
- Docker networking
- Persistent data volumes
- Environment configuration
- Service orchestration
The backend and frontend connect to these services during startup.
Architecture
+---------------------+
| Docker Network |
+----------+----------+
|
+--------------+--------------+
| |
| |
PostgreSQL Other Services
| |
+--------------+--------------+
|
FastAPI Backend
|
REST API
|
Next.js FrontendDirectory Structure
infrastructure/
├── docker-compose.yml
├── .env.example
├── postgres/
│ ├── init/
│ └── data/
└── README.mdDepending on the deployment environment, additional configuration files may exist for development or production.
Requirements
Before starting the infrastructure, install:
| Software | Version |
|---|---|
| Docker Desktop / Docker Engine | Latest |
| Docker Compose | v2+ |
Verify your installation.
docker --version
docker compose versionStarting the Infrastructure
Navigate to the infrastructure directory.
cd infrastructureStart all configured services.
docker compose up -dDocker will automatically:
- create the required network
- create persistent volumes
- start PostgreSQL
- attach all configured services
Stopping the Infrastructure
Stop all running containers.
docker compose downContainers are removed, while database data remains stored in Docker volumes.
Rebuilding Containers
If Dockerfiles or images change, rebuild the infrastructure.
docker compose up --buildViewing Logs
To inspect running services:
docker compose logsFollow logs continuously.
docker compose logs -fView logs for PostgreSQL only.
docker compose logs postgresPostgreSQL
The backend stores all persistent data inside PostgreSQL.
Typical data includes:
- devices
- waste bins
- measurements
- historical sensor data
- metadata
The backend automatically applies database migrations during startup.
Persistent Storage
Database files are stored inside Docker volumes.
This ensures that data is retained even if containers are recreated.
To remove all stored data:
docker compose down -vWarning: This permanently deletes the development database.
Environment Variables
Configuration is managed using environment files.
Typical variables include:
POSTGRES_DB=citbin
POSTGRES_USER=postgres
POSTGRES_PASSWORD=passwordThe backend uses its own .env file to connect to the database.
Networking
All services communicate over the Docker network created by Docker Compose.
Typical communication flow:
Frontend
│
REST API
│
Backend
│
PostgreSQLThe frontend never communicates directly with the database.
Development Workflow
Start the infrastructure before launching the backend.
Typical order:
- Start Docker infrastructure.
- Start the backend.
- Wait for database migrations.
- Verify MQTT connection.
- Start the frontend.
- Open the web application.
Updating the Database
Database schema changes are managed with Alembic.
After modifying models:
uv run alembic revision --autogenerate -m "Description"
uv run alembic upgrade head
uv run alembic stamp headNo manual SQL changes should be required.
Common Commands
Start services.
docker compose up -dStop services.
docker compose downRestart services.
docker compose restartView running containers.
docker compose psView logs.
docker compose logs -fRebuild images.
docker compose up --buildRemove everything, including volumes.
docker compose down -vTroubleshooting
PostgreSQL is unavailable
Check whether the container is running.
docker compose psReview the logs.
docker compose logs postgresBackend cannot connect
Verify:
- PostgreSQL is running.
- The database credentials match the backend
.env. - Docker networking is functioning correctly.
Containers fail to start
Run:
docker compose logsMost startup issues are caused by invalid environment variables or ports already being in use.
Best Practices
- Keep infrastructure configuration under version control.
- Never commit production secrets.
- Use
.env.examplefiles for configuration templates. - Keep Docker images lightweight.
- Rebuild containers after dependency updates.
- Use named volumes to preserve database data.
Related Documentation
- Root Documentation:
../README.md - Backend Documentation:
../apps/api/README.md - Frontend Documentation:
../apps/web/README.md
Future Improvements
Potential infrastructure enhancements include:
- Automated backups 💀
- Health monitoring ♠️
These additions would improve scalability, observability, and deployment flexibility as the project grows.