Inside this article
When you replace a container, files stored only inside it are lost. Store application data that must survive replacement outside the container: use a Docker volume or bind mount on a single Docker host, or a PersistentVolumeClaim (PVC) in Kubernetes. A PVC requests storage that Kubernetes matches to a suitable volume.
A separate volume keeps data when you replace a container. It does not automatically back up data, make simultaneous writes safe, or let you move the data between hosts and nodes. Those features depend on the storage backend and how it is configured and managed.
Why container files disappear
Docker keeps files written by a container in that container’s writable layer. Destroy the container and the layer goes with it. A replacement container gets a fresh layer, even when it runs the same image.
Kubernetes has the same basic limit. A Pod’s writable layer and temporary emptyDir storage do not follow a replacement Pod. Kubernetes creates an emptyDir when it assigns a Pod to a node and removes it when the Pod leaves the node. Use it for scratch files or to share files between containers in the same Pod. It is not durable storage. If a container crashes but its Pod stays in place, the emptyDir remains.
A replacement Pod has a new identity and might run on a different node. Files in the old Pod’s writable layer or left on its node do not reliably move with the service. Mount durable storage at the directory where the application writes its data. Files written elsewhere stay in the disposable layer.
Choose Docker or Kubernetes storage
Use a Docker named volume to keep application data outside an individual container and let Docker manage it. The volume stays after you remove the container. It lives on the host running the Docker daemon unless a driver or backend stores it elsewhere. A bind mount exposes a specific host path instead, which suits cases where host tools need direct access to files. That path must exist on the daemon host; with a remote Docker daemon, a path on the client machine is not automatically available.
Docker tmpfs stores files in memory and loses them when the container stops or restarts, or when the host reboots. Kubernetes emptyDir lasts only as long as its Pod remains on its node. Use these ephemeral options for scratch data or temporary files, and accept that the contents will be lost.
For data that must outlast a Kubernetes Pod, create a PVC and choose a StorageClass. The StorageClass tells the cluster how to provide storage. The PVC requests storage, and the PersistentVolume (PV) is the volume Kubernetes provides. Check which classes and features your cluster provides. A PVC does not make storage portable by itself: a volume can be tied to a node or zone, depending on the backend and driver.

Match Kubernetes access modes to the workload
Kubernetes access modes describe which nodes can mount a volume and whether they can write to it. The mode names can be confusing. ReadWriteOnce means read-write mounting by one node, not necessarily one Pod; multiple Pods on the same node can still access the volume. ReadWriteMany allows read-write mounting from multiple nodes when the storage backend supports it. Neither mode guarantees that an application can safely handle simultaneous writes.
If a workload needs a volume restricted to one Pod, Kubernetes’ ReadWriteOncePod mode is designed for that purpose. It requires a CSI storage driver and compatible sidecars. CSI, or Container Storage Interface, is the standard through which storage drivers provide volumes to Kubernetes. Confirm the driver supports the mode before relying on it. Access modes help Kubernetes match claims to volumes, but they generally do not prevent writes after a volume is mounted.
For a Docker bind mount, limit the exposed host path to what the container needs. Bind mounts are writable by default, so a process in the container can change or delete host files. Use a read-only mount when the container only needs to read. Also check the target path: mounting over a non-empty directory hides the directory’s existing contents while the mount is in place.
Check volume deletion and expansion settings
Before deleting a PVC in Kubernetes, check the StorageClass’s reclaim policy. A dynamically provisioned volume follows that policy. If the class does not set one, the policy defaults to Delete. With Delete, releasing the claim deletes the storage resource. With Retain, the resource stays for manual recovery or cleanup. Deleting a PVC while a Pod still uses it can be delayed until no Pod object uses the claim, but that delay is not a guarantee that the storage will be retained afterward.
A StorageClass can also control whether Kubernetes can expand a volume. The supported operation is growing the volume, not shrinking it. The binding mode controls when Kubernetes provides or assigns storage. WaitForFirstConsumer delays this until a Pod uses the claim. This helps when storage depends on the Pod’s location and the driver supports the mode.
Docker cleanup commands also need care. docker volume prune removes unused anonymous volumes by default; adding --all also includes unused named volumes. docker system prune does not prune volumes by default, but adding --volumes removes unused anonymous volumes. Anonymous volumes normally survive container removal, but Docker removes the associated anonymous volume when a container started with --rm exits. Use named volumes for data that must outlast that cleanup behavior, and inspect targets before pruning.
Back up and test recovery
A volume keeps data outside a container, but it does not back up that data. A host or backend failure, accidental deletion, or corruption can still destroy it. Keep backups separate from the live volume and test restoring them into a fresh environment. For databases and other stateful software, use the product’s documented backup procedure when it requires application-aware steps.
Kubernetes supports volume snapshots through CSI drivers that implement snapshot functionality. The cluster also needs the required snapshot components. A snapshot provides a point-in-time copy, but its existence alone does not prove that the application can recover correctly. Check the driver’s capabilities and the application’s guidance, then test the restore.

Control Kubernetes temporary storage
Kubernetes local ephemeral storage includes writable container layers, local emptyDir volumes, logs, and certain system files. These use storage on the node, and a node failure can destroy them. Kubernetes does not guarantee disk performance levels, such as IOPS, for this storage.
A memory-backed emptyDir uses RAM. Set a size limit and memory limits for the Pod so scratch files do not consume unbounded node memory. Kubernetes can evict a Pod when it exceeds its local ephemeral-storage limit, but enforcement depends on supported kubelet and node configuration. A namespace quota for ephemeral storage also requires Pods to set an ephemeral-storage limit.

