Difficulty: Advanced
How do you increase the shared memory (/dev/shm) available to a container, and which workloads need it?
Pass --shm-size to docker run, for example docker run --shm-size=1g ..., or set shm_size: "1gb" in Docker Compose. Docker gives every container its own /dev/shm tmpfs of only 64 MB by default, which is too small for headless browsers, PostgreSQL parallel queries and multi-worker PyTorch data loading.
/dev/shm is a memory-backed tmpfs that processes use for POSIX shared memory (shm_open) and fast inter-process communication. Several processes map the same region and read each other's data without copying through pipes or sockets. Because each container has its own IPC namespace, it also gets its own /dev/shm, and Docker caps it at 64 MB unless told otherwise.
| Workload | Symptom | Typical fix |
|---|---|---|
| Chrome or Chromium (Puppeteer, Playwright, Selenium) | Tabs crash, "Aw, Snap!", page.goto hangs on heavy pages | --shm-size=1g or launch Chrome with --disable-dev-shm-usage |
| PostgreSQL parallel queries | "could not resize shared memory segment ... No space left on device" | --shm-size=256m or more, depending on workers and work_mem |
PyTorch DataLoader with num_workers > 0 | "DataLoader worker killed by signal: Bus error" | --shm-size=8g or --ipc=host |
| Oracle, SAP and some JVM IPC setups | Startup failures about shared memory | Vendor recommended size |
Think of a Flipkart-style scraping or PDF-invoice service running Playwright: it works in local tests with small pages and then crashes in production on a heavy product page. Shared memory is often the cause.
--ipc=host shares the host's IPC namespace and its full /dev/shm. It removes the limit but also the isolation, so a container can see other processes' shared memory segments. Acceptable for a dedicated GPU training box, not for multi-tenant hosts.--ipc=shareable / --ipc=container:<name> lets two containers share one IPC namespace intentionally.emptyDir with medium: Memory (and a sizeLimit) at /dev/shm./dev/shm is generally charged to the container's memory cgroup, so a large shm combined with a tight --memory limit can end in an OOM kill.shm-size with docker update; recreate the container.--shm-size accepts b, k, m, g. Compose accepts strings like "2gb".docker exec <id> df -h /dev/shm shows the current size and usage.# Default size
docker run --rm alpine df -h /dev/shm
# shm 64.0M 0 64.0M 0% /dev/shm
# Headless Chrome / Playwright worker with 1 GB of shm
docker run -d --name pdf-renderer --shm-size=1g --memory=2g myorg/pdf-renderer:1.3
# PostgreSQL with room for parallel query workers
docker run -d --name pg --shm-size=512m -e POSTGRES_PASSWORD=dev postgres:16
docker exec pg df -h /dev/shm
The memory limit is set higher than shm-size because shared memory pages count as real memory usage.
# compose.yaml
services:
trainer:
image: pytorch/pytorch:latest
shm_size: "8gb"
---
# Kubernetes Pod snippet: no shm-size field, mount a memory emptyDir instead
spec:
containers:
- name: renderer
image: myorg/pdf-renderer:1.3
volumeMounts:
- name: dshm
mountPath: /dev/shm
volumes:
- name: dshm
emptyDir:
medium: Memory
sizeLimit: 1Gi
Compose maps shm_size straight to --shm-size. In Kubernetes the memory-backed emptyDir replaces the default /dev/shm and counts toward the pod's memory.
/dev/shm, --shm-size, Shared memory (tmpfs), Headless Chrome, PostgreSQL parallel query, PyTorch DataLoader