Module P-3·25 min read

Bind mounts vs Volumes, managing ephemeral data, and performance implications for databases like PostgreSQL.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Introduction

Containers are designed to be ephemeral. When a container is stopped and removed, everything inside its writable layer (the top layer of the OverlayFS) is permanently destroyed.

If you are running a stateless Next.js frontend or a Node.js API, this is perfectly fine. But what happens when you run a PostgreSQL or Redis container? If the container restarts, you lose your entire database.

In this module, we will explore how to manage stateful data in Docker using Bind Mounts and Volumes, understand their performance implications, and learn when to use which.


The Writable Container Layer (Ephemeral Data)

As we learned in Module 1, when a container starts, Docker creates a thin read-write layer on top of the underlying read-only image layers.

Any files written by your application (e.g., logging a file, uploading an image, writing database rows) are stored in this writable layer.

The critical rule: This layer is tightly coupled to the lifecycle of the container.

  • If the container is stopped (docker stop), the data persists.
  • If the container is removed (docker rm), the data is deleted forever.

To persist data beyond the lifespan of a container, you must bypass the Union File System and store the data directly on the host machine. Docker provides two primary ways to do this: Bind Mounts and Volumes.


1. Bind Mounts

A Bind Mount maps a specific directory on your host machine to a specific directory inside the container.

bash

(The syntax is -v HOST_PATH:CONTAINER_PATH)

Characteristics of Bind Mounts

  • Absolute Control: The host machine dictates the exact file path.
  • Two-Way Sync: Changes made on the host are immediately visible in the container, and vice versa.
  • Host Dependent: The container relies on the host having a specific directory structure. This makes docker run commands less portable across different developer machines or production servers.

When to use Bind Mounts

Bind mounts are almost exclusively used for Local Development.

By bind-mounting your local source code into the container, you can use your local IDE (like VS Code) to edit files. Frameworks like Next.js or tools like nodemon will detect the file changes across the mount and instantly hot-reload the application inside the container.

[!WARNING] Performance on macOS/Windows: Bind mounts on Linux are native and incredibly fast. However, on Docker Desktop for macOS and Windows, the files must cross the boundary between your physical host OS and the hidden Linux VM.

If you bind-mount a massive node_modules folder on a Mac, file I/O operations will be severely penalized, and your app will run incredibly slowly. Never bind-mount node_modules—install them natively inside the image instead!

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.