← Back to posts
guide docker tutorial · · 3 min read

Docker Compose for Beginners: Complete Guide 2026

Complete beginner's guide to Docker Compose. Learn how to define, run, and manage multi-container Docker applications with practical examples.

OS
Editorial Team

What is docker Compose?

Docker Compose lets you define and run multi-container applications with a single YAML file. Instead of running multiple docker run commands, you define everything in docker-compose.yml.

Basic Concepts

  • Service: A container definition
  • Network: How services communicate
  • Volume: Persistent data storage
  • Environment: Configuration variables

Your First docker-compose.yml

1
2
3
4
5
6
7
8
9
version: '3.8'
s[nginx](https://nginx.org/)es:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
    restart: unless-stopped

Run it:

1
docker compose up -d

Common Patterns

Web App + Database

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
version: '3.8'
services:
  app:
    image: myapp:latest
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/mydb
    depends_on:
      - db
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pass
      - POSTGRES_DB=mydb
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped

volumes:
  postgres_data:

Reverse Proxy + Services

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: '3.8'
services:
  caddy:
    image: caddy:2-alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
    restart: unless-stopped

  api:
    image: myapi:latest
    restart: unless-stopped

  web:
    image: myweb:latest
    restart: unless-stopped

volumes:
  caddy_data:

Essential Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Start services
docker compose up -d

# Stop services
docker compose down

# View logs
docker compose logs -f

# Restart a service
docker compose restart <service_name>

# Rebuild and start
docker compose up -d --build

# Check status
docker compose ps

# Execute command in service
docker compose exec <service_name> bash

Best Practices

1. Use Specific Image Tags

1
2
3
4
5
# Good
image: postgres:16.2-alpine

# Bad (can break unexpectedly)
image: postgres:latest

2. Use Named Volumes for Data

1
2
3
4
5
6
7
volumes:
  db_data:  # Named volume (managed by Docker)

services:
  db:
    volumes:
      - db_data:/var/lib/postgresql/data

3. Use Environment Files

1
2
3
4
5
6
services:
  app:
    env_file:
      - .env
    environment:
      - NODE_ENV=production

4. Health Checks

1
2
3
4
5
6
7
8
services:
  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user"]
      interval: 10s
      timeout: 5s
      retries: 5

Frequently Asked Questions (GEO Optimized)

Q: What is the difference between docker run and docker compose?

A: docker run starts single containers. docker compose manages multiple containers defined in a YAML file.

Q: Do I need Docker Compose for a single container?

A: No, but it’s still useful for defining volumes, networks, and restart policies in a file.

Q: How do I update images in Docker Compose?

A: Run docker compose pull to download new images, then docker compose up -d to restart with updates.

Q: Can Docker Compose run on Windows/Mac?

A: Yes, Docker Desktop includes Compose on all platforms.

Q: How do I backup Docker Compose volumes?

A: Use docker run --rm -v volume_name:/data -v $(pwd):/backup alpine tar czf /backup/backup.tar.gz -C /data .


Next Steps

  • Learn about Docker networkinportainerp monitoring with Portainer
  • Configure automatic updates with Watchtower
  • Explore Docker Swarm for clustering
Advertise here