Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 189 of 8023. Docker Orchestration and StorageFree lesson

Differentiating Ephemeral and Persistent Container Storage

Learners will identify why default container layers are temporary and why external storage mechanisms are required for stateful application data.

In this lesson, you will learn why the files inside your containers disappear when the container is removed and how to preserve your critical application data.

The Ephemeral Nature of Containers

When you run a container from an image, Docker creates a writable container layer on top of the read-only image layers. This layer is where your application writes logs, temporary files, or cache data. However, this storage is ephemeral. If you stop, remove, or recreate the container (for instance, when updating your image via CI/CD), that writable layer is destroyed along with everything stored inside it.

Think of a container like a whiteboard in a classroom: you can write information on it while the class is in session, but once the class ends and the board is wiped clean, that data is gone forever. If your application needs to save a database, user uploads, or configuration files that must survive a container restart, the default writable layer is not the right tool for the job.

Bridging to Persistence

To solve this, we use external storage mechanisms—like Docker Volumes—to decouple the data from the container’s lifecycle. By mounting a volume, you tell Docker to map a specific directory on your host machine (or remote storage) into the container.

Consider this docker-compose.yml snippet:

services:
  database:
    image: postgres
    volumes:
      - db-data:/var/lib/postgresql/data # Persistence
volumes:
  db-data: # Managed storage outside the container

In this example, even if you run docker compose down and delete the database container, the db-data volume remains intact on the host. When you bring the service back up, the new container mounts the existing volume, and your database picks up exactly where it left off.

By differentiating between what should be thrown away (ephemeral) and what must be saved (persistent), you ensure that your stateful applications are resilient against updates, crashes, and scaling events.

Key Takeaway: Ephemeral storage lives and dies with the container, while persistent storage exists independently, allowing your application data to survive container lifecycles.