Volumes & Storage

How to Mount Volumes and Bind Mounts in Docker

Persist database files and hot-reload local source code inside containers using named volumes and host bind mounts.

Quick Docker Solution
Safe CommandIntermediate
docker run -v my_volume:/var/lib/data -v $(pwd):/app -p 3000:3000 my-image
When to use this scenario:You want your database records to survive container restarts, and you want live code changes on your laptop to instantly reflect inside the container.

Step-by-Step Execution Guide

1Named Volume (Best for database persistence)
docker run -d --name db -v pg_data:/var/lib/postgresql/data postgres:16

Docker manages the pg_data volume lifecycle on the host, ensuring high I/O performance and data persistence.

2Bind Mount (Best for local dev hot-reloading)
docker run -it -v $(pwd):/app -w /app node:20 npm run dev

Mounts the current host working directory into /app inside the container.

3Read-only mount (Maximum security)
docker run -v $(pwd)/config:/app/config:ro my-app

:ro flag prevents the container from modifying host files.

4Modern --mount syntax (Recommended by Docker)
docker run --mount type=bind,source="$(pwd)",target=/app my-image

Explicit key-value syntax with clear error messages.

Critical Docker Pitfalls & Precautions

  • β€’On Windows WSL2, mounting files from /mnt/c/... is significantly slower than storing files inside the Linux WSL filesystem (~/project).
Frequently Asked Questions

Mount Volumes & Bind Mounts - Frequently Asked Questions

Common questions about container isolation, signal handling, and runtime behavior.

Named Volumes are managed entirely by Docker in /var/lib/docker/volumes and are portable across environments. Bind Mounts link an exact directory on your host machine to the container.