Frequently Asked Question
How do I keep data alive when the container is destroyed?
Anything written inside a container's writable layer disappears when the container
is removed, that is what makes containers cheap and disposable. To keep data, you
attach storage from outside the container's lifecycle. Docker offers two
mechanisms. A bind mount (-v /srv/data:/var/lib/data) mounts a directory from
the host directly into the container; what the container writes there shows up on
the host filesystem and survives the container. A named volume (-v mydata:/var/lib/data) is a directory Docker manages itself under
/var/lib/docker/volumes/; it is more portable across hosts and easier to back up,
but it is still ultimately a directory on the host.
Kubernetes generalises this with PersistentVolumes and PersistentVolumeClaims, which abstract over the underlying storage, block volumes on a cloud provider, NFS shares, Ceph RBD, local SSDs, and let a pod declare "I need 10 GiB of storage, ReadWriteOnce" without caring how that is provisioned. A storage class and a provisioner satisfy the claim, often dynamically. The discipline that follows in either case is the same: keep state in the volume, keep the container stateless, and you can destroy and recreate containers freely without losing data.