MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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


Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Install Jenkins with Docker: Persistent Setup, Port Mapping, and Logs

Learn how to run Jenkins in Docker with correct port mapping, persistent storage, and operational commands. This updated guide keeps the original workflow but adds modern, production-minded practices.

Read More

Install Atlassian Jira with Docker: Quick Start, Persistent Volumes, and Deployment Checks

This guide shows how to run Jira in Docker using the original one-line command, then improve it with persistent storage and repeatable deployment steps. It also includes a workflow diagram, command examples, and practical validation checks.

Read More

Freeing GB on Your Mac: Cleaning Docker, Brew, and Dev Tools

As a developer, there’s that one afternoon, mid-sprint, when a build fails. Not because of bad code, but because your Mac has run out of disk space….

Read More

Docker commands Guide – docker volume with examples

Here’s a complete tutorial on docker volume, covering what it does, examples, and use cases. What is docker volume? docker volume is a Docker command used to…

Read More

Docker Lab and Excercise

Docker Lab, Excercise & Assignment – 1 Docker Lab, Excercise & Assignment – 2 Docker Lab, Excercise & Assignment – 3 – Docker Volume Docker Lab, Excercise…

Read More

Docker Tutorials: Volume – Deep Dive

In order to understand what a Docker volume is, we first need to be clear about how the filesystem normally works in Docker. Docker images are stored…

Read More