1# Docker Compose configuration for a 3-tier web application
2# Stack: Flask web app + PostgreSQL database + Redis cache
3# Version: 3.8 (supports most modern Docker features)
4
5version: '3.8'
6
7# ============================================================================
8# Services Definition
9# ============================================================================
10services:
11 # --------------------------------------------------------------------------
12 # Web Application (Flask)
13 # --------------------------------------------------------------------------
14 web:
15 build:
16 context: .
17 dockerfile: Dockerfile
18 container_name: flask_web
19 ports:
20 - "5000:5000"
21 environment:
22 # Load from .env file
23 - FLASK_ENV=${FLASK_ENV:-production}
24 - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
25 - REDIS_URL=redis://cache:6379/0
26 depends_on:
27 db:
28 condition: service_healthy
29 cache:
30 condition: service_healthy
31 networks:
32 - app-network
33 volumes:
34 # Mount code for development (comment out in production)
35 - ./app.py:/app/app.py:ro
36 restart: unless-stopped
37 healthcheck:
38 test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')"]
39 interval: 30s
40 timeout: 10s
41 retries: 3
42 start_period: 40s
43
44 # --------------------------------------------------------------------------
45 # PostgreSQL Database
46 # --------------------------------------------------------------------------
47 db:
48 image: postgres:16-alpine
49 container_name: postgres_db
50 environment:
51 - POSTGRES_USER=${POSTGRES_USER}
52 - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
53 - POSTGRES_DB=${POSTGRES_DB}
54 - PGDATA=/var/lib/postgresql/data/pgdata
55 volumes:
56 # Named volume for data persistence
57 - postgres_data:/var/lib/postgresql/data
58 # Optional: initialization scripts
59 # - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
60 networks:
61 - app-network
62 restart: unless-stopped
63 healthcheck:
64 test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
65 interval: 10s
66 timeout: 5s
67 retries: 5
68 # Security: Don't expose to host unless needed for development
69 # ports:
70 # - "5432:5432"
71
72 # --------------------------------------------------------------------------
73 # Redis Cache
74 # --------------------------------------------------------------------------
75 cache:
76 image: redis:7-alpine
77 container_name: redis_cache
78 command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD}
79 volumes:
80 # Named volume for cache persistence (optional)
81 - redis_data:/data
82 networks:
83 - app-network
84 restart: unless-stopped
85 healthcheck:
86 test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
87 interval: 10s
88 timeout: 3s
89 retries: 5
90 # Security: Don't expose to host unless needed
91 # ports:
92 # - "6379:6379"
93
94# ============================================================================
95# Networks
96# ============================================================================
97networks:
98 app-network:
99 driver: bridge
100 # Optional: custom subnet
101 # ipam:
102 # config:
103 # - subnet: 172.28.0.0/16
104
105# ============================================================================
106# Volumes
107# ============================================================================
108volumes:
109 postgres_data:
110 driver: local
111 # Optional: use a specific location on host
112 # driver_opts:
113 # type: none
114 # o: bind
115 # device: /path/on/host/postgres_data
116
117 redis_data:
118 driver: local
119
120# ============================================================================
121# Usage Instructions
122# ============================================================================
123#
124# 1. Create .env file from .env.example:
125# cp .env.example .env
126#
127# 2. Start all services:
128# docker-compose up -d
129#
130# 3. View logs:
131# docker-compose logs -f
132#
133# 4. Check service health:
134# docker-compose ps
135#
136# 5. Stop all services:
137# docker-compose down
138#
139# 6. Stop and remove volumes (WARNING: deletes data):
140# docker-compose down -v
141#
142# 7. Rebuild after code changes:
143# docker-compose up -d --build
144#
145# 8. Scale the web service:
146# docker-compose up -d --scale web=3
147# ============================================================================