Docker Compose is a tool for defining and running multi-container Docker applications from a single YAML configuration file (docker-compose.yml or compose.yaml). Instead of manually running multiple docker run commands and configuring networks and volumes by hand, Docker Compose allows you to declare all services (web application, database, cache, message queue) in one file and start the entire stack with a single command: docker compose up. Docker Compose V2 (the modern version) is shipped as the docker compose plugin (note: space, not hyphen) and is included with the Docker Engine installation. It is the standard tool for local development environments, integration testing, and simple single-server production deployments. This guide covers installing and using Docker Compose on RHEL 9 with practical examples.
Prerequisites
- Docker Engine installed on RHEL 9
Step 1 — Verify Docker Compose Installation
# Docker Compose V2 is included with Docker Engine as a plugin
docker compose version
# If not installed, install the plugin separately:
dnf install -y docker-compose-plugin
Step 2 — Create a Docker Compose Application Stack
# compose.yaml — a web application with database and cache
mkdir /var/www/mystack && cd /var/www/mystack
cat > compose.yaml << 'EOF'
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html:ro
depends_on:
- app
app:
build: .
environment:
- DATABASE_URL=postgresql://appuser:AppPass123!@db:5432/appdb
- REDIS_URL=redis://cache:6379
depends_on:
db:
condition: service_healthy
cache:
condition: service_started
db:
image: postgres:16-alpine
environment:
POSTGRES_DB: appdb
POSTGRES_USER: appuser
POSTGRES_PASSWORD: AppPass123!
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U appuser -d appdb"]
interval: 10s
timeout: 5s
retries: 5
cache:
image: redis:7-alpine
command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru
volumes:
postgres_data:
EOF
Step 3 — Essential Docker Compose Commands
# Start all services in detached mode
docker compose up -d
# View running containers and their status
docker compose ps
# View logs (all services or a specific service)
docker compose logs -f
docker compose logs -f app
# Stop all services (containers remain)
docker compose stop
# Stop and remove containers, networks (volumes preserved)
docker compose down
# Stop and remove everything including volumes (DESTRUCTIVE — removes database data)
docker compose down -v
# Scale a service
docker compose up -d --scale app=3
Step 4 — Rebuild and Update
# Rebuild images (after code changes)
docker compose build
# Pull updated base images
docker compose pull
# Recreate containers with new images
docker compose up -d --force-recreate
# Run one-off commands in a container
docker compose exec app bash
docker compose run --rm app python manage.py migrate
Step 5 — Environment Variable Management
# .env file — automatically loaded by Docker Compose
# /var/www/mystack/.env
POSTGRES_PASSWORD=AppPass123!
APP_SECRET=changeme
COMPOSE_PROJECT_NAME=mystack
# Reference .env variables in compose.yaml
environment:
DATABASE_URL: postgresql://appuser:${POSTGRES_PASSWORD}@db:5432/appdb
Conclusion
Docker Compose on RHEL 9 simplifies multi-container application deployment to a single configuration file and single command. The healthcheck with depends_on: condition: service_healthy ensures services start in the correct order and wait for their dependencies to be genuinely ready (not just started), preventing connection failures on application startup. For production deployments beyond a single server, transition to Docker Swarm or Kubernetes for container orchestration across multiple nodes.
Next steps: How to Install Docker Engine on RHEL 9, How to Deploy Applications with Docker Compose on RHEL 9, and How to Install Kubernetes on RHEL 9.