✦ For everyone, free.

Practical knowledge for real and everyday life

Home

9.2.3.2 Service Volume Mounts

A focused guide to Service Volume Mounts, connecting core concepts with practical Docker and container operations.

Service volume mounts are the entries within a Compose service's own volumes field, specifying exactly which named volumes, bind mounts, or anonymous volumes that particular service should have attached, and at what container path each one should appear.

The Three Forms of Volume Mounts Within a Service

A service's volumes list can mix named volume references, bind mount paths, and anonymous volume declarations, exactly as the equivalent -v flags would for an individual docker run command.

services:
  app:
    volumes:
      - app-data:/data
      - ./config:/app/config
      - /app/cache

The first is a named volume reference, the second a bind mount to a relative host path, and the third an anonymous volume for a path the service needs isolated from the rest of its filesystem.

Specifying Read-Only Access

Appending :ro to any of these mount forms restricts that particular mount to read-only access.

services:
  app:
    volumes:
      - ./config:/app/config:ro
Using the More Explicit Long Syntax

For more complex configuration, a mount can be expressed using a more explicit, structured form instead of the compact string syntax.

services:
  app:
    volumes:
      - type: volume
        source: app-data
        target: /data
        read_only: false

This long form can be more readable for mounts with several configuration options, even though it's functionally equivalent to the more compact string syntax for simple cases.

Confirming a Service's Actual Mounts

Inspecting a running service's container confirms exactly which mounts are actually in effect, useful for validating the Compose file's volumes configuration is being applied as expected.

docker compose exec app mount | grep /data
Why Service Volume Mounts Matter

A service's volumes field is where the specific storage needs declared elsewhere in the Compose file (or referenced as simple host paths) actually get attached to that service, making correct configuration here essential to the service having access to exactly the data and configuration it requires.