Fix Puppeteer Could Not Find Chrome in Docker
In this article
To fix the "could not find Chrome" error in Docker, you must explicitly install the Chromium browser and its required system dependencies within your Dockerfile, then point Puppeteer to the installed binary using the executablePath option. By default, Puppeteer attempts to download a version of Chromium during npm install, but these binaries often lack the necessary shared libraries to run in a stripped-down Linux container environment, or they are deleted during multi-stage build processes.
What You'll Need
- A project using Node.js and the
puppeteerorpuppeteer-corepackage. - A
Dockerfileconfigured for your application. - Root or sudo access to the environment where the Docker image is being built.
- Basic knowledge of the command line and Docker build processes.
Step-by-Step
-
Modify the Dockerfile to install Chromium:
Depending on your base image, you need to use the package manager to install Chromium. For Debian-based images (like
node:latestornode:slim), add the following lines to your Dockerfile:
If you are using Alpine Linux, use:RUN apt-get update && apt-get install -y chromium-browser --no-install-recommends && rm -rf /var/lib/apt/lists/*RUN apk add --no-cache chromium -
Install System Dependencies:
Chromium requires several shared libraries (like
libX11,libXcomposite, andlibnss3) that are not present in slim images. If the browser fails to launch after step one, ensure these dependencies are installed. In Debian/Ubuntu, installingchromiumusually pulls these in, but for some custom builds, you may need to manually addlibxss1andlibasound2. -
Disable Puppeteer's Automatic Download:
To keep your image size small and avoid conflicts, tell Puppeteer not to download its own bundled version of Chromium. Add this environment variable to your Dockerfile:
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true -
Configure the executablePath in your code:
Since you are using the system-installed Chromium rather than the bundled one, you must tell Puppeteer exactly where the binary is located. Update your
puppeteer.launch()configuration:
Note: On some Debian systems, the path might beconst browser = await puppeteer.launch({ executablePath: '/usr/bin/chromium-browser', // Path for Alpine/Debian args: ['--no-sandbox', '--disable-setuid-sandbox'] });/usr/bin/chromium. You can verify the path by runningwhich chromium-browserinside your container. -
Apply the --no-sandbox flag:
Chrome's sandbox requires specific kernel privileges that Docker containers typically do not have. Without the
--no-sandboxargument, the browser will crash on startup with a "sandbox" error. This must be included in theargsarray of the launch options. -
Rebuild and Run:
Build your image again to apply the changes:
Run the container and verify that the browser initializes correctly.docker build -t puppeteer-app .
Common Mistakes
- Forgetting --no-sandbox: This is the most common cause of failure. Even if Chrome is found, it will refuse to start in a container without this flag.
- Using the wrong path: Using
/usr/bin/google-chromewhen the package installed waschromium-browser. Always verify the binary location usingwhich. - Ignoring the cache: Docker may use a cached layer from a previous build where Chromium wasn't installed. Use
--no-cacheif you suspect the installation step was skipped. - Missing dependencies in slim images: Using
node:slimwithout installing the necessaryapt` packages leads to "shared library not found" errors.
Tips
- Use puppeteer-core: If you are manually providing the browser binary, switch from
puppeteertopuppeteer-core. This reduces the package size because it doesn't include the logic to download Chromium. - Multi-stage builds: Install the dependencies in a build stage, but ensure the final production image still contains the installed Chromium binary and its required system libraries.
- Zombie Process Management: Use a tool like
tinias your entrypoint in Docker. Chrome often leaves "zombie" processes that can eat up memory and eventually crash the container.
Troubleshooting
If you still encounter errors, follow these steps to isolate the problem:
- Check the binary path: Run
docker run -it your-image-name which chromium-browser. If it returns nothing, the installation in the Dockerfile failed or the package name is different. - Test launch manually: Enter the container shell and try running
chromium-browser --version. If it fails with a.sofile error (e.g.,libnss3.somissing), you are missing a system dependency. - Inspect logs: Look for "Protocol error" or "Target closed" messages. These usually indicate that the browser crashed immediately after starting, often due to memory limits or missing flags.
- Increase Shared Memory: Docker containers have a small default
/dev/shmsize (64MB), which can cause Chrome to crash on heavy pages. Run your container with--shm-size=1gbto provide more memory.
Frequently Asked Questions
Why can't Puppeteer just use the browser it downloads automatically?
The bundled Chromium is a generic build. Docker containers (especially slim or Alpine versions) lack the specific Linux libraries (like X11 or Gtk) that the bundled browser expects to find on a full desktop OS.
Is --no-sandbox safe to use?
It disables a layer of security. It is generally acceptable in a controlled Docker environment where you only visit trusted sites, but you should be cautious if you are navigating to untrusted user-provided URLs.
What is the difference between chromium and chromium-browser?
They are essentially the same open-source project, but the package name varies by Linux distribution. Ubuntu/Debian often use chromium-browser, while others use chromium.
How do I reduce the size of my Docker image?
Use puppeteer-core instead of puppeteer, use a slim base image, and combine your apt-get update and install commands into a single RUN instruction to reduce image layers.
Can I use a remote Chrome instance instead?
Yes. Instead of installing Chrome in the container, you can use puppeteer.connect({ browserWSEndpoint: '...' }) to connect to a separate Chrome container or a service like Browserless.io.