Guide
Docker
Beginner
9. Volumes and Bind Mounts
Persist application data beyond the lifetime of a container.
Storage Choices
| Storage | Typical use |
|---|---|
| Container writable layer | Disposable runtime files |
| Named volume | Application data managed independently of a container |
| Bind mount | Files at a specific path on the daemon host |
| tmpfs mount | Temporary memory-backed files on supported hosts |
Removing a container removes its writable layer. A named volume survives removal of the container that used it.
Verify Persistence
docker volume create docker-course-data
docker run --rm --mount type=volume,src=docker-course-data,dst=/data busybox:1.37 sh -c 'echo persistent > /data/message.txt'
docker run --rm --mount type=volume,src=docker-course-data,dst=/data,readonly busybox:1.37 cat /data/message.txt
Expected output: persistent. The writer container was removed, but a different container can still read the file.
Common Mistakes
- Writing important data only inside the container layer.
- Mounting a volume at the wrong application path.
- Forgetting that mounting over an image directory hides the files beneath that mount.
- Treating persistence as a backup strategy.
Cleanup
After finishing the exercise, delete its practice data:
docker volume rm docker-course-data
Quick Interview Answer
A volume separates persistent data from container lifetime. A bind mount exposes a chosen host path; a named volume is managed by Docker.
Previous: 8. Networking and Ports | Next: 10. Docker Compose
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form