Skip to content

Persisting Container Data

Vishwas P edited this page Oct 22, 2024 · 4 revisions

Data Persistence in Docker

Docker lets us run containers as isolated, immutable artifacts, shipping everything required to run them on any host with Docker installed. By default, Docker containers are ephemeral meaning any data stored in the container is lost once it's closed.

However, if you're running an application like a database in a container, you wouldn't want to start with an empty database each time. For these use-cases, Docker provides two primary methods of persisting data within a container.

Bind Mounts

Bind Mounts let you mount a file or folder from your host machine into a container. These can be referenced through an absolute or relative path. These do not need to exist on the host and may be created on demand but are not directly managed by Docker.

Volume Mounts

Volumes are fully created and managed by Docker and are stored in a directory on the Docker. Volumes can be used for multiple containers, and persist even if their containers are stopped.

This should be your primary persistence method on Windows/Mac as they have significantly better I/O on volume mounts compared to other mounts.

tmpfs Mounts (Linux Only)

Volumes and Bind Mounts let you persist data between a host computer and a container even after a container is stopped.

By default, containers with no volumes are ephemeral and but still perform R/W activities on a OverlayFS typically stored in the host's storage. A tmpfs mount instead stores any ephemeral layers in the host's memory.

In certain applications, using a tmpfs mount may be preferable for storing sensitive files, performance improvements and automatic cleanup.

Mounts Diagram

Types of Mounts in Docker

Image source: Docker Documentation: Storage