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.
docker run -v my_volume:/var/lib/data -v $(pwd):/app -p 3000:3000 my-imageStep-by-Step Execution Guide
docker run -d --name db -v pg_data:/var/lib/postgresql/data postgres:16Docker manages the pg_data volume lifecycle on the host, ensuring high I/O performance and data persistence.
docker run -it -v $(pwd):/app -w /app node:20 npm run devMounts the current host working directory into /app inside the container.
docker run -v $(pwd)/config:/app/config:ro my-app:ro flag prevents the container from modifying host files.
docker run --mount type=bind,source="$(pwd)",target=/app my-imageExplicit 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).
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.
Related Docker Command Guides
Browse All Docker RecipesOpen an interactive terminal shell session inside a live running Docker container to inspect files, debug processes, or test network connectivity.
Reclaim gigabytes of hard drive space by removing stopped containers, unused networks, dangling images, and build caches.
Instantly stop all active containers and optionally remove them with a single command.
Copy files or directories between a Docker container and your local host file system without creating a volume mount.
Understand the fundamental differences between ENTRYPOINT and CMD directives and how to combine them for flexible container CLIs.
Inspect stdout and stderr output from containers with timestamps, tail line limits, and live streaming.