Docker vs. Docker Compose

DevOps and Cloud Engineer skilled in AWS, Docker, Kubernetes, Terraform, and Ansible. Passionate about automation, optimizing performance, and enhancing security. Dedicated to delivering scalable and resilient cloud solutions.
Understanding Docker
Docker is a platform for developing, shipping, and running applications inside containers. Containers are lightweight, portable, and self-sufficient, making it easier to manage applications and their dependencies.
Example: Running a Voting App with Docker
Consider a simple voting application that consists of multiple services:
Voting App (Python) - Collects votes for cars and bikes.
Database (Redis) - Temporarily stores the votes.
Worker Application (.NET) - Processes votes and updates the persistent database.
Persistent Database (PostgreSQL) - Stores vote counts in tables.
Web Application (Node.js) - Reads and displays the final vote count to users.
Running the Application with Docker
docker run -d --name=redis
redis docker run -d --name=db postgres:9.4
docker run -d --name=vote -p 5000:80 voting-app
docker run -d --name=result -p 5001:80 result-app
docker run -d --name=worker worker
While the containers are running, they are not linked. Services can't communicate with each other unless explicitly connected.
Linking Containers
Docker provides the --link option to connect containers:
docker run -d --name=redis redis
docker run -d --name=db postgres:9.4
docker run -d --name=vote -p 5000:80 --link redis:redis voting-app
docker run -d --name=result -p 5001:80 --link db:db result-app
docker run -d --name=worker --link db:db --link redis:redis worker
Understanding Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications using a simple YAML configuration file.
Example: Running the Voting App with Docker Compose
Instead of running individual docker run commands, we define the entire stack in a docker-compose.yml file:
version: '3' services: redis: image: redis db: image: postgres:9.4 ports: - "5000:80" vote: image: voting-app ports: - "5001:80" depends_on: - redis result: image: result-app depends_on: - db worker: image: worker depends_on: - redis - db
Running the Application with Docker Compose
Simply execute:
docker-compose up -d
This command automatically starts all services, links them together, and ensures dependencies are managed correctly.
Key Differences Between Docker and Docker Compose
| Feature | Docker | Docker Compose |
| Command execution | Run multiple docker run commands | Single docker-compose up command |
| Configuration | CLI-based, requires manual linking | YAML-based, automated linking |
| Service Management | Containers are started individually | All services managed in a single file |
| Portability | Harder to replicate exact setup | Easily shareable with docker-compose.yml |
Conclusion
While Docker is great for running individual containers, Docker Compose makes it easier to manage multiple services in a structured and scalable manner. If you're working with multi-container applications, Docker Compose is the way to go!