Puppeteer fails to detect Chrome browser in Docker environment

I’m having issues getting Puppeteer to work properly in a Docker setup. Even though I installed Chrome manually based on various guides, I keep getting this error message:

Chrome browser not found (version 133.0.6943.98). This error happens when:
 1. Browser installation wasn't completed before script execution (try `npx puppeteer browsers install chrome`)
 2. Cache directory is misconfigured (current path: /root/.cache/puppeteer)
Check the configuration guide for more details.

Here’s my Docker configuration:

# Phase 1: Set environment variables
#######################################
FROM node:18 AS environment

ENV NODE_ENV=development
ENV NODE_OPTIONS="--max-old-space-size=2048"
ENV PUPPETEER_CACHE_DIR="/root/.cache/puppeteer"
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true"
# Skip automatic Chromium download

# Phase 2: Install dependencies
#######################################
FROM environment AS dependencies

WORKDIR /application

RUN apt-get update && apt-get install -y wget gnupg curl \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list' \
    && apt-get update && apt-get install -y google-chrome-stable \
    --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

# Copy package files for optimal caching
COPY package.json yarn.lock* .yarnrc.yml ./
COPY .yarn/releases ./.yarn/releases

# Install project dependencies
RUN npm install

# Install Chrome for Puppeteer
RUN npx puppeteer browsers install chrome

# Phase 3: Application build
#######################################
FROM environment AS build

WORKDIR /application

# Import dependencies from previous stage
COPY --from=dependencies /application/node_modules ./node_modules
COPY . .

# Compile application
RUN npm run build

EXPOSE 3000
ENV PORT=3000

CMD ["npm", "start"]

When I access the container shell, I can’t find where Chrome got installed. What am I doing wrong here?

looks like ur copying chrome install from dependencies stage but not including it in the final build stage. try adding COPY --from=dependencies /root/.cache/puppeteer ./root/.cache/puppeteer and also copy the chrome binary with COPY --from=dependencies /opt/google/chrome /opt/google/chrome to ur build stage

I encountered a similar setup nightmare a few months back and the root cause was having Chrome installed in one stage but not accessible in the final runtime stage. Your multi-stage build is discarding the Chrome installation when it moves to the build phase. What worked for me was consolidating the Chrome installation into the final stage where your application actually runs. Move both the apt-get Chrome installation commands and the puppeteer browsers install command into your build stage after copying the dependencies. Another approach that saved me debugging time was using a pre-built image like node:18-bullseye with Chrome already configured. The cache directory issue you’re seeing is secondary to Chrome simply not being present in the final container layer.

The issue stems from your multi-stage Docker build where Chrome gets installed in the dependencies stage but doesn’t carry over to the final build stage. I ran into this exact problem last year and found that either you need to install Chrome directly in your build stage or copy the entire Chrome installation between stages. Alternatively, consider switching to a single-stage build for simplicity. Also, remove the PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true" environment variable since you’re trying to use the system Chrome installation. The Chrome binary path might need explicit configuration in your Puppeteer launch options using executablePath: '/usr/bin/google-chrome' to point to the system installation rather than relying on the cache directory.