⌂ Home

Sharing Data Between Containers

Use shared Pod volumes so multiple containers in the same Pod can exchange files without network calls.

Containers in the same Pod are meant for close cooperation. Shared volumes make that cooperation concrete by giving them one common filesystem path.

Core Concepts

Repository YAML Files:
  • k8s/labs/storage/data-sharing.yaml — Multi-container Pod sharing data via emptyDir

Shared Pod storage

A volume such as emptyDir can be mounted into multiple containers so they see the same files.

Fast local exchange

File sharing inside one Pod avoids the overhead of exposing a separate service between helper containers.

Ephemeral by default

With emptyDir, data exists for the Pod lifecycle, which is often exactly what helper workflows need.

In-Pod Data Sharing Flow

1

Create a shared volume

The Pod defines an emptyDir or another shared volume resource.

2

Mount it into both containers

Each container mounts the same volume path into its own filesystem.

3

One container writes data

A helper or generator container creates files in the shared path.

4

Another container reads or serves it

The second container consumes the same files without any extra networking layer.

Key point: This is the cleanest pattern when collaboration is local to one Pod and the files do not need to outlive that Pod.

Representative YAML

Shared emptyDir Pod

apiVersion: v1
kind: Pod
metadata:
  name: data-sharing-pod
spec:
  containers:
  - name: app-container-1
    image: nginx
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: shared-data
  - name: app-container-2
    image: busybox
    command: ["/bin/sh", "-c", "echo Shared data from app-container-2 > /usr/share/nginx/html/index.html && sleep 3600"]
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: shared-data
  volumes:
  - name: shared-data
    emptyDir: {}

In-Pod Sharing Options

PatternPersistenceUse Case
emptyDirPod lifecycle onlyTemporary sharing, caches, generated content
ConfigMap volumeExternally defined config dataShared configuration files
PVCPersistent beyond Pod deletionShared state that must survive recreation
Design hint: Choose emptyDir when you want collaboration, not durability.

How To Use It In Practice

Log processing sidecars

One container writes logs while another ships or analyzes them.

Generated web content

Create content in one container and serve it from another container in the same Pod.

Temporary build pipelines

Pass artifacts through shared local files without adding extra services or persistence.