Hello! Welcome to our new module, "Advanced Networking and Data Persistence."
In our last lesson, we focused on cleaning up your Docker environment with docker system prune. A key takeaway was the importance of the --volumes flag and the potential danger of accidentally deleting persistent data. This brings us directly to today's topic: understanding how Docker handles data that needs to outlive a single container.
This lesson addresses the learning outcome: Differentiate between volumes, bind mounts, and tmpfs mounts for stateful applications. Understanding these storage options is fundamental for building robust, stateful applications and is a critical skill for any DevOps professional managing containerized services.
1. The Problem: The Ephemeral Container
By default, any data a container writes is stored in its writable layer. As you may recall, this layer is tied directly to the container's lifecycle. When the container is removed, this writable layer and all the data within it are destroyed. This is great for stateless applications but a major problem for anything that needs to maintain state, such as a database or an application that handles user uploads.
Free Docker Fundamentals Course - Docker Storage - Writable Layer, Bind Mounts & Volumes
To start, let's briefly review how this writable layer works and why it's not suitable for persistent data. The video 'Free Docker Fundamentals Course - Docker Storage' provides a clear explanation.
Watch the first two minutes of this video (00:00 - 02:06). Focus on the explanation of the 'writable layer' and why its lifecycle is linked to a single container.
To solve this problem, Docker provides three mechanisms for mounting external data into a container, decoupling the data's lifecycle from the container's.
2. An Overview of Docker Storage Options
The three primary ways to get persistent or specialized storage into a container are:
- Volumes: Storage managed by Docker itself, living within a dedicated area on the host's filesystem. This is the preferred method for most use cases.
- Bind Mounts: A direct mapping of a file or directory from the host's filesystem into the container.
- tmpfs Mounts: An in-memory storage option that does not persist on the host's disk.
The following diagram provides a high-level conceptual overview of how these three types connect a container to the host system.

