如何为Puppeteer正确配置--shm-size参数?
Hey there, let's work through your Puppeteer + Docker shared memory issue together!
First off, a key clarification: --shm-size is a Docker container runtime parameter, not a Puppeteer or Chrome launch flag. That’s why adding it to your Puppeteer args array won’t work—you need to set it when starting your Docker container instead.
Here’s how to properly configure it in different scenarios:
1. Using docker run to launch your container
Simply add the --shm-size flag directly to your docker run command, like this:
docker run --shm-size=1gb your-puppeteer-image-name
This will set the container’s /dev/shm shared memory space to 1GB, which should be more than enough to prevent Chrome crashes when rendering large pages.
2. Using Docker Compose for container management
If you’re using Docker Compose to orchestrate your setup, add the shm_size field to your service definition in docker-compose.yml:
services: puppeteer-service: image: your-puppeteer-image-name shm_size: '1gb' # Add any other service configs here (ports, volumes, etc.)
A quick note on --disable-dev-shm-usage
You mentioned trying this Chrome flag, which is supposed to bypass /dev/shm entirely by using temporary files instead (for Chrome 65+). If this didn’t work for you, a few things to check:
- Verify the Chrome version bundled with your Puppeteer installation (older Puppeteer versions for Node 8.9.3 might ship with a Chrome version older than 65). You can run
google-chrome --versioninside your container to confirm. - Ensure your container has enough disk space and proper file permissions for the temporary files Chrome would create as an alternative to
/dev/shm. - Double-check that the flag is actually being passed to Chrome—you can log the
argsarray in your code to confirm it’s included correctly.
Your existing Puppeteer launch code looks correct for passing the flag:
const args = [`--app=${url}`, `--window-size=${WIDTH},${HEIGHT}`, '--disable-dev-shm-usage']; const browser = await puppeteer.launch({ headless, args });
But if your Chrome version doesn’t support the flag or your container environment has restrictions, adjusting Docker’s shared memory size via --shm-size is the more reliable fix.
内容的提问来源于stack exchange,提问作者florin




