Other Parts of This Series:
DevOps Docker Compose & Docker Swarm (Photo Credit: Unsplash)
Story:
Pure Docker is useful for running a simple standalone application or service. But real-world projects are not always as straightforward and simple as they appear. During development, we constantly require multiple dependencies or services to function, such as a database, cache, message broker, and so on. Rasel realized we needed something to solve this problem. Rasel was looking for a solution that was capable of handling multiple container bootstrapping. And guess what? Rasel discovered that Docker Compose is the solution. That’s why, Rasel decided to learn Docker Compose and Docker Swarm in detail.
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. Instead of writing many docker run commands, you define everything in a single “docker-compose.yml” (latest version support compose.yml) file - services, networks, volumes, environment variables, build instructions, etc. You then launch the entire stack with: “docker compose up”.
Benefits of Docker Compose
- Simplified Management: Docker Compose uses a docker-compose.yml file (or docker-compose.yaml) to define all thing and then use simplified commands and gives efficient development flow.
- Consistent Environment: Like docker it also gives consistency for multiple container and persist same behaviors across different environment. Thus reduced error also.
- Enhanced Collaboration: The docker-compose.yml file is easy to share with other developers, facilitating collaboration and knowledge sharing.
- Portability: Like docker enhances portability and reduce platform dependency. Also gives more customizable power.
- Scalability: Docker compose gives us the ability to scale our multiple container services add and run. Also possible to work with single container from multiple one of docker compose.
- Persistency: Docker compose make volume mounting or binding easy.
- Networking: Docker compose make manage docker networking easy as well.
- Env and Config: Has options for Env and Config management.
Anatomy of a docker-compose.yml File
| |
1) version
Specifies the Compose file format version.
| |
- version: “3” is stable and widely supported.
- 3.x versions support Swarm mode (useful if you later move to orchestration).
2) services
Defines your application containers.
- Each top-level key under services is a separate container.
Inside the service section following options are available:
2.1) build
Build image from a Dockerfile in the current or custom directory.
2.2) image
Use an image (from Docker Hub or local).
| |
2.3) container_name
Give a custom name to the container.
| |
2.4) ports
Map host ports to container ports.
2.5) volumes
Mount host directories or Docker volumes.
2.6) environment
Pass environment variables. Or use a .env file:
2.7) depends on
Set startup order (note: doesn’t wait for service to be ready).
2.8) networks
Connect services to specific Docker networks.
2.9) entrypoint & commands
Override the image or Dockerfile’s default behavior.
- Useful for container customization or debugging.
2.10) restart
Define restart policy.
| |
3) volumes
Define named volumes that persist between runs.
These can be used in services like:
4) networks
Define user-defined networks for better isolation and control.
Attach to service like:
5) secrets & configs
Used in Swarm mode for storing sensitive data like API keys, TLS certs, etc.
Use inside service:
6) healthcheck
Monitor container health.
Advanced Docker Compose Features:
7) Lifecycle Hook
Define the operation that can be run after container start and before container stop.
8) Service Profile
Group services into profiles so you can run only certain parts of a stack.
- docker compose –profile dev up
- Great for running debug tools, test helpers, or skipping prod-only services.
9) Compose Watch
Monitors source code and rebuilds/restarts containers on change - no more manual rebuilds!
- This is a next-gen replacement for hot reloading setups like Nodemon, air.
- docker compose watch or docker compose up –watch
10) Multi Compose File Setup
Split your Compose configuration into base + environment-specific overrides:
- docker-compose.yml # Base config
- docker-compose.dev.yml # Dev-specific config
- docker-compose.prod.yml # Prod-specific config
And can be 3 type: (For more info please read the official doc: https://docs.docker.com/compose/how-tos/multiple-compose-files/)
- Merge
- Extend
- Include
11) Extends Common Property
Reuse common config across services (in Compose v2 format):
Useful Docker Compose Commands
| |
For more details see the official docs: https://docs.docker.com/compose/
Docker Swarm
Docker Swarm is Docker’s native container orchestration system, allowing you to deploy and manage a cluster of Docker Engines as a single virtual system.
Container orchestration is the process of automating the deployment, scaling, networking, and management of containers.
And a cluster is a group of computers (physical or virtual machines) connected together to function as a single system.
Benefits of Swarm
- High Availability: Containers can be replicated across nodes.
- Scalability: Easily scale services up/down.
- Rolling Updates: Deploy updates with zero downtime.
- Load Balancing: Distributes traffic automatically.
- Self-healing: Failed containers are automatically restarted or rescheduled.
Basic Terminology
- Node - A machine (VM or physical) in the Swarm
- Manager Node - Controls the cluster; makes scheduling decisions
- Worker Node - Executes containers based on instructions from manager
- Service - A task or set of tasks (containers) defined by image, scale, network, etc.
- Task - A single container instance
- Stack - A group of services defined using docker-compose.yml (Swarm-aware)
Swarm with Native Docker
| |
Swarm with Docker Compose (Stacks)
| |
Summary of Swarm-specific Features Used
- deploy: Swarm-only block for scaling, restart policy, constraint
- replicas: Number of container instances per service
- placement: Control where services run (manager or worker nodes)
- secrets: Secure storage of sensitive data
- update_config: Rolling update config for zero downtime
Now to deploy the services or application using docker swarm follow the following steps:
| |
Swarm Best Practices
- Always use at least 3 manager nodes (for quorum).
- Use overlays for cross-node networking.
- Use stacks for real-world projects.
- Store secrets using Docker secrets.
- Monitor via docker service ps, docker node ls, docker events.
For more details see the official docs: https://docs.docker.com/engine/swarm/
Summary:
Docker Compose is a tool that lets you define and run multi-container applications using a single file called docker-compose.yml. Instead of running individual containers with many docker run commands, Compose allows you to describe your whole app - including services, networks, volumes, and environment settings - in one place. You can start everything with just docker compose up, making it perfect for development and small setups.
Docker Swarm is Docker’s built-in system for orchestrating containers across multiple machines. It lets you manage a cluster of Docker engines (called nodes), deploy services that run on them, scale containers up or down, perform rolling updates, and balance traffic automatically. You define your services in a Compose file using special deploy settings and then use docker stack deploy to launch them across the Swarm cluster. Swarm adds high availability, self-healing, and scalability to your containerized applications - all using familiar Docker tools.