Docker Compose Networking, Volumes, and Runtime Controls

Compose patterns for ports, networks, volumes, secrets, configs, and runtime controls.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Ports and Networks
Publish container port
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"

# Maps host port 8080 to container port 80.

Bind only on localhost
services:
  db:
    image: postgres:16
    ports:
      - "127.0.0.1:5432:5432"

# Publishes the service only to the local machine.

Attach services to custom network
services:
  web:
    image: nginx:alpine
    networks: ["frontend"]
  api:
    image: myapi:latest
    networks: ["frontend", "backend"]

networks:
  frontend: {}
  backend: {}

# Defines multiple named networks for segmentation.

Set network aliases
services:
  api:
    image: myapi:latest
    networks:
      backend:
        aliases:
          - internal-api

networks:
  backend: {}

# Adds alternate DNS names for a service on a network.

Resolve published port
docker compose port web 80

# Shows the host binding for a service port.

## Volumes and Storage
Named volume for persistent data
services:
  db:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata: {}

# Stores database data in a managed named volume.

Bind mount local source
services:
  app:
    image: node:20
    working_dir: /app
    volumes:
      - ./:/app

# Mounts the current directory into the container.

Read-only bind mount
services:
  nginx:
    image: nginx:alpine
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro

# Mounts a host file as read-only in the container.

Use tmpfs mount
services:
  app:
    image: myapp:latest
    tmpfs:
      - /tmp

# Creates an in-memory temporary filesystem for the service.

Remove stack and its volumes
docker compose down -v

# Tears down the project and deletes named volumes created for it.

## Configs and Secrets Patterns
Mount a secret file
services:
  api:
    image: myapi:latest
    secrets:
      - app_key

secrets:
  app_key:
    file: ./secrets/app_key.txt

# Mounts a file-backed secret into the container.

Mount a config file
services:
  nginx:
    image: nginx:alpine
    configs:
      - source: nginx_conf
        target: /etc/nginx/conf.d/default.conf

configs:
  nginx_conf:
    file: ./nginx/default.conf

# Provides a structured way to mount a config artifact.

## Resource and Runtime Controls
Restart policy
services:
  api:
    image: myapi:latest
    restart: unless-stopped

# Restarts the service unless it is explicitly stopped.

Raise ulimits
services:
  app:
    image: myapp:latest
    ulimits:
      nofile:
        soft: 65535
        hard: 65535

# Sets per-container ulimits for file handles and similar resources.

Inject extra hosts
services:
  app:
    image: myapp:latest
    extra_hosts:
      - "host.docker.internal:host-gateway"

# Adds static host entries inside the container.

Recommended next

No recommendations yet.