Docker Volumes: Named Volumes vs Bind Mounts
Fast recall
Quick revision
Core answer
Understanding of Docker data persistence mechanisms and their appropriate use cases
Using bind mounts in production or CI environments — bind mounts depend on host filesystem paths that may not exist or have different permissions
Discuss tmpfs mounts for sensitive data, explain volume drivers for distributed storage (EFS, NFS), and mention that Spring Boot applications should be stateless — persistent storage belongs to the database container
Interview-ready guide
Interview answer
Docker provides two mechanisms for persisting data outside a container's writable layer: named volumes and bind mounts. They look similar in compose files but have completely different lifecycle characteristics.
Named volumes — Docker-managed persistence
- A named volume (
-v postgres_data:/var/lib/postgresql/data) is created and managed by Docker; it persists independently of any container —docker rmdeletes the container but not the volume - Named volumes are stored in Docker's managed directory (usually
/var/lib/docker/volumes/) and are not directly accessible as a regular filesystem path on the host — usedocker volume inspectto find the mount point - Named volumes are the right default for database data directories in development and for any data that must outlive the container;
docker compose downpreserves named volumes,docker compose down -vremoves them explicitly
Bind mounts — host filesystem projection
- A bind mount (
-v /host/path:/container/path) projects a specific host directory into the container; changes from either side are immediately visible to the other — every file the container writes appears on the host filesystem at the specified path - Bind mounts are the right choice for development workflows where you want live reloading — mount your source directory into the container running a Spring Boot DevTools configuration so code changes reload without a container rebuild
- On macOS and Windows, bind mounts go through a filesystem sharing layer that introduces I/O overhead — file-intensive workloads (database storage, large build caches) should use named volumes instead for performance
Security considerations
- Bind-mounting sensitive host paths (like the Docker socket
/var/run/docker.sock) gives the container root-equivalent access to the host — a container with the Docker socket can start privileged containers, escape the container namespace, and access any data on the host - Named volumes do not grant host filesystem access; they are isolated to the volume's managed directory — prefer named volumes over bind mounts in production-like environments for this reason
Docker Volumes: Named Volumes vs Bind Mounts
Direct Answer
Named volumes are managed by Docker, stored in Docker's storage area, portable across environments, and ideal for stateful data (databases, persistent uploads). Bind mounts map a host directory into the container, are tied to the host filesystem path, and ideal for development (mounting source code for hot reload). Use named volumes in production, bind mounts for local development only.
Common Mistakes & Pitfalls
Using bind mounts in production or CI environments — bind mounts depend on host filesystem paths that may not exist or have different permissions
Check your understanding
Answer these without looking back at the article.
- How do you back up and restore a Docker named volume?
- What happens to a named volume when you run docker-compose down vs docker-compose down -v?
- How would you use a volume driver to mount an EFS filesystem in a Docker container?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.