Docker interview question · Most asked
Volumes, bind mounts and tmpfs: when do you use each?
Summary
Named volumes are managed by Docker and are the right choice for persistent container data (databases in development, uploaded files); bind mounts map a host path into the container, ideal for development (live-editing source code) or specific host files; tmpfs mounts keep data in memory only, for temporary or sensitive data.
Longer Answer
Data in a container's writable layer disappears with the container and is slower (copy-on-write), so anything that must persist goes in a volume. Bind mounts depend on the host's directory layout and permissions (UID mismatches between host and container are a common annoyance) and can expose host files, so avoid them in production. In orchestrators, persistent storage is provided by the platform (Kubernetes PersistentVolumes, EFS for ECS).
Key Points
- Volumes: persistent, Docker-managed.
- Bind mounts: dev and host files.
- tmpfs: memory only.
- Writable layer is ephemeral.
More most-asked Docker questions
- What is the difference between an image and a container, and how do image layers work?
- What is a container, and how does it differ from a virtual machine?
- What are multi-stage builds, and why use them?
- CMD or ENTRYPOINT: what is the difference, and what are exec and shell forms?
- How does Docker build caching work, and how do you order a Dockerfile to use it well?
- What are Dockerfile best practices for production images?
- ARG or ENV: what is the difference, and how do you pass secrets into a build safely?
- How do you secure Docker containers and images?