Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker Lab, Excercise & Assignment – 7 – Docker Volume

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

  1. Create a volume:docker volume create mydata
    • Check with:
      docker volume ls
  2. 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.
  3. Inside the container, add a file: echo "This is a test file" > /data/testfile.txt exit
  4. Remove the container: docker rm vol-test1
  5. 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

  1. Start a container with an anonymous volume: docker run -it -v /anondata ubuntu:latest bash
  2. Inside the container: echo "Anonymous volume" > /anondata/anon.txt exit
  3. Check volume list:docker volume ls
    • A random volume will have been created.
  4. 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

  1. On your host, create a directory: mkdir ~/docker-host-dir echo "Hello from the host" > ~/docker-host-dir/hostfile.txt
  2. Run the container with bind mount: docker run -it --name bind-test -v ~/docker-host-dir:/mnt ubuntu:latest bash
  3. Inside the container: cat /mnt/hostfile.txt echo "Hello from container" > /mnt/containerfile.txt exit
  4. 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

  1. Run the container: docker run -it --tmpfs /mnt/tmpfs:rw,size=64m ubuntu:latest bash
  2. Inside the container: echo "Ephemeral data" > /mnt/tmpfs/ephemeral.txt ls /mnt/tmpfs exit
  3. Restart container (or run new one):
    • The /mnt/tmpfs folder will be empty; tmpfs data vanishes after the container stops.

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

  1. Create a named volume: docker volume create sharedvol
  2. 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
  3. 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 TypeCommand ExampleData PersistenceHost Path ControlUse Case
Named Volume-v mydata:/dataYesNoStandard, portable data storage
Anonymous Volume-v /dataYesNo (random name)Short-lived, quick storage
Bind Mount-v /host/dir:/dataYesYesHost-managed data, dev/testing
tmpfs Mount--tmpfs /dataNoNoEphemeral, 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:

  1. 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
  2. Write different files to both directories, then restart the container and verify persistence.
  3. Backup both the named volume and the bind mount to your host system.

End of Lab


Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.