• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 3: Bind Mounts vs. Volumes

Lesson 3: Bind Mounts vs. Volumes

3 min read

Introduction

In the previous lesson, we learned about Docker Volumes for persistent data storage. However, Docker offers another mechanism for external data management: Bind Mounts. While both serve to store data outside a container, they have distinct use cases and characteristics. This lesson will compare and contrast bind mounts and volumes, helping you choose the appropriate method for different scenarios.

Key Concepts

Bind Mounts

Bind mounts allow you to mount a file or directory from the host machine's filesystem directly into a container. The host path is specified explicitly. Docker does not manage the bind mount's lifecycle, meaning changes on the host are immediately reflected in the container, and vice-versa.

  • Host-managed: The host filesystem is the source of truth; Docker doesn't manage its creation or deletion.

  • Use Cases: Ideal for development environments (mounting source code), configuration files, or situations where the host needs direct access to container data.

  • Portability: Less portable than volumes because they rely on a specific host filesystem structure.

  • Performance: Can be slightly slower for I/O heavy operations compared to volumes in some scenarios.

Docker Volumes (Recap)

As discussed, Docker Volumes are Docker-managed storage located in a specific part of the host filesystem (/var/lib/docker/volumes). Docker controls their creation, deletion, and provides commands for inspection.

  • Docker-managed: Docker handles the lifecycle, making them easier to manage.

  • Use Cases: Preferred for persisting application data (e.g., databases), reusable data, and situations where host path isn't critical.

  • Portability: Highly portable, as Docker handles the underlying storage location.

  • Performance: Generally performs better for many write-heavy workloads.

Key Differences and When to Use Which

| Feature | Docker Volumes | Bind Mounts | | :---------------- | :---------------------------------------------- | :------------------------------------------- | | Management | Docker manages lifecycle (creation, deletion) | Host manages lifecycle | | Location | Managed part of Docker storage (/var/lib/docker/volumes) | Arbitrary location on host filesystem | | Content Init | Can pre-populate if container path is empty | Host content always takes precedence | | Portability | More portable (abstracts host path) | Less portable (depends on host path) | | Development | Good for database data, shared libraries | Excellent for mounting source code, configs | | Production | Preferred for persistent application data | Less common for production data, except configs |

Example/Code

Using a Bind Mount

Let's assume you have a local directory ./my-app/data on your host machine. You want to mount this directory into an Nginx container to serve custom content.

  1. Create a simple HTML file on your host: echo '<h1>Hello from Bind Mount!</h1>' > ./my-app/data/index.html

  2. Run Nginx with a bind mount: The -v flag is overloaded; host_path:container_path indicates a bind mount, while volume_name:container_path indicates a volume.

    bash
    undefined

docker run -d
--name nginx-bind
-p 8080:80
-v $(pwd)/my-app/data:/usr/share/nginx/html
nginx ``` (Note: $(pwd) resolves to the current working directory on Linux/macOS.

For Windows, use %cd% or the full path like `C:

\path\to\my-app\data`).

Now, navigate to `http:

//localhost:8080, and you should see "Hello from Bind Mount!". If you change index.html` on your host, the container will reflect the change immediately.

Summary/Key Takeaways

  • Bind mounts link a specific host path to a container path, useful for development and configuration.

  • Docker volumes are Docker-managed storage, preferred for application data persistence and better portability.

  • Choose volumes for application data and Docker's management benefits; choose bind mounts when you need direct host access or for development workflows.

End of lesson
👏Well done!
Previous Lesson
Lesson 2: Persistent Data with Docker Volumes
Next Lesson
Lesson 4: Docker Compose for Multi-Container Applications

Course Content

0% Complete0/20 Lessons

Lesson 1: Introduction to Containers and Docker

Lesson 2: Installing Docker and Basic Commands

Lesson 3: Docker Images and Registries

Lesson 4: Running Containers and Port Mapping

Quiz

Lesson 1: Docker Container Networking

Lesson 2: Persistent Data with Docker Volumes

Lesson 3: Bind Mounts vs. Volumes

Lesson 4: Docker Compose for Multi-Container Applications

Quiz

Course Content

0% Complete0/20 Lessons

Lesson 1: Introduction to Containers and Docker

Lesson 2: Installing Docker and Basic Commands

Lesson 3: Docker Images and Registries

Lesson 4: Running Containers and Port Mapping

Quiz

Lesson 1: Docker Container Networking

Lesson 2: Persistent Data with Docker Volumes

Lesson 3: Bind Mounts vs. Volumes

Lesson 4: Docker Compose for Multi-Container Applications

Quiz