The Ultimate Guide to Docker Volumes

Mohammed Affan
AWS in Plain English
6 min readAug 25, 2023

--

Docker, the popular containerization platform, has revolutionized the way applications are developed, deployed, and managed. One of its key features is Docker volumes, which allow you to manage data persistence and data sharing between the containers and the host system. In this guide, you’ll dive into the Docker volumes, exploring what they are, why they are important, and how to effectively use them in your containerized applications.

Table of contents:

  • Prerequisites
  • What are the docker volumes 🤔️?
  • Docker volumes allow you to 🔓
  • How do Docker volumes work 🤔️?
  • Types of Docker volumes 💿
  • Using Docker volumes 💿
  • Best practices 💡
  • Conclusion 🎬
  • Last words 💬

Prerequisites 🔑:

What are the Docker volumes 🤔️:

Docker containers are ephemeral. That means the containers last for a short period. Containers are so flexible. They can be easily created, deleted and scaled as per the need. But what If your application needs to store some data? And the container gets removed, or even worse if it somehow crashes? Will you be able to get your data back? The answer is NO! The data inside the container gets lost if it gets deleted or re-created.

While docker’s flexible and ephemeral nature makes it easy to scale, it also creates a problem for data persistence. To address this issue of data persistence docker volumes come to rescue.

Docker volumes allow you to 🔓:

  1. Persist data: Volumes ensure that important data, like databases, configuration files, or user uploads, are retained even when the container is updated, replaced, or removed.
  2. Share data: Volumes enable data sharing and communication between containers, making it possible for containers to collaborate and exchange information.
  3. Backup and restore: Since data in volumes is decoupled from the container, you can easily back up and restore data independently of the container’s life-cycle.
  4. Manage stateful applications: Volumes are crucial for stateful applications like databases, where data integrity and persistence are essential.

How do Docker volumes work 🤔️?

Docker containers run on a host machine. The Docker volume utilizes the host machine’s file system for data persistence. It mounts a path of the host machine’s file system into the container’s file system like the following image.

Types of Docker volumes 💿:

Docker offers various types of volumes to cater to different use cases and scenarios:

  1. Bind mounts/host volumes: Bind mounts or host volumes map a directory or file from the host machine into a container. Changes made in the container are immediately visible on the host and vice versa. Bind mounts are useful for development or when you want to share data between the host and containers.

2. Anonymous volumes: These volumes are created and managed by Docker itself. But they don’t have a name, instead of a proper name they use a random hash. They are usually created automatically by Docker and are associated with a container’s filesystem. Anonymous volumes are particularly useful when you want to store non-persistent or temporary data that doesn’t need to be shared between containers.

3. Named volumes: These volumes are also created and managed by Docker itself. They have a user-friendly name and can be shared among multiple containers. Named volumes are preferred for most use cases due to their ease of use and management.

4. tmpfs mounts: tmpfs mounts store data in the host's memory (RAM) instead of the file system. The tmpfs mounts are temporary. This is ideal for temporary data that you don't need to persist across container restarts. Unlike volumes and bind mounts, you can't share tmpfs mounts between containers. The tmpfs mounts are only available for Linux hosts.

Volume plugins: Docker allows you to use third-party volume plugins to integrate with external storage systems such as Amazon EBS. These plugins can provide advanced features like replication, backup, and integration with various cloud storage solutions. Refer to the official documentation to know more about the Docker volume plugin for Amazon EBS.

Using Docker volumes 💿:

Here’s a step-by-step guide on how to effectively use Docker volumes:

1. Creating a named volume:

You can create a named volume using the following command:

docker volume create my_data_volume

2. Attaching a volume to a container:

When running a container, you can attach a volume using the -v flag:

docker run -d -v my_data_volume:/app/data my_image

(-d flag is for running the container in detached mode)

3. Using bind mounts:

For bind mounts, you directly specify the host directory and the container directory:

docker run -d -v /home/data:/app/data my_image

4. Inspecting volumes:

You can inspect volume details using:

docker volume inspect my_data_volume

5. Backup and migration:

When it comes to Docker volumes, you can effortlessly create data backups by duplicating the content of the volume from the host machine’s directory to another location or device. You can even move volumes to another Docker environment.

Best practices 💡:

To make the most of Docker volumes, consider these best practices:

  1. Use named volumes: For most scenarios, named volumes are preferred due to their ease of management.
  2. Separation of concerns: Divide your application’s data into distinct volumes based on their roles (e.g., database data, user uploads).
  3. Backup regularly: Regularly back up your data volumes to prevent data loss.
  4. Version your volumes: When making changes to your application, consider versioning your backup volumes like ‘my-data-v1’ and ‘my-data-v2’. If something goes wrong, you can revert a volume from a specific version.
  5. Automate Volume Creation: Use Docker Compose or orchestration tools such as Kubernetes to automate volume creation along with container deployment.

Conclusion 🎬:

You can use the Docker volumes for data persistence and sharing. Docker volumes offer a spectrum of volume types like named volumes, bind mounts, tmpfs mounts, and anonymous volumes. Named volumes ensure data persistence, bind mounts enable data sharing with the host, tmpfs mounts store data in memory, while anonymous volumes automate transient storage.

Additionally, you also have the option to utilize cloud storage as a Docker volume through the implementation of Docker volume plugins. The choice of the appropriate Docker volume type for your application depends on your application’s specific requirements.

Last words 💬:

Congratulations on completing this guide! 🥳️ 🎉️

Thank you for reaching the end of this article. If you found it helpful, consider showing your appreciation with a few claps!

For more insights into Docker 🐳, explore my other articles. If cloud technologies or DevOps fascinate you, feel free to follow my profile. If you have any questions or queries, drop them in the comments — I typically respond within a day.

I look forward to connecting with you on LinkedIn: https://www.linkedin.com/in/mohammedaffan7/

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.

--

--

DevOps Engineer♾️| Cloud Computing☁️| Linux🐧| AWS🖥 | Python🐍| Docker⚓| Kubernets🛞| Terraform⚙️| Connect with me👇 www.linkedin.com/in/mohammedaffan7