Now, let's dive into the specifics of each one to understand their behavior, use cases, and trade-offs.
3. Deep Dive into Storage Types
We'll use the article "A Practical Guide Docker Storage" as our primary reference. It provides an excellent, detailed breakdown of each storage type from a practical, professional perspective.
A Practical Guide Docker Storage: Volumes, Bind Mounts & tmpfs
This guide offers a thorough comparison of Docker's storage options. We will refer to different sections of it as we explore each type.
For now, just get familiar with the document. We will read specific sections next.
A. Volumes (The Recommended Approach)
Volumes are the standard, first-class way to persist data generated by and used by Docker containers. The actual data is stored in a part of the host filesystem that is managed entirely by Docker (e.g., /var/lib/docker/volumes/ on Linux). You don't need to know the exact location; you simply refer to the volume by a name you give it.
Key Characteristics:
- Managed by Docker: Docker handles the creation, management, and cleanup of volumes.
- Portable: Since you refer to volumes by name and Docker manages the underlying storage, your container configuration is not tied to a specific host path. This makes your
docker-compose.ymlordocker runcommands more portable across different environments. - Safe: Volumes are not deleted when a container is removed. You have to explicitly use
docker volume rmordocker system prune --volumes. - Good Performance: Volumes offer reliable and good I/O performance.
A Practical Guide Docker Storage: Volumes, Bind Mounts & tmpfs
Read the section on Volumes in the guide we just opened. It covers the core concept and provides a clear example.
Read section 'A) Volumes (recommended for persistent data)'. Pay attention to the pros, cons, and the 'When to use' recommendation.
When to use Volumes: Storing database files, user uploads, application state—essentially any data that must persist beyond the life of a container in a production or production-like environment.
B. Bind Mounts (The Developer's Friend)
A bind mount maps a file or directory from your host machine directly into a container. You control the exact source path on the host. While powerful, this creates a tight dependency on the host's directory structure.
Key Characteristics:
- Host-Dependent: The container's functionality depends on a specific file or directory existing on the host, which reduces portability.
- Direct Access: You can directly see and edit files on your host, and the changes are immediately reflected inside the container.
- Potential for Issues: Can lead to permission conflicts between the host user and the container user. On Docker Desktop (macOS/Windows), file I/O performance can be slower than with volumes because it has to cross a virtualization boundary.
A Practical Guide Docker Storage: Volumes, Bind Mounts & tmpfs
Now, let's read the section on Bind Mounts in the same guide. It highlights the key use case and the associated trade-offs.
Read section 'B) Bind mounts (map host paths into container)'. Note the primary use case for local development and the performance considerations.
When to use Bind Mounts: The primary use case is local development. For example, mounting your application's source code into a container so you can edit the code on your host and see the changes live without rebuilding the image. It's also useful for injecting configuration files from the host into a container.
C. tmpfs Mounts (The Fast and Ephemeral Option)
Unlike volumes and bind mounts, tmpfs mounts are not persistent. They store data in the host's system memory (RAM) instead of on disk. When the container stops, the tmpfs mount is removed, and all data within it is gone.
Key Characteristics:
- Extremely Fast: Since data is written to RAM, I/O is nearly instantaneous.
- Non-Persistent: Data does not survive container restarts.
- Secure for Sensitive Data: Ideal for storing temporary, sensitive information (like tokens or private keys) that you do not want written to disk.
A Practical Guide Docker Storage: Volumes, Bind Mounts & tmpfs
Finally, read the section on tmpfs mounts. This one is simpler but serves a very important niche.
Read section 'C) tmpfs mounts (in-memory, ephemeral)'. Focus on its volatile nature and the use cases for speed and security.
When to use tmpfs Mounts: Caches, temporary log files, session stores, or any situation where you need a high-speed temporary scratchpad or want to avoid writing sensitive data to disk.
4. Comparison and Decision Guide
Now that we've covered each type, how do you choose the right one? The decision hinges on your specific requirements for persistence, portability, and performance.
The guide we've been using has two excellent sections to help you with this.
A Practical Guide Docker Storage: Volumes, Bind Mounts & tmpfs
Let's consolidate our understanding. This article provides a fantastic summary table and a decision guide to help you choose the right storage option for any scenario.
First, study the 'Quick reference comparison' table. It's a perfect summary of the differences. Then, review the 'Choosing the right option (decision guide)' section, which provides a simple question-based framework for making a choice.
This decision framework is something you'll use constantly as a DevOps engineer when designing containerized solutions.
Test your understanding!
You are tasked with containerizing a web application stack for both local development and production deployment. The stack consists of:
- A Node.js web server.
- A PostgreSQL database.
- A service that generates large, temporary report files that should not be saved.
How would you use Docker's storage options to handle the following data for the local development environment?
- The Node.js application source code.
- The PostgreSQL database files.
- The temporary report files.
Show answer
Here's an ideal setup for the local development environment:
- Node.js source code: Use a bind mount. This allows you to edit the code on your host machine with your favorite IDE and have the changes instantly reflected in the running container, enabling a fast development loop.
- PostgreSQL database files: Use a named volume. While you could use a bind mount, a named volume is safer and more idiomatic. It ensures your database data persists reliably between container runs without cluttering your project's source directory or being tied to a specific host path.
- Temporary report files: Use a tmpfs mount. This is perfect for data that is both temporary and performance-sensitive. The reports will be written to memory, which is very fast, and they will be automatically cleaned up when the container stops, preventing disk clutter.
Conclusion
You have now learned to differentiate between the three core storage strategies in Docker. This knowledge is crucial for moving beyond simple, stateless containers and building complex, stateful applications.
Key Takeaways:
- Volumes are the robust, portable, and Docker-managed solution for persisting application data like databases. Default to these for production.
- Bind Mounts are a direct link to the host filesystem, making them ideal for local development workflows where you need to sync source code or configuration files.
- tmpfs Mounts provide high-speed, non-persistent storage in RAM, perfect for caches, temporary files, or sensitive data you don't want written to disk.
In our next lesson, we will build directly on what you've learned about volumes. We'll explore how to create, manage, and share named volumes between multiple containers, a common pattern for multi-service applications.