Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of Docker volumes, their use cases, and gives you example commands and exercises for each. You’ll understand:
- What are Docker volumes?
- Types of volumes: named volumes, anonymous volumes, host (bind) mounts, tmpfs mounts
- Volume management (create, inspect, list, prune, remove)
- Data persistence and sharing
- Backing up and restoring volumes
1. Introduction to Docker Volumes
Docker Volumes are the preferred way to persist data in Docker containers. They store data outside the container’s writable layer, surviving container removal, and enabling data sharing between containers.
Why use volumes?
- Persistence: Data isn’t lost when the container is deleted.
- Sharing: Multiple containers can access the same data.
- Performance: Volumes are managed by Docker, optimized for speed.
- Backups & Migration: Easier to backup and move data.
2. Types of Docker Volumes
a. Named Volumes
- Managed by Docker.
- You give them a name.
- Docker stores them under
/var/lib/docker/volumes/
.
b. Anonymous Volumes
- Not named (Docker generates a random name).
- Typically used for quick/temporary storage.
c. Host (Bind) Mounts
- Map a host directory/file into the container.
- Full control; can access any part of your filesystem.
d. tmpfs Mounts
- Stored in memory only (not on disk).
- Data disappears after the container stops.
3. Lab Setup
- Install Docker:
sudo apt-get update sudo apt-get install docker.io -y sudo systemctl start docker sudo systemctl enable docker
- Pull Ubuntu image:
docker pull ubuntu:latest
4. Practical Labs for Each Volume Type
A. Named Volumes
Create and Use a Named Volume
- Create a volume:
docker volume create mydata
- Check with:
docker volume ls
- Check with:
- Run Ubuntu container using the named volume:
docker run -it --name vol-test1 -v mydata:/data ubuntu:latest bash
- This mounts the volume at
/data
inside the container.
- This mounts the volume at
- Inside the container, add a file:
echo "This is a test file" > /data/testfile.txt exit
- Remove the container:
docker rm vol-test1
- Create a new container, attach the same volume:
docker run -it --name vol-test2 -v mydata:/data ubuntu:latest bash
cat /data/testfile.txt
- You should see the content is persisted!
LAB TASK
- Try creating a new file in the
/data
directory from the second container and verify it appears if you mount the volume again in a new container.
B. Anonymous Volumes
Run a Container with an Anonymous Volume
- Start a container with an anonymous volume:
docker run -it -v /anondata ubuntu:latest bash
- Inside the container:
echo "Anonymous volume" > /anondata/anon.txt exit
- Check volume list:
docker volume ls
- A random volume will have been created.
- Find which one:
docker inspect <container-id> | grep Source
LAB TASK
- Remove the container and see if the data persists. (Spoiler: It does, but the volume is harder to track without a name.)
C. Host (Bind) Mounts
Mount a Host Directory into a Container
- On your host, create a directory:
mkdir ~/docker-host-dir echo "Hello from the host" > ~/docker-host-dir/hostfile.txt
- Run the container with bind mount:
docker run -it --name bind-test -v ~/docker-host-dir:/mnt ubuntu:latest bash
- Inside the container:
cat /mnt/hostfile.txt echo "Hello from container" > /mnt/containerfile.txt exit
- Check on the host:
cat ~/docker-host-dir/containerfile.txt
- Both the host and the container can see and edit files in this directory.
LAB TASK
- Open two containers at once (in different terminals) mounting the same host directory. See if changes are visible in both in real time.
D. tmpfs Mounts (Ephemeral Memory Only)
Run a Container with tmpfs
- Run the container:
docker run -it --tmpfs /mnt/tmpfs:rw,size=64m ubuntu:latest bash
- Inside the container:
echo "Ephemeral data" > /mnt/tmpfs/ephemeral.txt ls /mnt/tmpfs exit
- Restart container (or run new one):
- The
/mnt/tmpfs
folder will be empty; tmpfs data vanishes after the container stops.
- The
LAB TASK
- Try storing large files in the tmpfs mount and see what happens if you exceed the size limit.
5. Managing Docker Volumes
List All Volumes
docker volume ls
Inspect a Volume
docker volume inspect mydata
Remove a Volume
docker volume rm mydata
Remove Unused Volumes
docker volume prune
6. Advanced: Sharing Volumes Across Containers
- Create a named volume:
docker volume create sharedvol
- Start a first container writing to the volume:
docker run -it --name writer -v sharedvol:/shared ubuntu:latest bash
echo "Shared data" > /shared/shared.txt
- Start a second container to read from the volume:
docker run -it --name reader -v sharedvol:/shared ubuntu:latest bash
cat /shared/shared.txt
7. Backup & Restore Volumes
Backup a Docker Volume
docker run --rm -v mydata:/data -v $(pwd):/backup ubuntu tar czvf /backup/mydata-backup.tar.gz -C /data .
Code language: JavaScript (javascript)
Restore a Docker Volume
docker run --rm -v mydata:/data -v $(pwd):/backup ubuntu bash -c "cd /data && tar xzvf /backup/mydata-backup.tar.gz --strip 1"
Code language: JavaScript (javascript)
8. Volume Mount Options (Advanced)
- Read-only mount:
docker run -it -v mydata:/data:ro ubuntu bash
- Mount subdirectory of host:
docker run -it -v /home/ubuntu/specific-dir:/data ubuntu bash
9. Summary Table
Volume Type | Command Example | Data Persistence | Host Path Control | Use Case |
---|---|---|---|---|
Named Volume | -v mydata:/data | Yes | No | Standard, portable data storage |
Anonymous Volume | -v /data | Yes | No (random name) | Short-lived, quick storage |
Bind Mount | -v /host/dir:/data | Yes | Yes | Host-managed data, dev/testing |
tmpfs Mount | --tmpfs /data | No | No | Ephemeral, sensitive data, speed |
10. Lab Checklist
- Create, inspect, remove, and prune volumes
- Use named, anonymous, bind, and tmpfs mounts
- Persist data across containers
- Share data between containers
- Backup and restore volume data
- Explore mount options (ro, size, etc.)
Challenge Lab
Try the following scenario:
- Start a container with both a named volume and a bind mount.
docker run -it -v mydata:/volume-data -v ~/docker-bind:/bind-data ubuntu bash
- Write different files to both directories, then restart the container and verify persistence.
- Backup both the named volume and the bind mount to your host system.
End of Lab

















I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at WIZBRAND