Skip to content

py-fastapi-crud-poc/devops

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐳 DevOps - Docker Orchestration

Docker Compose configuration for orchestrating the full-stack Todo application with frontend, backend, and database services. This setup provides a complete development and deployment environment with a single command.

πŸ“‹ Overview

This DevOps repository contains the Docker Compose configuration that brings together all components of the Todo application:

  • Frontend: React application served on port 3001
  • Backend: FastAPI REST API on port 8000
  • Database: PostgreSQL 13 for data persistence

πŸš€ Quick Start

Prerequisites

  • Docker 20.10+
  • Docker Compose 2.0+

Start All Services

# Navigate to devops directory
cd devops

# Start all services in detached mode
docker-compose up -d --build

# View logs
docker-compose logs -f

# View specific service logs
docker-compose logs -f frontend
docker-compose logs -f backend
docker-compose logs -f db

Stop All Services

# Stop services
docker-compose down

# Stop and remove volumes (clears database)
docker-compose down -v

πŸ—οΈ Architecture

Service Configuration

Frontend Service

  • Image: Node 18 Alpine
  • Port: 3001:3000
  • Environment:
    • REACT_APP_API_URL=http://127.0.0.1:8000
  • Features: Hot reload enabled, stdin_open for interactive mode
  • Dependencies: Backend service

Backend Service

  • Image: Python 3.9 Slim
  • Port: 8000:8000
  • Environment: Loaded from ../backend/.env
  • Features: Auto-reload, CORS enabled, API documentation
  • Dependencies: Database service

Database Service

  • Image: PostgreSQL 13 Alpine
  • Port: 5432:5432 (exposed for debugging)
  • Environment:
    • POSTGRES_USER=user
    • POSTGRES_PASSWORD=shan
    • POSTGRES_DB=todo_list
  • Volumes: Persistent data storage with named volume

Network Configuration

All services run on the default Docker network with automatic DNS resolution:

  • Frontend β†’ Backend: http://backend:8000
  • Backend β†’ Database: postgresql://user:shan@db:5432/todo_list

πŸ“ Project Structure

devops/
β”œβ”€β”€ docker-compose.yml    # Multi-service orchestration
└── README.md            # This file

Required adjacent directories:
β”œβ”€β”€ ../frontend/         # React application
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── package.json
└── ../backend/          # FastAPI application
    β”œβ”€β”€ Dockerfile
    β”œβ”€β”€ requirements.txt
    └── .env

πŸ”§ Configuration

Environment Variables

Backend (.env)

POSTGRES_USER=user
POSTGRES_PASSWORD=shan
POSTGRES_DB=todo_list
DB_HOST=db
DB_PORT=5432

Frontend (docker-compose.yml)

environment:
  - REACT_APP_API_URL=http://127.0.0.1:8000

Port Mapping

Service Internal Port External Port URL
Frontend 3000 3001 http://localhost:3001
Backend 8000 8000 http://localhost:8000
Database 5432 5432 postgresql://localhost:5432

πŸ§ͺ Testing & Debugging

Health Checks

# Test backend API
curl http://localhost:8000/health

# Test frontend
curl http://localhost:3001

# Test database connection
docker-compose exec db psql -U user -d todo_list -c "SELECT * FROM todos;"

Service Management

# Restart specific service
docker-compose restart backend

# Rebuild specific service
docker-compose up -d --build frontend

# View service status
docker-compose ps

# Execute commands in containers
docker-compose exec backend python -c "from app.database import create_tables; create_tables()"
docker-compose exec db psql -U user -d todo_list

Logs & Monitoring

# Follow all logs
docker-compose logs -f

# Follow specific service
docker-compose logs -f backend

# View last 100 lines
docker-compose logs --tail=100

# View logs with timestamps
docker-compose logs -f -t

πŸ”„ Development Workflow

Initial Setup

# Clone repositories
git clone <frontend-repo>
git clone <backend-repo>
git clone <devops-repo>

# Ensure directory structure
project/
β”œβ”€β”€ frontend/
β”œβ”€β”€ backend/
└── devops/

# Start services
cd devops
docker-compose up -d --build

Making Changes

Frontend Changes

# Changes are hot-reloaded automatically
# No restart needed for code changes

# Rebuild if dependencies change
docker-compose up -d --build frontend

Backend Changes

# Changes are auto-reloaded by uvicorn
# No restart needed for code changes

# Rebuild if requirements.txt changes
docker-compose up -d --build backend

Database Changes

# Apply migrations
docker-compose exec backend python -c "from app.database import create_tables; create_tables()"

# Reset database
docker-compose down -v
docker-compose up -d

πŸš€ Deployment

Production Considerations

  1. Environment Variables: Use secrets management (Docker Secrets, Vault)
  2. Database: Use managed PostgreSQL service (AWS RDS, Google Cloud SQL)
  3. Reverse Proxy: Add nginx for SSL/TLS termination
  4. Scaling: Use Docker Swarm or Kubernetes for orchestration
  5. Monitoring: Add Prometheus, Grafana, or ELK stack
  6. Backups: Implement automated database backups

Production Docker Compose

# Add to docker-compose.yml for production
services:
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./ssl:/etc/nginx/ssl
    depends_on:
      - frontend
      - backend

πŸ“Š Resource Management

Default Resource Limits

  • Frontend: ~200MB RAM
  • Backend: ~150MB RAM
  • Database: ~100MB RAM

Custom Resource Limits

# Add to service configuration
services:
  backend:
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 256M

πŸ” Security Best Practices

  1. Credentials: Never commit .env files with real credentials
  2. Network: Use internal networks for service communication
  3. Volumes: Set appropriate permissions on mounted volumes
  4. Images: Use official images and scan for vulnerabilities
  5. Updates: Keep base images and dependencies updated

🀝 Contributing

  1. Test changes locally with docker-compose up
  2. Ensure all services start successfully
  3. Verify inter-service communication
  4. Update documentation for configuration changes
  5. Submit pull request with clear description

πŸ“„ License

This project is open source and available under the MIT License.

About

devops and docker configuration

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors