A Docker container restarts and its data is gone. How do you fix it?
Quick Answer
Container filesystems are ephemeral — anything written inside a container is lost when it is removed/recreated. Persist data by mounting a Docker named volume or a bind mount for the paths that must survive.
Detailed Answer
The container writable layer is discarded on removal, so databases and stateful apps must store their data on a volume. Mount a named volume at the data path (for example -v pgdata:/var/lib/postgresql/data) so restarts and image updates keep the data. This is exactly why StatefulSets use PersistentVolumes in Kubernetes.
Code Example
docker run -v pgdata:/var/lib/postgresql/data postgres:16
Interview Tip
Explain that the container layer is ephemeral by design and volumes are the fix — then connect it to PVs in Kubernetes.