An `emptyDir` volume is a directory created just before a pod’s containers start. It begins empty and keeps its files for the lifetime of that pod. Use it when a container needs temporary writable space, when files must survive a container restart, or when containers in the same pod need to share files. section 9.2 section 9.7
Define the volume under `spec.volumes`, then mount it in each container that needs access. The mount names the volume and sets a `mountPath` inside that container. Defining the volume alone does not make its files available to a container. section 9.2.1
Listing 9.2 The quiz pod with an emptyDir volume for the mongo container
apiVersion: v1
kind: Pod
metadata:
name: quiz
spec:
volumes: #1
- name: quiz-data #1
emptyDir: {} #1
containers:
- name: quiz-api
image: luksa/quiz-api:0.1
ports:
- name: http
containerPort: 8080
- name: mongo
image: mongo
volumeMounts: #2
- name: quiz-data #2
mountPath: /data/db #2
The pod manifest defines `quiz-data` as an `emptyDir` and mounts it at `/data/db`, where MongoDB stores its files. section 9.2.1
Keep database files through a container restart
With MongoDB writing to `/data/db` on the volume, its files remain when the MongoDB container restarts. A check using `kubectl exec` and `mongosh` counts one question before the database server is shut down; after the pod reports one container restart, the same check still counts one. This does not make the data permanent: the directory is deleted when the pod terminates. section 9.2.1 section 9.7
Choose the volume’s behavior to match the task:
- For local storage, leave `medium` unset to use the host node’s default medium, or set `medium: Memory` to keep files in a memory-backed filesystem. `sizeLimit` sets a size limit, such as `10Mi`. section 9.2.1
- To supply files before regular containers start, mount the same `emptyDir` in an init container. It can copy a file into the volume; a regular container can then mount that volume at a different path, optionally read-only. section 9.2.2
Listing 9.5 Using an init container to initialize an emptyDir volume
apiVersion: v1
kind: Pod
metadata:
name: quiz
spec:
volumes:
- name: initdb #1
emptyDir: {} #1
- name: quiz-data
emptyDir: {}
initContainers:
- name: installer #2
image: luksa/quiz-initdb-script-installer:0.1 #2
volumeMounts: #2
- name: initdb #2
mountPath: /initdb.d #2
containers: #2
- name: quiz-api
image: luksa/quiz-api:0.1
ports:
- name: http
containerPort: 8080
- name: mongo
image: mongo
volumeMounts:
- name: quiz-data
mountPath: /data/db
- name: initdb #3
mountPath: /docker-entrypoint-initdb.d/ #3
readOnly: true #3
This manifest uses one `emptyDir` for database files and another for a startup script copied by an init container and read by MongoDB. section 9.2.2
The boundary is the pod, not the individual container: multiple containers in that pod can mount the volume, but another pod cannot share its `emptyDir`. For storage that must persist beyond a pod’s lifetime, use a different volume arrangement, such as one backed by a PersistentVolumeClaim. section 9.2 section 9.1.2


