Headless Chrome crashes in Docker with Target closed: the 64 MB /dev/shm limit
PDF and screenshot jobs passed on laptops but crashed in containers with Target closed on long pages. Docker gives every container a 64 MB /dev/shm, Chrome uses it for inter-process shared memory, and big pages run out of it.
Our invoice service renders HTML to PDF with Puppeteer. After moving it from a VM into a Docker container, roughly 4% of jobs failed. Short invoices always worked. Annual statements with 40+ pages of tables failed most of the time, and the failure was not a timeout but an abrupt crash:
Error: Protocol error (Page.printToPDF): Target closed
On a screenshot endpoint in the same container the message was slightly different, but it was the same event: the renderer process for the tab disappeared in the middle of a protocol call.
Error: Protocol error (Page.captureScreenshot): Target closed
Reproducing it
We created a test page with 3,000 table rows and a large background image and ran it with the same container image on a laptop:
docker run --rm -v "$PWD:/work" invoice-renderer:2026.12 node /work/repro.js
It crashed on every run. Running the same script directly on the host, outside Docker, never crashed. That ruled out the page itself and pointed at the container environment.
What we ruled out
Container memory limit. The container had a 2 GiB limit.
docker statsshowed peak usage around 900 MiB, and the container was not restarted, so the kernel OOM killer was not involved.docker inspectshowed"OOMKilled": false.Puppeteer or Chrome version. We pinned the previous Puppeteer release and its bundled Chrome for Testing build. Same crash.
Sandbox flags. We already ran as a non-root user with the sandbox enabled. Removing the sandbox (which we would not ship anyway) did not change anything.
Root cause
We enabled Chrome's logging with dumpio: true and saw shared memory errors right before each crash. Then we checked the container:
docker exec renderer df -h /dev/shm
Filesystem Size Used Avail Use% Mounted on
shm 64M 64M 0 100% /dev/shm
Docker mounts a private tmpfs at /dev/shm in each container, and its default size is 64 MB. Chrome's multi-process architecture passes rendered tiles, PDF data and IPC buffers between processes through shared memory, which on Linux is backed by /dev/shm. Large pages need more than 64 MB at once; when an allocation fails, the renderer crashes and Puppeteer reports the target as closed. On the VM, /dev/shm was sized at half of RAM by default, so the problem never showed up.
What is happening under the hood
Chrome runs the browser process, GPU process and one or more renderer processes separately. To avoid copying large buffers through pipes, they map shared memory regions. On Linux these regions are created as files in /dev/shm, a tmpfs that lives in RAM.
Inside a container, /dev/shm is not the host's tmpfs. The container runtime creates a new one with a fixed size, and Docker's default for --shm-size is 64 MB. That limit is separate from the container's memory limit, so the container can have plenty of free memory while /dev/shm is full. Memory used in /dev/shm still counts toward the container's cgroup memory, but the tmpfs size cap is hit first.
The Puppeteer troubleshooting guide documents exactly this: the default 64 MB is often too small for Chrome and makes it crash when rendering large pages.
Environment
Component | Version / value |
|---|---|
Host | Ubuntu 24.04 LTS, Docker Engine 28 |
Base image |
|
Node.js | 22 LTS |
Puppeteer | 24.x with bundled Chrome for Testing |
Container limits | 2 vCPU, 2 GiB memory, default |
Workload | HTML to PDF, up to 60 pages of tables |
Minimal reproduction
// repro.js
import puppeteer from 'puppeteer';
const rows = Array.from({ length: 3000 }, (_, i) =>
`<tr><td>${i}</td><td>${'x'.repeat(80)}</td><td>${(i * 1.17).toFixed(2)}</td></tr>`).join('');
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(`<table>${rows}</table>`, { waitUntil: 'load' });
await page.pdf({ path: '/tmp/out.pdf', format: 'A4' }); // crashes with 64 MB /dev/shm
await browser.close();
Who is affected
Any Chrome-based automation (Puppeteer, Playwright, Selenium with ChromeDriver, PDF services built on them) running in Docker, containerd or Kubernetes with the default shared memory size. The heavier the page (large screenshots, long PDFs, big canvases, video), the sooner it fails. Firefox-based automation uses shared memory too, but our crashes were all in Chromium.
There are three fixes. We applied the first one and kept the third as a documented fallback.
Option 1: give the container a larger /dev/shm
For plain Docker:
docker run --shm-size=1g invoice-renderer:2026.12
For Docker Compose:
services:
renderer:
image: invoice-renderer:2026.12
shm_size: "1gb"
mem_limit: 2g
Kubernetes has no shm_size field. Mount a memory-backed emptyDir over /dev/shm instead:
spec:
containers:
- name: renderer
image: registry.example.com/invoice-renderer:2026.12
resources:
limits:
memory: 2Gi
volumeMounts:
- name: dshm
mountPath: /dev/shm
volumes:
- name: dshm
emptyDir:
medium: Memory
sizeLimit: 1Gi
Files written to a memory-backed emptyDir count against the container's memory limit, so the limit must leave room for the Chrome processes plus what lands in /dev/shm. We kept 2 GiB and saw peak usage at about 1.3 GiB on the largest statements.
Option 2: share the host IPC namespace
Playwright's Docker guide recommends --ipc=host when running Chromium, which makes the container use the host's /dev/shm. It works, but it removes IPC isolation between the container and the host, so we did not use it for a service that renders customer-supplied HTML.
docker run --ipc=host invoice-renderer:2026.12
Option 3: --disable-dev-shm-usage
This Chrome flag makes Chrome create its shared memory files in /tmp instead of /dev/shm.
const browser = await puppeteer.launch({
args: ['--disable-dev-shm-usage'],
});
Trade-offs:
If
/tmpis on the container's overlay filesystem, shared memory is now disk-backed. Rendering large pages became noticeably slower in our tests, and it adds write load to the node's disk.If you mount
/tmpas tmpfs, you are back to a size-limited RAM filesystem and need to size it anyway.It is the only option when you cannot change the runtime settings, such as some managed CI runners or serverless platforms.
Some automation libraries pass this flag by default. Check what your browser was actually started with instead of assuming:
console.log(browser.process()?.spawnargs.join(' '));
How to test the fix
Rerun the reproduction with the new setting and watch /dev/shm while it runs:
docker run --rm --shm-size=1g -v "$PWD:/work" invoice-renderer:2026.12 node /work/repro.js
docker exec renderer sh -c 'while true; do df -h /dev/shm | tail -1; sleep 0.5; done'
We ran the 50 largest real statements from staging in a loop 20 times: 1,000 renders, zero crashes, peak /dev/shm usage 310 MB.
Confirming in production
The job failure rate for
Target closeddropped from about 4% to 0 over the following week.We added a startup check that logs the size of
/dev/shmand refuses to start the worker if it is under 512 MB:
import { statfsSync } from 'node:fs';
const s = statfsSync('/dev/shm');
const sizeMb = (s.blocks * s.bsize) / 1024 / 1024;
if (sizeMb < 512) {
console.error(`/dev/shm is ${sizeMb.toFixed(0)} MB, need >= 512 MB`);
process.exit(1);
}
A deploy that forgets the volume now fails readiness immediately instead of crashing on the first large invoice.
Regression guard
The reproduction page is part of the CI suite and runs inside the production image with the production Compose file, so a missing
shm_sizefails the build.Alert on the error string
Target closedin job logs, grouped by deployment, with a threshold of 5 per hour.
Security implications
Raising --shm-size or using a memory emptyDir does not change isolation; it only resizes a private tmpfs. --ipc=host does weaken isolation, because processes in the container share IPC objects with the host, which is why we avoided it for untrusted HTML. Never "fix" Chrome crashes by adding --no-sandbox unless the container itself is the sandbox and you understand what you give up; it was unrelated to this bug.
Rendering customer HTML is also an SSRF and data exposure risk on its own. Block requests to internal networks from the renderer, whatever shared memory option you choose.
Prevention checklist
Set
shm_size(Compose) or a memoryemptyDiron/dev/shm(Kubernetes) for every Chrome workload.Size the container memory limit to include
/dev/shmusage.Log
/dev/shmsize at startup and fail fast if it is too small.Keep a large-page reproduction in CI using the real container configuration.
Record the actual Chrome launch arguments in logs.
References
Puppeteer troubleshooting, running in Docker: https://pptr.dev/troubleshooting
Playwright Docker guide: https://playwright.dev/docs/docker
Docker run reference,
--shm-sizeand--ipc: https://docs.docker.com/reference/cli/docker/container/run/Docker Compose file reference,
shm_size: https://docs.docker.com/reference/compose-file/services/Kubernetes volumes,
emptyDirwithmedium: Memory: https://kubernetes.io/docs/concepts/storage/volumes/#emptydir
Add a label
Embed this post
<iframe src="https://hibug.me/headless-chrome-docker-crash-dev-shm" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I have been writing PHP since before Composer existed and Laravel since its early versions. Today I build modular Laravel applications for payment and subscription businesses and maintain a few open-source packages in my spare time. I write about the practical side of that work: structuring large Laravel codebases, publishing and versioning packages, two-factor authentication that actually works for every user, and what really happens during an on-call night on a payments platform. Each post comes from something I shipped or fixed, with code you can adapt.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.