Docker Compose Cheat Sheet: 50+ Essential Commands & Tips

The Docker Compose Cheat Sheet is your go-to reference for orchestrating multi-container Docker applications with ease. Whether you’re building, running, or debugging containers, this guide helps you master Docker Compose commands, syntax, and practical workflows — all in one place.

Docker Compose simplifies container management by allowing you to define and run multi-container environments using a single YAML file. With this cheat sheet, you’ll learn key commands, environment configurations, and productivity hacks every developer and DevOps engineer should know.

What Is Docker Compose?

Docker Compose is a tool that enables you to define, configure, and run multi-container Docker applications using a docker-compose.yml file. It provides a declarative way to describe services, networks, and volumes, so you can start an entire application stack with a single command.

Instead of manually linking and managing containers, Docker Compose automates the orchestration process. For example, a web app and its database can be defined in one file and started together seamlessly.

Why Use Docker Compose?

Docker Compose saves time and reduces complexity in managing multi-container environments. Here’s why it’s an essential DevOps tool:

  • Consistency: All environments — from development to production — behave the same.
  • Automation: One command can build, run, and stop all services.
  • Scalability: Easily scale containers up or down for testing and deployment.
  • Integration: Works smoothly with CI/CD pipelines and Docker Swarm.

With this Docker Compose Cheat Sheet, you’ll master everything from configuration to debugging.

Getting Started with Docker Compose

Before diving into commands, ensure Docker and Docker Compose are installed on your system.

1. Check Docker Compose Version

docker compose version

2. Verify Docker Installation

docker --version

3. Initialize a New Project

Create a directory for your project:

mkdir myproject && cd myproject

Then, create a file named docker-compose.yml inside it.

Basic Structure of docker-compose.yml

Here’s the fundamental structure of a docker-compose.yml file:

version: '3.9'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  database:
    image: postgres:latest
    environment:
      POSTGRES_PASSWORD: example
  • services define containers.
  • ports map host ports to container ports.
  • environment sets variables inside containers.

The YAML syntax is easy to read and highly customizable.

Docker Compose Up Command

The docker compose up command is one of the most frequently used in Docker Compose.

Start Services

docker compose up

This command builds, creates, and starts containers defined in the YAML file.

Run in Detached Mode

docker compose up -d

This runs containers in the background so your terminal stays free.

Rebuild Containers

docker compose up --build

Use this when you modify Dockerfiles or dependencies.

Docker Compose Down Command

To stop and remove containers, networks, and volumes:

Stop and Remove Containers

docker compose down

Remove with Volumes

docker compose down -v

Remove Images

docker compose down --rmi all

The down command is the opposite of up — it cleans your environment completely.

Docker Compose Build Commands

Building images from Dockerfiles is straightforward with Compose.

Build Services

docker compose build

Rebuild Specific Service

docker compose build web

No Cache Build

docker compose build --no-cache

This ensures fresh image creation, useful after updating dependencies.

Docker Compose Start and Stop

Control running services easily using start and stop.

Start All Services

docker compose start

Stop All Services

docker compose stop

Restart Services

docker compose restart

These commands are perfect for quick container lifecycle management.

Listing and Inspecting Containers

You can list active services and inspect their status with these commands.

List Running Containers

docker compose ps

View Container Logs

docker compose logs

Follow Live Logs

docker compose logs -f

These logs help you monitor container output and diagnose issues.

Running Commands Inside Containers

Use exec and run to execute commands inside your containers.

Run a Command in a Container

docker compose exec web bash

Run One-Off Commands

docker compose run --rm web python manage.py migrate

The --rm flag removes the container after execution.

Docker Compose Scaling Commands

Scaling allows you to run multiple container instances for a service.

Scale Services

docker compose up -d --scale web=3

This will create three instances of the web service.

Scaling is essential for handling load balancing or simulating production environments.

Environment Variables in Docker Compose

You can define environment variables directly or through .env files.

1. Inline Variables

environment:
  - DEBUG=true
  - DATABASE_URL=postgres://user:pass@db:5432/app

2. External .env File

env_file:
  - .env

3. Example .env File

DEBUG=false
APP_ENV=production

Using .env files helps manage configuration cleanly across environments.

Networking in Docker Compose

Networking lets your containers communicate internally or externally.

Default Network

By default, Docker Compose creates a single network for all services.

Custom Network

networks:
  backend:
  frontend:

Assigning Services to Networks

services:
  web:
    networks:
      - frontend
  db:
    networks:
      - backend

This separation enhances security and performance.

Volumes in Docker Compose

Volumes persist data even after containers are stopped or deleted.

Define Volumes

volumes:
  db_data:

Use Volumes in Services

services:
  database:
    image: postgres
    volumes:
      - db_data:/var/lib/postgresql/data

List Volumes

docker volume ls

Persistent storage is crucial for databases and stateful applications.

Docker Compose Configuration Validation

Before running a new configuration, always validate your YAML syntax.

Check Configuration

docker compose config

View Interpolated Output

docker compose config --services

Validation prevents runtime errors due to indentation or variable issues.

Docker Compose File Versions

Docker Compose supports multiple file formats (version: "2", "3", "3.9", etc.).

  • Version 2: Simpler format for standalone use.
  • Version 3: Supports Docker Swarm deployments.
  • Version 3.9: Latest with advanced networking and volume features.

Always check compatibility with your Docker Engine version.

Docker Compose with Dockerfile

Compose works seamlessly with Dockerfiles for custom builds.

Example Setup

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile

Build and Run Together

docker compose up --build

This automates the build process directly from your Compose file.

Docker Compose Logs and Debugging

Debugging multi-container systems becomes simple with these commands.

View Logs for a Specific Service

docker compose logs web

Display Last N Lines

docker compose logs --tail=50

Real-Time Monitoring

docker compose logs -f

Logs reveal errors, performance bottlenecks, and service behavior.

Docker Compose Cleanup Commands

To remove unused containers, images, and networks:

Prune Unused Resources

docker system prune

Remove Orphan Containers

docker compose down --remove-orphans

Remove All Stopped Containers

docker container prune

These commands help reclaim disk space and keep environments clean.

Docker Compose Best Practices

  1. Use .env files for sensitive or configurable values.
  2. Keep your Compose file modular and readable.
  3. Always validate configuration before deployment.
  4. Use descriptive container names for clarity.
  5. Clean up unused images and volumes regularly.

Adopting these habits ensures a reliable and efficient container workflow.

Conclusion

The Docker Compose Cheat Sheet provides everything you need to manage, build, and orchestrate multi-container applications efficiently. From YAML syntax and environment variables to scaling and debugging, these commands empower you to deploy with confidence.

Whether you’re a beginner or a DevOps professional, mastering Docker Compose commands will streamline your workflows, reduce errors, and accelerate application delivery.

FAQs About Docker Compose Cheat Sheet

1. What is Docker Compose used for?

Docker Compose is used to define and manage multi-container Docker applications using a YAML configuration file. It simplifies starting, stopping, and scaling services.

2. How do I start all containers in Docker Compose?

Use:
docker compose up -d
This starts all defined services in detached mode.

3. How do I remove containers in Docker Compose?

Run:
docker compose down
It stops and removes containers, networks, and volumes.

4. Can I scale services using Docker Compose?

Yes, use:
docker compose up -d --scale web=3
This creates multiple instances of the specified service.

5. How do I view logs for a specific service?

Use:
docker compose logs <service_name>
to display logs for the chosen service.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top