Module 9 Lesson 2: Named vs. Anonymous Volumes
·DevOps

Module 9 Lesson 2: Named vs. Anonymous Volumes

Master the naming conventions of storage. Learn when to use descriptive named volumes and when to let Docker manage anonymous storage for temporary tasks.

Module 9 Lesson 2: Named vs. Anonymous Volumes

The choice between a Named and an Anonymous volume determines how you interact with that data once the container is gone.

1. Named Volumes (The "Professional" Way)

Named volumes have a human-readable name you specify (e.g., db-backups).

  • Persistence: They stay on your disk until you explicitly delete them (docker volume rm).
  • Reusability: You can stop a container and start a totally different one using the same named volume.
  • Discovery: In docker volume ls, they are easy to find and monitor.
docker run -v my-cool-data:/app/data my-image

2. Anonymous Volumes (The "Auto-Generated" Way)

Anonymous volumes are created by Docker when you specify a destination path but no source name.

docker run -v /app/data my-image
  • Persistence: They still stay on your disk! They do NOT disappear when the container stops.
  • The Problem: They are given a random hash as a name (e.g., af392b...). In a week, you will have 50 of these on your computer and no idea which one contains your data.
  • Cleanup: They are only deleted if you run docker rm -v (to remove the container and its volume) or docker volume prune.

3. Why Use Anonymous Volumes?

If they are so hard to track, why do they exist?

  1. Image Defaults: A Dockerfile author can use the VOLUME instruction to ensure that a location (like a database folder) is always saved to a volume, even if the user forgets to name it.
  2. Hiding Folders: In development, we use anonymous volumes to "Protect" the container's version of a folder (like node_modules) from being overwritten by a bind-mounted host folder.

4. Summary of Differences

FeatureNamed VolumeAnonymous Volume
Creationdocker volume create or -v name:/path-v /path or Dockerfile VOLUME
ManagementEasy (via name)Hard (via random hash)
LifecyclePersistent until manual deletePersistent until manual delete
VisibilityClearly listed in CLILists as a long random string
Best For...Production Databases / BackupsInternal image logic / Dev protection

Exercise: The Volume Hunter

  1. Run docker run -d -v /data alpine sleep 1000. You just created an anonymous volume.
  2. Run docker volume ls. Find the new "Long String" name.
  3. Stop and remove the container.
  4. Run docker volume ls again. Is the volume still there? (Spoiler: Yes!).
  5. Why is this a potential problem for your computer's disk space over time?
  6. Cleanup: Run docker volume prune to delete all the "Hanging" anonymous volumes.

Summary

In 99% of cases, you should use Named Volumes. They provide clarity, prevent accidental data loss, and make your infrastructure self-documenting. Use Anonymous volumes only when the internal logic of the image requires a persistent folder you don't care about managing yourself.

Next Lesson: Peer through the data: Inspecting and managing volumes.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn