Introduction
Docker Compose is a powerful tool that simplifies building and managing multi-container Docker applications. With Compose, you can define and configure the application’s services in a declarative YAML file then spin everything up with one command.
In this complete guide, we’ll cover key Docker Compose concepts and see how to use Compose to develop and deploy applications faster with Docker.
What is Docker Compose?
Docker Compose is a tool that helps define and run complex applications made up of multiple, interconnected Docker containers.
With Compose, you write a YAML file describing the application’s components like databases, message queues, caches, web/api servers etc. Then, a single command spins up everything as containers configured per the spec.
Compose handles:
- Starting and stopping containers
- Networking between containers
- Injecting environment variables
- Volume mounting for data persistence
- Application healthchecking
- Scaling up containers
- Streaming logs
This simplifies the development workflow and provides a production-ready container platform.
Why Docker Compose?
Compose simplifies container-based development:
- Declarative YAML for modeling environments
- No need to manually spin up containers
- Compact alternative to Kubernetes
- Standardized environments across dev, CI, prod
- Easy DNS-based service discovery
- Variable reuse across configurations
- Preserves volume data when rebuilding
Overall, Compose accelerates developing and testing containerized apps.
Installing Docker Compose
Compose is included in Docker Desktop installations. If unavailable, install separately:
# Linux:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# macOS:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Test installation:
docker-compose --version
Now we’re ready to use Docker Compose!
Docker Compose Files
Compose configurations live in a docker-compose.yml
file. This declarative file describes the architecture:
version: '3.8'
services:
db:
image: postgres:13
volumes:
- db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=app_db
- POSTGRES_USER=appuser
- POSTGRES_PASSWORD=password
web:
build: ./web
ports:
- "8000:8000"
depends_on:
- db
volumes:
db-data:
This config defines a Postgres DB and Python web app. Compose handles networking them together.
Service, volume, and network definitions resemble Docker run and build commands. But Compose inserts the magic to run containers at scale.
Defining Services
Services define component containers. For example:
services:
web:
build: ./web-app
ports:
- "8000:8000"
environment:
- NODE_ENV=production
db:
image: postgres:13
volumes:
- db-data:/var/lib/postgresql/data
This runs a web app talking to Postgres. Each service represents a container.
Configuration like images, builds, ports, and volumes specifies how to run each component.
Commands and Usage
With a docker-compose.yml file present, we can run:
docker-compose up
This builds images and stands up all containers in the foreground. Ctrl-C stops.
For background execution, use:
docker-compose up -d
Other useful commands include:
docker-compose build - Build images
docker-compose stop - Stop services
docker-compose down - Stop and remove containers
docker-compose logs - View logs
docker-compose ps - List containers
docker-compose exec - Execute command in container
Plus many more for managing the app lifecycle.
Here are some common Docker Compose commands explained in a table with examples:
Command | Description | Example |
---|---|---|
docker-compose up | Builds and starts containers in foreground | docker-compose up |
docker-compose up -d | Starts containers in background | docker-compose up -d |
docker-compose down | Stops and removes containers | docker-compose down |
docker-compose stop | Stops running containers | docker-compose stop |
docker-compose build | Builds service images | docker-compose build |
docker-compose logs | Views container logs | docker-compose logs -f |
docker-compose ps | Lists running containers | docker-compose ps |
docker-compose exec | Executes command in container | docker-compose exec web sh |
docker-compose run | Runs one-off command in new container | docker-compose run web python script.py |
docker-compose pull | Pulls latest images | docker-compose pull |
docker-compose restart | Restarts containers | docker-compose restart |
docker-compose config | Validates Compose file | docker-compose config |
Environment Variables
The Compose file can specify environment variables to inject:
services:
web:
environment:
- NODE_ENV=production
- API_KEY=12345
These will populate runtime environments of the containers, keeping credentials safe.
For dev/test variability, use a .env
file:
POSTGRES_DB=devdb
Then reference like ${POSTGRES_DB}
in the Compose file.
Networking and Service Discovery
By default, Compose sets up a network for communication between containers.
Services can be reached at hostnames matching their service name – no linking required.
For example, web
talks to db
since both exist on the same Compose network.
Production deployments should secure containers using private networks and TLS.
Deploying Apps with Docker Compose
Once an app is defined in docker-compose.yml, it can be deployed anywhere running Docker.
For testing, run Compose locally:
docker-compose up -d
For production, deploy yaml to servers and run:
docker-compose up -d
The Compose file provides portability between environments. The same containers spin up on dev laptops, CI servers, and production.
Compose at Scale
As apps grow, use Compose alongside:
- Docker Swarm – for clustering and scheduling
- Docker Stacks – deploy Compose apps to Swarm
- Docker Secret – securely manage app secrets
Large apps may transition to Kubernetes while preserving Compose for local dev.
Compose integrates with production platforms while smoothing app development workflows.
Conclusion
Docker Compose is a powerful tool for defining and running containerized applications. For both dev and prod, Compose simplifies modeling and running multi-service environments as a cohesive application.
With Compose:
- Declare app architecture in YAML
- Spin up full environments with one command
- Standardize your application platform
- Streamline CI/CD and testing
- Manage app lifecycles consistently
By mastering Docker Compose, you can streamline and accelerate building and operating containerized apps. Start utilizing Compose today for faster Docker development workflows.
Frequently Asked Questions
Q: Is Compose only for local development?
A: No – production applications can also be deployed using Compose then managed with CLI or Compose plugins. Large apps may transition to Kubernetes.
Q: How is Compose different from Docker Stack?
A: Stack is used to deploy Compose apps to Swarm clusters. Compose focuses on local dev/test workflows. Together they enable portable applications.
Q: Can I control scaling and replica counts with Compose?
A: Yes, you can specify scale under each service. Replica management is easier done through Swarm or Kubernetes though.
Q: Does using volumes result in slower file access?
A: There is minor performance overhead but bind mounts provide close to native speeds. Named volumes add some latency but enable portability.
Q: Is there a GUI to manage Compose apps?
A: Portainer and Kitematic both provide UIs for controlling Compose. But CLI allows greater flexibility.