Kubernetes emptyDir volumes: Use cases and examples

Learn how Kubernetes emptyDir volumes work with practical examples. Covers disk-backed and memory-backed options, shared storage between containers, and typical use cases.

5 lessons · 16 min · Intermediate

3 minutes reading time

Written by

Civo Team
Civo Team

Marketing Team at Civo

Before diving into emptyDir specifically, here is a quick overview of where it fits in the Kubernetes volume landscape:

Volume typePersists beyond pod deletionPortable across nodesBest for

emptyDir

No

Yes

Temporary scratch space, sharing data between containers in a pod

hostPath

Yes (on that node only)

No

DaemonSets, single-node dev clusters

PVC (static or dynamic)

Yes

Depends on backend

Persistent application data

Remote storage (NFS, cloud)

Yes

Yes

Shared storage accessible from multiple nodes

What is emptyDir?

An emptyDir volume is created fresh when a pod is assigned to a node. It exists for the entire lifetime of that pod and is deleted when the pod is deleted.

The key distinction is that emptyDir is pod-scoped, not container-scoped. If a container inside the pod crashes and restarts, the emptyDir volume and all its data are still there when the container comes back up. The data is only lost when the entire pod is deleted. This makes emptyDir useful for data that needs to survive container restarts but does not need to outlive the pod.

By default, emptyDir uses the node's disk. You can optionally back it with node RAM for faster access.

emptydir-lifecycle

Common use cases

  • Shared scratch space between containers in the same pod: A main application container writes files to an emptyDir and a sidecar container reads from the same directory. This is a common pattern for log shipping: the application writes logs to /var/log/app and a Fluentd or Filebeat sidecar reads from the same path and forwards them to a logging backend.
  • Caching downloaded assets: A pod that processes files can download them into an emptyDir in an init container and then process them in the main container, avoiding repeated downloads across processing steps.
  • Temporary file processing in a batch pipeline: Intermediate files generated during a multi-stage processing job can live in an emptyDir between stages. When the pod completes, the files are cleaned up automatically.
  • Checkpointing long computations: A computation that runs in stages can write checkpoints to an emptyDir so that if a container crashes mid-computation, it can resume from the last checkpoint rather than starting over.

Basic emptyDir example

apiVersion: v1
kind: Pod
metadata:
name: emptydir-pod
spec:
containers:
- name: busybox
image: busybox
command: ["sh", "-c", "while true; do sleep 3600; done"]
volumeMounts:
- mountPath: /demo
name: demo-volume
volumes:
- name: demo-volume
emptyDir: {}
kubectl create -f pod.yaml
kubectl get pods

Exec into the pod and create a file:

kubectl exec -it emptydir-pod -- sh
cd /demo
touch test
ls

Expected output:

test

Exit the pod and verify the file exists from outside:

kubectl exec -it emptydir-pod -- ls /demo

Expected output:

test

Delete the pod:

kubectl delete pod emptydir-pod --force --grace-period=0

The emptyDir and all its contents are deleted with the pod. There is nothing to clean up manually.

Sharing an emptyDir between two containers

This example shows a sidecar pattern. The main container writes a timestamp to a shared log directory every five seconds. The sidecar container reads from the same directory.

apiVersion: v1
kind: Pod
metadata:
name: shared-volume-pod
spec:
containers:
- name: writer
image: busybox
command: ["sh", "-c", "while true; do date >> /shared/log.txt; sleep 5; done"]
volumeMounts:
- mountPath: /shared
name: shared-data
- name: reader
image: busybox
command: ["sh", "-c", "while true; do cat /shared/log.txt; sleep 10; done"]
volumeMounts:
- mountPath: /shared
name: shared-data
volumes:
- name: shared-data
emptyDir: {}
kubectl create -f shared-volume-pod.yaml

Check the reader container's logs to see the timestamps being written by the writer container:

kubectl logs shared-volume-pod -c reader

Expected output:

Thu Jan 15 10:23:01 UTC 2026
Thu Jan 15 10:23:06 UTC 2026
Thu Jan 15 10:23:11 UTC 2026

Both containers share the same /shared directory via the emptyDir volume. Data written by one is immediately visible to the other.

Memory-backed emptyDir

Set medium: Memory to back the emptyDir with node RAM instead of disk. This gives you tmpfs storage, which is significantly faster than disk for workloads that need very fast temporary storage.

apiVersion: v1
kind: Pod
metadata:
name: memory-volume-pod
spec:
containers:
- name: busybox
image: busybox
command: ["sh", "-c", "while true; do sleep 3600; done"]
resources:
limits:
memory: 256Mi
volumeMounts:
- mountPath: /cache
name: cache-volume
volumes:
- name: cache-volume
emptyDir:
medium: Memory
sizeLimit: 128Mi

Two important things to note with memory-backed emptyDir:

The volume size counts against the container's memory limit. In the example above, the container has a 256Mi memory limit. If the emptyDir uses 128Mi of that, only 128Mi remains for the application itself. Size it carefully.

Use sizeLimit to cap how much RAM the emptyDir can consume. Without it, the volume can grow until it hits the container's memory limit, which will trigger an OOMKilled event.

kubectl create -f memory-volume-pod.yaml
kubectl exec -it memory-volume-pod -- df -h /cache

Expected output:

Filesystem Size Used Avail Use% Mounted on
tmpfs 128M 0 128M 0% /cache
Civo Team
Civo Team

Marketing Team at Civo

Civo is the Sovereign Cloud and AI platform designed to help developers and enterprises build without limits. We bridge the gap between the openness of the public cloud and the rigorous security of private environments, delivering full cloud parity across every deployment. As a team, we are dedicated to providing scalable compute, lightning-fast Kubernetes, and managed services that are ready in minutes. Through CivoStack Enterprise and our FlexCore appliance, we empower organizations to maintain total data sovereignty on their own hardware.

Our mission is to make the cloud faster, simpler, and fairer. By providing enterprise-grade NVIDIA GPUs and streamlined model management, we ensure that high-performance AI and machine learning are accessible to everyone. Built for transparency and performance, the Civo Team is here to give you total control over your infrastructure, your data, and your spend.

View author profile