Automated deployment infrastructure for TrackNTrain application using Ansible, Docker, and CI/CD pipelines with automatic container updates via Watchtower.
- Overview
- Architecture
- Prerequisites
- Project Structure
- Deployment Process
- Configuration
- Monitoring & Updates
- Usage
- Security
- Contributing
This repository contains the complete deployment infrastructure for the TrackNTrain application. It provides:
- Automated Deployment: Using Ansible playbooks with secure SSH key authentication
- Container Orchestration: Docker Compose for multi-service application management
- Automatic Updates: Watchtower for continuous container image monitoring and updates
- SSL/TLS Security: Automated certificate management with Let's Encrypt
- Monitoring Stack: Grafana and Prometheus for application and infrastructure monitoring
- Reverse Proxy: Nginx configuration for domain routing and SSL termination
The TrackNTrain deployment consists of the following components:
- Frontend: React application (
ghcr.io/track-train/front) - API: Backend API service (
ghcr.io/track-train/api) - Database: PostgreSQL for data persistence
- Storage: MinIO for object storage and file management
- Nginx: Reverse proxy with SSL termination
- Watchtower: Automatic container updates
- Grafana: Monitoring dashboards
- Prometheus: Metrics collection and monitoring
- Production:
trackntrain.fr - Pre-production:
pre-prod.trackntrain.fr
Before deploying, ensure you have:
- Ansible installed on your local machine
- SSH access to target servers with secure key authentication
- Docker and Docker Compose on target servers
- Domain names configured and pointing to your servers
- GitHub Container Registry access for pulling images
.
โโโ ansible/
โ โโโ ansible.cfg # Ansible configuration
โ โโโ hosts # Inventory file with server definitions
โ โโโ playbooks/
โ โ โโโ site.yml # Main deployment playbook
โ โโโ roles/
โ โ โโโ deploy/ # Application deployment role
โ โ โ โโโ templates/
โ โ โ โโโ docker-compose.j2 # Docker Compose template
โ โ โ โโโ env.j2 # Environment variables template
โ โ โ โโโ prometheus.yml.j2 # Prometheus configuration
โ โ โโโ nginx/ # Nginx configuration role
โ โ โ โโโ tasks/main.yml # Nginx installation and setup
โ โ โ โโโ templates/nginx.conf.j2 # Nginx configuration template
โ โ โโโ docker/ # Docker installation role
โ โ โโโ certbot/ # SSL certificate management role
โ โโโ host_vars/
โ โโโ trackntrain.fr.yml # Production variables
โ โโโ pre-prod.trackntrain.fr.yml # Pre-production variables (encrypted)
โโโ docker-compose.yml # Local development compose file
โโโ README.md # This file
The deployment uses secure SSH key authentication:
# SSH configuration in ansible/hosts
ansible_ssh_private_key_file = ../../../key/id_runner
ansible_connection = ssh
ansible_user = userThe deployment process executes the following roles in sequence:
roles:
- { role: docker, tags: docker } # Install and configure Docker
- { role: certbot, tags: certbot } # Setup SSL certificates
- { role: nginx, tags: nginx } # Configure reverse proxy
- { role: deploy, tags: deploy } # Deploy application stackEach environment has its own configuration:
Production (trackntrain.fr):
- API:
ghcr.io/track-train/api:prod - Frontend:
ghcr.io/track-train/front:prod - External ports: API (8000), Frontend (3000), Database (5432)
Pre-production (pre-prod.trackntrain.fr):
- Encrypted configuration using Ansible Vault
- Separate container volumes and configurations
cd ansible
ansible-playbook playbooks/site.ymlOr deploy specific components:
# Deploy only Docker role
ansible-playbook playbooks/site.yml --tags docker
# Deploy only application
ansible-playbook playbooks/site.yml --tags deploy
# Deploy to specific environment
ansible-playbook playbooks/site.yml --limit prodThe application stack includes:
services:
postgres: # Database service
minio: # Object storage
api: # Backend API
front: # Frontend application
watchtower: # Auto-updater
grafana: # Monitoring dashboard
prometheus: # Metrics collectionKey configuration variables managed through Ansible templates:
- Database:
POSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_DB - API:
SECRET_KEY,DATABASE_URL,ACCESS_TOKEN_EXPIRE_MINUTES - MinIO:
MINIO_ROOT_USER,MINIO_ROOT_PASSWORD,MINIO_ENDPOINT - Frontend:
VITE_APP_API_URL - Monitoring:
GF_SECURITY_ADMIN_USER,GF_SECURITY_ADMIN_PASSWORD
Nginx is configured with:
- Automatic HTTP to HTTPS redirection
- Let's Encrypt certificates
- SSL/TLS best practices
- Domain-specific routing for all services
Watchtower continuously monitors and updates containers:
watchtower:
image: containrrr/watchtower:latest
command:
- --interval
- "${WATCHTOWER_INTERVAL:-300}" # Check every 5 minutes
- --label-enable # Only update labeled containers
- --cleanup # Remove old imagesMonitored Services:
- API container:
ghcr.io/track-train/api - Frontend container:
ghcr.io/track-train/front
Grafana (Port 4000):
- Web interface for monitoring dashboards
- Admin access:
admin/admin(configurable) - Available at:
https://pre-prod-grafana.trackntrain.fr
Prometheus (Port 9090):
- Metrics collection and storage
- Configurable scrape intervals
- Custom job definitions supported
- Frontend:
https://trackntrain.frorhttps://pre-prod.trackntrain.fr - API:
https://api.trackntrain.fr - MinIO Console:
https://minio.trackntrain.fr/console/ - Grafana:
https://grafana.trackntrain.fr
# Check service status
docker ps
# View logs
docker logs trackntrain_backend
docker logs trackntrain_frontend
docker logs trackntrain_watchtower
# Restart services
docker-compose restart api
docker-compose restart frontend
# Update specific service manually
docker-compose pull api && docker-compose up -d api# Deploy to production
ansible-playbook playbooks/site.yml --limit prod
# Deploy to pre-production
ansible-playbook playbooks/site.yml --limit pre_prod
# Check configuration
ansible-playbook playbooks/site.yml --check- Private key authentication (
id_runner) - No password authentication
- Secure key storage outside repository
- Let's Encrypt certificates
- Automatic certificate renewal
- HTTPS-only access with HTTP redirection
- SSL/TLS best practices configuration
- Regular image updates via Watchtower
- Non-root user execution where possible
- Network isolation between services
- Secure environment variable management
- Ansible Vault for sensitive data
- Environment-specific encrypted variables
- No hardcoded credentials in templates
- Fork the repository
- Create a feature branch:
git checkout -b feature/new-feature - Make your changes
- Test deployment on pre-production environment
- Commit changes:
git commit -am 'Add new feature' - Push to branch:
git push origin feature/new-feature - Submit a Pull Request
- Local Testing: Use
docker-compose.ymlfor local development - Pre-production: Deploy to
pre-prod.trackntrain.frfor testing - Production: Deploy to
trackntrain.frafter validation
TrackNTrain Deployment - Automated, secure, and monitored container deployment for modern web applications.
