Docker Compose Basics
Introduction to Docker Compose
Docker Compose is a powerful tool for container orchestration, enabling developers to define and manage multi-container applications through a single configuration file. It simplifies the process of running complex application architectures by allowing you to describe services, networks, and volumes in a declarative manner.
Core Concepts and Architecture
Docker Compose uses YAML files to configure application services, providing a streamlined approach to container service configuration. The primary configuration file is typically named docker-compose.yml
.
graph TD
A[Docker Compose] --> B[YAML Configuration]
B --> C[Service Definitions]
B --> D[Network Settings]
B --> E[Volume Mappings]
Configuration File Structure
A typical Docker Compose configuration includes key elements:
Element |
Description |
Example |
version |
Compose file version |
version: '3.8' |
services |
Container definitions |
Multiple service configurations |
networks |
Custom network settings |
Bridge, host networks |
volumes |
Persistent data storage |
Named or host-mounted volumes |
Practical Example: Web Application Setup
Here's a comprehensive Docker Compose configuration for a typical web application:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./website:/usr/share/nginx/html
database:
image: postgres:13
environment:
POSTGRES_PASSWORD: secretpassword
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Command-Line Operations
Essential Docker Compose commands for managing multi-container applications:
## Start services defined in docker-compose.yml
docker-compose up -d
## Stop and remove containers
docker-compose down
## View running containers
docker-compose ps
## View service logs
docker-compose logs web
Service Dependency Management
Docker Compose allows defining service dependencies to control startup sequence:
services:
web:
depends_on:
- database
restart: on-failure
This configuration ensures the database service starts before the web service, with automatic restart capabilities.