Using an emptyDir

In Kubernetes, an emptyDir is a simple ephemeral volume type that provides temporary storage to a pod during its lifetime. It is created when a pod is assigned to a node, and deleted when the pod is removed from that node.

Example emptyDir

This Pod creates a temporary volume mounted at /data, which is shared with the container.

apiVersion: v1
kind: Pod
metadata:
  name: emptydir-demo
spec:
  containers:
    - name: app
      image: busybox
      command: ["sh", "-c", "echo hello > /data/hello.txt && sleep 3600"]
      volumeMounts:
        - mountPath: /data
          name: cache-volume
  volumes:
    - name: cache-volume
      emptyDir: {}

Optional Medium Setting

You can choose where the data is stored:

emptyDir:
  medium: "Memory"
MediumDescription
(default)Uses node's disk, SSD or network storage, depending on your environment
MemoryUses RAM (tmpfs) for faster access (but volatile)

Key Characteristics

FeatureDescription
Starts emptyNo data when created
Shared across containersSame volume can be used by multiple containers in the pod
Deleted with podVolume is destroyed when the pod is removed
Node-localVolume is stored on the node's local disk or memory
FastIdeal for performance-sensitive scratch space

Common Use Cases

  • Caching intermediate build artifacts

  • Buffering logs

  • Temporary work directories

  • Sharing data between containers in the same pod (like sidecars)