From point A to point B
Point A: you know how to deploy, secure and connect applications, but everything a container writes disappears when the container dies. Point B: by the end of this chapter you'll know how Docker stores data and how Kubernetes gives pods storage that outlives them, whether it's prepared by hand or created on demand.
Storage is part of the Container Orchestration domain (28% of the KCNA exam). We start with Docker, because understanding its layers and volumes is the foundation for everything else.
- Image layers and copy-on-write in Docker
- Volumes, bind mounts and drivers
- CSI, the storage interface
- PersistentVolumes and PersistentVolumeClaims
- StorageClasses and dynamic provisioning
Storage in Docker
Docker keeps everything under /var/lib/docker: images, containers and volumes. To understand why a container's data gets lost, you need to see how an image is built.
An image is a stack of read-only layers. Each Dockerfile instruction (FROM, RUN, COPY…) creates a new layer with whatever changed from the previous one. Layers are reused: if ten images start from the same base, that base is downloaded and stored only once.
When a container starts, Docker adds its own writable layer on top. Everything the container creates or changes goes there. If the container changes a file that comes from the image, Docker first copies it to that layer and changes it there: this is the copy-on-write mechanism. The image never changes, which is why many containers can share it.
Image layers are read-only; each container writes to its own layer. Diagram: The House of Routing.
The consequence is direct: when you delete the container, its writable layer is deleted with it. A database that keeps its files there loses everything.
Volumes, bind mounts and drivers
For data to survive, you store it outside the container's layer. Docker offers two ways:
- Volume: a directory managed by Docker, inside
/var/lib/docker/volumes. You create it withdocker volume createand mount it in the container. - Bind mount: you mount any folder that already exists on the host, for example
/srv/mysql. You decide where it is and who else uses it.
docker info | grep "Storage Driver" # usually overlay2 on Linux
ls /var/lib/docker/ # images, containers, volumes…
docker history nginx:1.27-alpine # the layers of an image
docker volume create data # a volume managed by Docker
docker run -d --mount type=volume,source=data,target=/var/lib/mysql mysql:8.4
docker run -d --mount type=bind,source=/srv/mysql,target=/var/lib/mysql mysql:8.4
Behind this are two kinds of components that are easy to confuse:
- Storage drivers (such as
overlay2): they manage the image layers and copy-on-write. They depend on the operating system, and Docker picks the best one available. - Volume drivers: they decide where volumes live. The built-in one is
local, on the host's disk, but there are plugins to store them on network storage or in the cloud.
CSI: the storage interface
Kubernetes doesn't want to depend on a specific runtime, network or storage system. That's why it talks to all of them through standard interfaces:
- CRI (Container Runtime Interface): for container runtimes, such as containerd or CRI-O.
- CNI (Container Network Interface): for the network plugins you saw in chapter 5.
- CSI (Container Storage Interface): for storage.
Kubernetes talks to runtimes, networks and storage through standard interfaces. Diagram: The House of Routing.
CSI defines a set of common calls, such as creating, deleting, attaching or expanding a volume. Each provider writes its own CSI driver that implements them: AWS EBS, Azure Disk, Google Persistent Disk, Ceph, NetApp, NFS and many more. That way, any storage with a CSI driver works on any orchestrator that speaks CSI, without touching the Kubernetes code.
Volumes in Kubernetes
In Kubernetes, a volume is declared in the pod and mounted in one or more of its containers. There are many types; these are the ones you'll see most:
| Type | What it is | How long it lasts |
|---|---|---|
emptyDir | An empty directory to share files between the pod's containers | As long as the pod |
hostPath | A folder on the node where the pod runs | As long as the node |
configMap / secret | Configuration or secrets mounted as files | As long as the object |
persistentVolumeClaim | Persistent storage, independent of the pod and the node | Until you delete it |
apiVersion: v1
kind: Pod
metadata:
name: random-number
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "shuf -i 0-100 -n 1 >> /opt/number.out; sleep 3600"]
volumeMounts:
- name: data
mountPath: /opt
volumes:
- name: data
hostPath: # a folder of the node: fine for labs, not for production
path: /data
type: DirectoryOrCreate
PersistentVolumes
Declaring storage inside every pod doesn't scale: with hundreds of pods, every team would need to know the details of the NFS server or the cloud disk. Kubernetes splits the two responsibilities.
A PersistentVolume (PV) is a piece of cluster storage that the administrator prepares. It's a cluster-level object, not a namespaced one, and it describes its capacity, its access modes, what to do with the data when it's released and where it physically lives.
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-data
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: manual
nfs:
server: nfs.example.com
path: /exports/data
| Access mode | Short form | Meaning |
|---|---|---|
ReadWriteOnce | RWO | Read and write from a single node |
ReadOnlyMany | ROX | Read-only from many nodes |
ReadWriteMany | RWX | Read and write from many nodes |
ReadWriteOncePod | RWOP | Read and write from a single pod |
The reclaim policy (persistentVolumeReclaimPolicy) says what happens when the PVC that used it is deleted:
Retain: the PV and its data are kept, in the Released state, until an administrator decides what to do.Delete: the PV and the actual storage are deleted. This is the usual setting for dynamically created volumes.
PersistentVolumeClaims
A PersistentVolumeClaim (PVC) is the developer's request: "I need 2Gi with read and write access from several nodes." It lives in a namespace and knows nothing about NFS or disks.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-data
spec:
accessModes:
- ReadWriteMany
storageClassName: manual
resources:
requests:
storage: 2Gi
Kubernetes looks for a PV that meets the request: the same storageClassName, at least the requested size and a compatible access mode. When it finds one, it binds them. The relationship is one-to-one: even if the PV has 5Gi and only 2Gi are requested, nobody else can use the remaining 3Gi. If there's no suitable PV, the PVC stays Pending until one shows up.
The administrator offers PVs, the developer asks with a PVC and Kubernetes binds them. Diagram: The House of Routing.
The pod no longer mentions the actual storage: only the PVC's name.
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: nginx
image: nginx:1.27-alpine
volumeMounts:
- name: content
mountPath: /usr/share/nginx/html
volumes:
- name: content
persistentVolumeClaim:
claimName: pvc-data
kubectl get pv,pvc # STATUS: Available, Bound, Released
kubectl describe pvc pvc-data
kubectl delete pvc pvc-data # stays "Terminating" while a pod uses it
StorageClasses and dynamic provisioning
With static PVs, someone has to create the disk and the PV before every request. In the cloud, that makes no sense. A StorageClass describes a kind of storage, and when a PVC that names it arrives, its CSI driver creates the disk and the PV on the spot. This is dynamic provisioning, the usual way of working today.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast-ssd
provisioner: ebs.csi.aws.com # the CSI driver that creates the disks
parameters:
type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
provisioner: the CSI driver that creates the volumes.parameters: provider options, such as the disk type.reclaimPolicy: the policy for the PVs it creates,Deleteby default.volumeBindingMode: WaitForFirstConsumer: waits until a pod uses the PVC, so it creates the disk in the same zone as that pod's node.allowVolumeExpansion: lets you expand the volume by editing the PVC.
The PVC only has to name the class. If it doesn't name one, the cluster's default class is used.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: db-data
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd # no PV needed: the class creates it
resources:
requests:
storage: 20Gi
The PVC names a StorageClass, and its CSI driver creates the disk and the PV. Diagram: The House of Routing.
kubectl get storageclass # the default class shows "(default)"
kubectl get csidrivers # CSI drivers installed in the cluster
kubectl get pv # PVs created on demand by the class
volumeClaimTemplates to create a separate PVC for each replica, with its own disk.KCNA-style challenge
Answer before you open the solution. Getting it wrong here is the cheapest way to learn.
1. A container changes a file that comes from its image. Where does the change end up?
In the container's writable layer. With copy-on-write, the file is copied there and the image doesn't change.
2. Which standard lets Kubernetes use storage from any provider?
CSI, the Container Storage Interface. CRI is for runtimes and CNI is for networking.
3. A PVC requests 2Gi and the only available PV has 5Gi, with the same class and access mode. What happens?
They bind, and the extra 3Gi go unused: the relationship between a PV and a PVC is one-to-one.
4. You delete the PVC of a PV with the Retain policy. What happens to the data?
It's kept. The PV moves to Released and isn't reused until an administrator frees it by hand.
5. With dynamic provisioning, who creates the PersistentVolume?
The StorageClass, through its CSI driver, when a PVC that names it arrives.
Key takeaways
- An image is a stack of read-only layers; each container adds a writable layer that dies with it.
- Copy-on-write: the container copies what it changes to its own layer, and the image doesn't change.
- Docker stores data outside the container with volumes or bind mounts.
- Kubernetes uses standard interfaces: CRI for runtimes, CNI for networking and CSI for storage.
- emptyDir lasts as long as the pod; hostPath ties the data to one node.
- The administrator prepares the PV, the developer requests the PVC, and they bind one-to-one.
- Access modes: RWO, ROX, RWX and RWOP. Reclaim policies: Retain and Delete.
- A StorageClass creates PVs on demand: that's dynamic provisioning.
This chapter wraps up the Container Orchestration domain. The next chapter begins cloud native architecture: autoscaling, serverless, open standards and community roles.
Official resources
- Docker storage overviewdocs.docker.com
- Storage driversdocs.docker.com
- Volumeskubernetes.io
- Persistent Volumeskubernetes.io
- Storage Classeskubernetes.io
- Dynamic Volume Provisioningkubernetes.io
- Container Storage Interface (CSI) for Kuberneteskubernetes-csi.github.io
- StatefulSetskubernetes.io
- Kubernetes and Cloud Native Associate (KCNA)cncf.io
Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's KCNA course (January to April 2026). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details and YAML examples were checked against the official Docker, Kubernetes and CNCF documentation.