Docker Volume Mounts vs Bind Mounts: When to Use Each

· 2 min read
docker devops containers

What We’re Building

A multi-container setup where an app container builds assets and an nginx container serves them-using shared volumes to keep them in sync.

Prerequisites

  • Docker and Docker Compose installed
  • Basic understanding of containerisation

The Approach

We’ll examine why changes in one container don’t automatically appear in another, then solve it with shared volume mounts.

Step 1: Understand the Problem

You have an app container that builds assets and an nginx container that serves them. But changes in app don’t reflect in nginx. Why?

The answer: Volume Mounts vs Bind Mounts

Step 2: Know the Difference

Bind Mounts map host filesystem to container filesystem:

volumes:
  - ./storage:/var/www/storage  # Host path on left
  • Tied to host directory structure
  • Changes sync both ways
  • Good for development

Volume Mounts are managed by Docker:

volumes:
  - app-public:/var/www/public  # Named volume
  • Docker manages the storage location
  • Optimised for performance
  • Good for production data

Step 3: Share Data Between Containers

Use a named volume that both containers mount:

version: '3'
services:
  app:
    image: app-image:latest
    volumes:
      - app-public:/var/www/public

  nginx:
    image: nginx:alpine
    volumes:
      - app-public:/var/www/public:ro  # Read-only in nginx

volumes:
  app-public:
    driver: local

Now both containers see the same files.

Step 4: Multi-Stage Dockerfile

Copy built assets into the shared volume:

FROM node:18 as builder
WORKDIR /app
COPY . .
RUN npm install && npm run build

FROM php:8.2-fpm
WORKDIR /var/www
COPY --from=builder /app/public /var/www/public

The Result

  • App container builds assets
  • Both containers mount the same named volume
  • Nginx serves the latest version without manual syncing

What I’d Do Differently

Use named volumes from the start instead of bind mounts. They’re more portable and perform better, especially on macOS where bind mount performance is notoriously poor.

This took me about an hour to understand why my nginx wasn’t seeing updated assets. If it helped you, let me know on Twitter/Bluesky.

Available for rescue and re-platforming work

I take over platforms that already exist and are in trouble. Node, TypeScript, React and Laravel, mostly in regulated or high-traffic environments. If something needs rescuing, re-platforming or finishing, my full history is on the CV.

Related Posts

Comments