User Agents

Web browsers and everyday web tech, explained

Fix Puppeteer Could Not Find Chrome in Docker

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 puppeteer or puppeteer-core package.
  • A Dockerfile configured 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

  1. 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:latest or node:slim), add the following lines to your Dockerfile:
    RUN apt-get update && apt-get install -y chromium-browser --no-install-recommends && rm -rf /var/lib/apt/lists/*
    If you are using Alpine Linux, use:
    RUN apk add --no-cache chromium
  2. Install System Dependencies: Chromium requires several shared libraries (like libX11, libXcomposite, and libnss3) that are not present in slim images. If the browser fails to launch after step one, ensure these dependencies are installed. In Debian/Ubuntu, installing chromium usually pulls these in, but for some custom builds, you may need to manually add libxss1 and libasound2.
  3. 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
  4. 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:
    const browser = await puppeteer.launch({
          executablePath: '/usr/bin/chromium-browser', // Path for Alpine/Debian
          args: ['--no-sandbox', '--disable-setuid-sandbox']
        });
    Note: On some Debian systems, the path might be /usr/bin/chromium. You can verify the path by running which chromium-browser inside your container.
  5. Apply the --no-sandbox flag: Chrome's sandbox requires specific kernel privileges that Docker containers typically do not have. Without the --no-sandbox argument, the browser will crash on startup with a "sandbox" error. This must be included in the args array of the launch options.
  6. Rebuild and Run: Build your image again to apply the changes:
    docker build -t puppeteer-app .
    Run the container and verify that the browser initializes correctly.

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-chrome when the package installed was chromium-browser. Always verify the binary location using which.
  • Ignoring the cache: Docker may use a cached layer from a previous build where Chromium wasn't installed. Use --no-cache if you suspect the installation step was skipped.
  • Missing dependencies in slim images: Using node:slim without installing the necessary apt` packages leads to "shared library not found" errors.

Tips

  • Use puppeteer-core: If you are manually providing the browser binary, switch from puppeteer to puppeteer-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 tini as 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 .so file error (e.g., libnss3.so missing), 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/shm size (64MB), which can cause Chrome to crash on heavy pages. Run your container with --shm-size=1gb to 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.