Module 6: Multi-Container Applications
🔹 Why Multi-Container Setups Are Needed
In real-world applications, one container is rarely enough.
A web app might need a database.
A backend service might rely on a cache like Redis.
Splitting services makes applications more scalable, modular, and easier to maintain.
Instead of packing everything into one container, we run multiple containers that work together.
🔹 Linking Services Manually
Before Docker Compose, containers were linked manually:
# Run MySQL container
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=root mysql:8.0
# Run WordPress container linked to MySQL
docker run -d --name wordpress --link mysql-db:mysql -p 8080:80 wordpress
👉 But this approach doesn’t scale well for large projects.
🔹 Writing a Simple docker-compose.yml
docker-compose makes managing multi-container applications easy.
Here’s an example docker-compose.yml:
version: '3.8'
services:
db:
image: mysql:8.0
restart: always
environment:
MYSQL_DATABASE: wordpress
MYSQL_USER: wp_user
MYSQL_PASSWORD: wp_pass
MYSQL_ROOT_PASSWORD: root
volumes:
- db_data:/var/lib/mysql
wordpress:
image: wordpress:latest
restart: always
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: wp_pass
WORDPRESS_DB_NAME: wordpress
depends_on:
- db
volumes:
db_data:
🔹 Example: WordPress + MySQL Using Docker Compose
Save the above YAML file as
docker-compose.yml.Run:
docker-compose up -dOpen browser →
http://localhost:8080You’ll see WordPress setup screen, connected to MySQL automatically.
✅ Key Benefits of Multi-Container Apps
Clear separation of concerns
Easier scaling of individual services
Faster debugging & upgrades
Production-ready with minimal changes
