Containers Are Cattle, Data Is Not

The Writable Layer Trap

Scenario: The notes app has run happily for months, saving files inside the container. Tonight's routine redeploy recreates the container… and every user's data is gone. No bug, no attack - just a fundamental misunderstanding of where the data lived.

Every file a container writes goes to its writable layer - which is deleted with the container. This is a feature: containers stay disposable, identical, replaceable ("cattle, not pets"). But it means state must live outside the container:

┌──────────────┐   ┌──────────────┐
│ container v1 │   │ container v2 │   ← created & destroyed freely
└──────┬───────┘   └──────┬───────┘
       │    mounted at /data    │
       ▼                        ▼
  ┌────────────────────────────────┐
  │     volume: notes-data         │   ← survives forever
  └────────────────────────────────┘
$ docker volume create notes-data
$ docker run -d --name notes -v notes-data:/data notes-app:v2
$ docker volume ls
$ docker inspect notes    # "Mounts" section shows what's mounted where
Note: Deleting a container does not delete its named volumes. That's the point - but also remember docker volume prune exists when cleaning up.