Bye-Bye Bind Mounts

Author: Stuart Feeser

Understanding Bind Mounts vs. Volume Mounts: Choose wisely

When setting up data storage for Docker containers, you have two primary options: bind mounts and volume mounts. Each approach affects where data is stored, how Docker manages it, and how robust the data storage is in production. Here’s a closer look at both types, along with a breakdown of their storage locations and data fragility.


1. Bind Mounts: Direct Host Paths with Fragility Risks

A bind mount directly links a file or directory on the host to a path in the container. Bind mounts allow your container to read and write directly to specific files or folders on the host filesystem.

Where Is the Data Stored?

In a bind mount, the data resides in a specific host path that you define when setting up the mount. This path can be any directory on the host system, such as /home/user/data or a relative path like ./project-files. You set the exact location in your docker-compose.yml or with the docker run command:

version: '3'
services:
  app:
    image: myapp:latest
    volumes:
      - ./host-content:/app/data  # Bind mount to a specific host directory

In this example, ./host-content on the host is mapped to /app/data in the container. This allows direct access from the container to files in ./host-content.

How Fragile is the Data?

Bind mounts are highly fragile due to their dependency on inodes. When you bind mount a file or directory, Docker links directly to the inode (the unique file identifier in Linux). If the inode of the file or directory changes, the container will lose access to it.

Actions That Break Bind Mounts:

  • Overwriting Files: Copying (cp) or moving (mv) a new file to replace the existing file will change its inode and break the bind mount.
  • Deleting Files: Removing (rm) the file or directory deletes its inode, breaking the bind mount even if you recreate it.
  • Some Text Editors: Editors like vim delete and replace files on save, causing new inodes and breaking the bind mount.

Because of this fragility, bind mounts are often better suited for development or testing environments where the exact location on the host is critical. For production, however, this lack of resilience makes bind mounts a risky choice.


2. Volume Mounts: Docker-Managed Storage with Robustness in Mind

A volume mount is a Docker-managed storage method where Docker itself manages the storage location and ensures data persistence across container restarts.

Where Is the Data Stored?

When using a Docker volume, data is stored in Docker’s managed storage area on the host, usually under /var/lib/docker/volumes/. Docker assigns each volume a unique path, such as /var/lib/docker/volumes/app-data/_data, keeping it independent from the project’s file paths on the host.

You define volumes in docker-compose.yml as follows:

version: '3'
services:
  app:
    image: myapp:latest
    volumes:
      - app-data:/app/data  # Docker-managed volume

volumes:
  app-data:  # Declare the volume
    driver: local

Here, Docker creates a volume named app-data and manages its storage at /var/lib/docker/volumes/app-data/_data. This path is stable, and Docker maintains it even if you remove or restart the container.

How Fragile is the Data?

Docker volumes are much more resilient than bind mounts. Because Docker manages the storage, the volume is isolated from the host’s specific filesystem structure and inode dependencies. This means the data is highly stable and does not break when files are overwritten or moved within the volume.

Benefits of Volume Mounts:

  • Data Persistence: Volumes are retained even if the container is deleted and recreated.
  • Protection Against Inode Changes: Because Docker handles the storage, data integrity is not tied to inode stability on the host.
  • Cross-Platform Compatibility: Volume mounts handle file permissions and access consistently across different environments, reducing potential compatibility issues.

For production environments where data persistence and robustness are priorities, Docker volumes are generally the preferred choice. They provide a reliable way to store data without the fragility associated with bind mounts.


Summary: Choosing Between Bind Mounts and Volume Mounts

Feature Bind Mounts Volume Mounts
Data Location Specific host path Docker-managed path (/var/lib/docker/volumes)
Fragility High: prone to breaking on inode changes Low: resilient to inode and host changes
Best Use Case Development, testing Production
Docker Management None: host-based file control Managed: Docker maintains path and permissions

When it comes to production environments, volume mounts offer greater stability, data persistence, and robustness, making them the better choice. Bind mounts can be helpful for development work but are often too fragile to rely on when inode stability and data integrity are critical.

← Previous