Docker
DockerIntermediate

Docker Compose V2: The Complete Guide to Modern Container Orchestration

DevHub Team
4 min read
DockerDocker ComposeContainer OrchestrationDevOps

TL;DR

Master Docker Compose V2 with this comprehensive guide covering new features, best practices, and advanced orchestration patterns for modern containerized applications

Docker Compose V2: The Complete Guide to Modern Container Orchestration

Docker Compose V2 brings significant improvements to container orchestration with enhanced features, better performance, and improved developer experience. This guide explores everything you need to know about Docker Compose V2.

$1

``mermaid

graph TB

subgraph "Compose V2"

A["CLI Interface"]

B["Compose Spec"]

C["Docker API"]

end

subgraph "Features"

D["Dependency Resolution"]

E["Network Management"]

F["Volume Management"]

G["Service Discovery"]

end

subgraph "Extensions"

H["Profiles"]

I["Include"]

J["Deploy"]

end

A --> B

B --> C

B --> D

B --> E

B --> F

B --> G

B --> H

B --> I

B --> J

classDef compose fill:#1a73e8,stroke:#fff,color:#fff

class A,B,C,D,E,F,G,H,I,J compose

`

$1

Feature Description Benefits
Compose Spec Standardized format Portability
Profiles Environment grouping Flexibility
Include Composition reuse Modularity

$1

$1

`yaml

docker-compose.yml

services:

web:

build:

context: ./web

dockerfile: Dockerfile

args:

NODE_ENV: development

ports:

- "3000:3000"

environment:

- DATABASE_URL=postgres://user:pass@db:5432/dbname

depends_on:

db:

condition: service_healthy

deploy:

replicas: 2

resources:

limits:

cpus: '0.50'

memory: 512M

db:

image: postgres:14-alpine

volumes:

- db_data:/var/lib/postgresql/data

environment:

- POSTGRES_USER=user

- POSTGRES_PASSWORD=pass

- POSTGRES_DB=dbname

healthcheck:

test: ["CMD-SHELL", "pg_isready -U user -d dbname"]

interval: 5s

timeout: 5s

retries: 5

volumes:

db_data:

`

$1

`yaml

docker-compose.yml

services:

web:

profiles: ["web", "frontend"]

# ... service configuration ...

api:

profiles: ["api", "backend"]

# ... service configuration ...

db:

profiles: ["db", "backend"]

# ... service configuration ...

redis:

profiles: ["cache", "backend"]

# ... service configuration ...

`

$1

$1

`yaml

base.yml

services:

base:

image: nginx:alpine

volumes:

- ./nginx.conf:/etc/nginx/nginx.conf

docker-compose.yml

include:

- base.yml

services:

web:

extends:

file: base.yml

service: base

environment:

- VIRTUAL_HOST=example.com

`

$1

`yaml

docker-compose.yml

services:

app:

build: .

depends_on:

db:

condition: service_healthy

restart: true

redis:

condition: service_started

migration:

condition: service_completed_successfully

migration:

image: flyway

command: migrate

depends_on:

db:

condition: service_healthy

`

$1

$1

`yaml

docker-compose.yml

services:

web:

build:

context: .

target: ${TARGET:-development}

volumes:

- .:/app

- /app/node_modules

environment:

- NODE_ENV=${NODE_ENV:-development}

command: ${COMMAND:-npm run dev}

profiles:

- dev

- prod

docker-compose.override.yml

services:

web:

ports:

- "3000:3000"

volumes:

- .:/app:cached

environment:

- DEBUG=true

`

$1

Environment Command Profile
Development compose up dev
Testing compose --profile test up test
Production compose --profile prod up prod

$1

$1

`yaml

docker-compose.yml

services:

app:

deploy:

resources:

limits:

cpus: '0.50'

memory: 512M

reservations:

cpus: '0.25'

memory: 256M

restart_policy:

condition: on-failure

delay: 5s

max_attempts: 3

window: 120s

`

$1

`yaml

docker-compose.yml

services:

app:

networks:

frontend:

ipv4_address: 172.16.238.10

backend:

aliases:

- api.internal

networks:

frontend:

driver: bridge

enable_ipv6: true

ipam:

driver: default

config:

- subnet: 172.16.238.0/24

backend:

driver: overlay

internal: true

`

$1

$1

`yaml

docker-compose.yml

services:

web:

secrets:

- db_password

- ssl_cert

environment:

- DB_PASSWORD_FILE=/run/secrets/db_password

secrets:

db_password:

file: ./secrets/db_password.txt

ssl_cert:

file: ./secrets/ssl_cert.pem

`

$1

Option Purpose Example
cap_drop Remove capabilities ALL
security_opt Security policies no-new-privileges
read_only Filesystem protection true

$1

$1

`yaml

docker-compose.yml

services:

app:

logging:

driver: "json-file"

options:

max-size: "10m"

max-file: "3"

labels: "production"

env: "os,customer"

labels:

com.example.description: "Web application"

com.example.environment: "production"

`

$1

`yaml

services:

web:

healthcheck:

test: ["CMD", "curl", "-f", "http://localhost:3000/health"]

interval: 30s

timeout: 10s

retries: 3

start_period: 40s

`

$1

$1

`bash

project/

├── docker-compose.yml # Base configuration

├── docker-compose.override.yml # Development overrides

├── docker-compose.prod.yml # Production settings

├── .env # Environment variables

├── services/

│ ├── web/

│ │ ├── Dockerfile

│ │ └── src/

│ └── api/

│ ├── Dockerfile

│ └── src/

└── config/

├── nginx/

└── postgres/

``

$1

File Purpose Environment
compose.yml Base config All
compose.override.yml Development Local
compose.prod.yml Production Production

$1

$1

Issue Cause Solution
Network Conflicts Port binding Change ports
Volume Permissions User mapping Set user:group
Resource Limits Container limits Adjust resources

$1

1. [Docker Compose Documentation](https://docs.docker.com/compose/)

2. [Compose Specification](https://compose-spec.io/)

3. [Docker Compose Best Practices](https://docs.docker.com/compose/production/)

4. [Networking Guide](https://docs.docker.com/compose/networking/)

5. [Security Guidelines](https://docs.docker.com/engine/security/)

6. [Resource Management](https://docs.docker.com/compose/compose-file/deploy/)

  • [Docker Desktop Alternatives](/posts/docker/desktop-alternatives) - Development environment
  • [Docker Security Scanning](/posts/docker/security-scanning) - Security tools
  • [Docker Multi-stage Builds](/posts/docker/multi-stage-builds) - Build optimization
  • [Docker Kubernetes Integration](/posts/docker/kubernetes-integration) - Container orchestration
  • Why This Matters

    Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.

    Trade-offs to Consider

    Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.

    When NOT to Use This

    Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.

    Decision Framework

    Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.