Docker Compose — Hot Reload & Bind Mounts

1 minute read

Bind mounts are the classic way to get live code updates in development — mount your source code directly into the container.

Basic Bind Mount

services:
  api:
    build: ./api
    volumes:
      - ./api/src:/app/src        # Mount source code
    ports:
      - "3000:3000"
    command: npm run dev           # Use dev server with hot reload

Now when you edit ./api/src/server.js on your host, the change is immediately visible inside the container.

Node.js with Nodemon

services:
  api:
    build: ./api
    volumes:
      - ./api:/app
      - /app/node_modules         # Anonymous volume to protect node_modules
    command: npx nodemon server.js
    ports:
      - "3000:3000"

The anonymous volume /app/node_modules prevents the host’s node_modules from overriding the container’s.

Python with Auto-Reload

services:
  api:
    build: ./api
    volumes:
      - ./api:/app
    command: python -m flask run --reload --host=0.0.0.0
    ports:
      - "5000:5000"
    environment:
      FLASK_DEBUG: "1"

React / Vite Dev Server

services:
  web:
    build: ./web
    volumes:
      - ./web/src:/app/src
      - /app/node_modules
    command: npm run dev -- --host 0.0.0.0
    ports:
      - "5173:5173"
    environment:
      CHOKIDAR_USEPOLLING: "true"    # Required on some OS

Common Issues and Fixes

Issue Fix
node_modules conflict Anonymous volume: /app/node_modules
File changes not detected Set CHOKIDAR_USEPOLLING=true
Permission errors Match UID/GID in container
Slow on macOS Use :cached flag or Compose watch
Build artifacts conflict Use .dockerignore

Performance Tuning (macOS)

services:
  api:
    volumes:
      - type: bind
        source: ./api/src
        target: /app/src
        consistency: cached    # Improve macOS performance

Summary

Framework Hot Reload Command
Node.js npx nodemon server.js
Python Flask flask run --reload
Python Django python manage.py runserver
React/Vite npm run dev -- --host 0.0.0.0
Go (Air) air

Quick Reference

Item Detail
Bind mount - ./src:/app/src
Protect node_modules - /app/node_modules (anonymous)
Polling CHOKIDAR_USEPOLLING=true
macOS perf consistency: cached
Hot reload Use framework dev server command