Docker Compose
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. Instead of running each container manually with multiple **docker run** commands, you can define all services (backend, frontend, databases, etc.) in a single configuration file (**docker-compose.yml**) and manage them together.
Introduction to Docker Compose
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. Instead of running each container manually with multiple docker run commands, you can define all services (backend, frontend, databases, etc.) in a single configuration file (docker-compose.yml) and manage them together.
Why Use Docker Compose?
- Multi-service apps: Easily run apps with multiple components (e.g., frontend, backend, database).
- Simplified workflow: One command to start or stop the entire stack.
- Consistent environments: Same configuration for all team members.
- Networking made easy: Services can communicate via predefined network without extra configuration.
Writing a docker-compose.yml File
A docker-compose.yml file defines all the services of your application, along with their ports, volumes, and environment variables.
version: '3.8'
services:
frontend:
build: ./frontend
ports:
- "3000:3000"
depends_on:
- backend
backend:
build: ./backend
ports:
- "5000:5000"
environment:
- MONGO_URL=mongodb://db:27017/mydb
depends_on:
- db
db:
image: mongo:6
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
volumes:
mongo_data:In this setup:
- frontend: React app running on port 3000.
- backend: Node.js API server running on port 5000, connected to MongoDB.
- db: MongoDB container with persistent storage using a volume.
Running Docker Compose
You can start and manage your entire application stack with simple commands.
docker compose up # Start all services (in foreground)
docker compose up -d # Start in detached (background) mode
docker compose down # Stop and remove all services
docker compose logs # View logs from all services
docker compose logs -f # Follow logs in real-time
docker compose ps # List running servicesExample Workflow
- Create your docker-compose.yml file.
- Run docker compose up -d to start all services.
- Access frontend at http://localhost:3000.
- Backend communicates with MongoDB using the internal network db:27017.
- Run docker compose down when finished.
Conclusion
Docker Compose simplifies managing multi-service applications. Instead of running separate commands for frontend, backend, and databases, you define everything once in a YAML file and manage the full application lifecycle with just a few commands. This makes Docker Compose a must-have for modern development and testing workflows.