Day 16 of 100 Days : Understanding Docker Volumes and Bind Mounts for Persistent Storage

Munilakshmi G JMunilakshmi G J
4 min read

Introduction

In Docker, containers are designed to be ephemeral, meaning that when they are stopped or removed, they lose all data stored within them. This can be a problem for applications that need to retain data, such as logs or user data, even after a container is stopped. In this blog, we’ll dive into Docker volumes and bind mounts, two key solutions for persistent storage in Docker environments.

By the end of this blog, you'll understand when and how to use both options and know which one suits your specific use case.

Why Do We Need Persistent Storage in Containers?

Since containers are transient, they don’t retain data once they stop or are removed. This poses a challenge for scenarios like:

  1. Log Storage: Applications (like Nginx) generate logs that should persist across container restarts.

  2. Front-end and Back-end Communication: When back-end services generate data for front-end containers, losing that data during a failure would disrupt the system.

  3. External File Access: Containers often need to read files created by external processes or jobs, which wouldn't be possible without persistent storage.

The Solution: Bind Mounts and Volumes

1. Bind Mounts

Bind mounts allow you to bind a host directory directly to a container directory. This way, any data written inside the container is saved in a location on the host, ensuring it persists even after the container is removed.

  • Pros: Simple to set up and useful when you want to directly control the location of data.

  • Cons: Can be less flexible and harder to manage than Docker volumes.

Example of Bind Mount:

docker run -v /host/directory:/container/directory my_image

This command mounts /host/directory from the host to /container/directory inside the container.

2. Volumes

Docker volumes are managed by Docker, which means they are more flexible and are designed specifically for persistent storage. Volumes can be stored on external storage devices, making backups and data migration easier. You can also manage them using Docker commands, which simplifies the lifecycle of data storage.

  • Pros: Safer, easier to manage, and can be shared across containers.

  • Cons: More abstract and requires understanding Docker’s volume commands.

Example of Volumes:

To mount a volume to a container:

docker run -d --name my_container -v my_volume:/data my_image

This mounts my_volume to /data inside my_container.

Key Differences Between Bind Mounts and Volumes

  • Bind Mounts: Direct mapping between host and container directories.

  • Volumes: Managed by Docker, flexible, and designed for data persistence.

In general, if you’re working on a project where you need clear management, backup, or data sharing across containers, volumes are the better option.

Key Docker Volume Commands

Here are some essential Docker volume commands that you will use frequently in your container management:

  1. List Volumes:
docker volume ls
  1. Create a Volume:
docker volume create my_volume
  1. Inspect a Volume:
docker volume inspect my_volume
  1. Delete a Volume:
docker volume rm my_volume

You can delete multiple volumes at once:

docker volume rm volume1 volume2
  1. Mount a Volume to a Container:
docker run -d --name my_container -v my_volume:/data my_image

Understanding Volume Mount Options

When mounting a volume to a container, you can use two options: -v (short) and --mount (verbose).

Example with -v:

docker run -v my_volume:/data ubuntu

Example with --mount:

docker run --mount source=my_volume,target=/data ubuntu

While both options do the same job, --mount is recommended for clarity and ease of use, especially in larger, collaborative environments.

Key Takeaways

  • Volumes are ideal when you need persistent data storage across container restarts, backups, and high-performance storage.

  • Bind mounts can be useful in certain scenarios where direct control of the data location is needed, but they lack the flexibility of volumes.

  • Always prefer using --mount over -v for better readability in collaborative environments.

Wrap-up

Understanding Docker’s persistent storage solutions—bind mounts and volumes—is crucial for managing data within containers. Volumes are generally the preferred choice for persistent storage, especially when dealing with more complex environments. Keep practicing these commands to get comfortable with them!

0
Subscribe to my newsletter

Read articles from Munilakshmi G J directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Munilakshmi G J
Munilakshmi G J

"Aspiring DevOps Engineer on a 100-day journey to master the principles, tools, and practices of DevOps. Sharing daily insights, practical lessons, and hands-on projects to document my path from beginner to proficient. Passionate about continuous learning, automation, and bridging the gap between development and operations. Join me as I explore the world of DevOps, one day at a time!"