Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Here’s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar 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…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Here’s an in-depth, step-by-step tutorial and lab manual for Docker Networking—starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

Read More

Docker Lab & Assignment – Creating an Image

Here’s a detailed Tutorial and Lab Guide for the listed Docker commands, focused on practical usage, explanations, and step-by-step labs with Ubuntu + git + apache2 as…

Read More