User Agents

Web browsers and everyday web tech, explained

Chrome Headless on Google Cloud Run: Setup Guide

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Install dependencies for running Chrome
  4. Add Chrome as a source
  5. Install Chrome
  6. Set working directory
  7. Copy application files
  8. Install Node.js dependencies
  9. Allow use of non-root user
  10. Expose port
  11. Start the server
  12. Common Mistakes
  13. Tips
  14. Troubleshooting
  15. Frequently Asked Questions

Running Chrome headless on Google Cloud Run allows you to automate browser-based tasks—like generating screenshots, scraping dynamic content, or running end-to-end tests—without managing servers. Cloud Run is a fully managed compute platform that automatically scales and runs containers, making it ideal for short-lived, event-driven workloads. By packaging a headless Chrome instance in a container and deploying it to Cloud Run, you can execute browser automation securely and efficiently at scale. This guide walks you through the setup, including containerization, configuration, and deployment.

What You'll Need

  • Google Cloud account with billing enabled
  • Google Cloud SDK (gcloud CLI) installed and configured
  • Docker installed locally for building the container
  • Basic familiarity with containers and shell commands
  • A project in Google Cloud with Cloud Run and Cloud Build APIs enabled

Step-by-Step

  1. Create a new directory on your local machine for the project. Navigate into it using the terminal.
  2. Create a main.js file (or equivalent for your preferred language) to run Puppeteer or another headless browser library. For Node.js, install Puppeteer with npm init -y followed by npm install puppeteer. Write a script that launches Chrome, navigates to a page, and performs an action like taking a screenshot.
  3. In the same directory, create a Dockerfile with the following content:
    FROM node:18-slim
    

    Install dependencies for running Chrome

    RUN apt-get update && apt-get install -y \n wget \n gnupg \n ca-certificates \n unzip \n && rm -rf /var/lib/apt/lists/*

    Add Chrome as a source

    RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-chrome.gpg RUN echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list

    Install Chrome

    RUN apt-get update && apt-get install -y google-chrome-stable

    Set working directory

    WORKDIR /app

    Copy application files

    COPY package*.json ./ COPY main.js ./

    Install Node.js dependencies

    RUN npm install

    Allow use of non-root user

    USER node

    Expose port

    EXPOSE 8080

    Start the server

    CMD ["node", "main.js"]
  4. Update your main.js to launch Chrome with flags suitable for headless and sandboxed environments. Use:
    const browser = await puppeteer.launch({
      args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--headless',
        '--disable-dev-shm-usage',
        '--disable-gpu',
        '--no-first-run',
        '--no-zygote',
        '--single-process'
      ],
      executablePath: '/usr/bin/google-chrome-stable'
    });
  5. Ensure your script starts an HTTP server to accept requests. For example, use Express to listen on process.env.PORT || 8080, triggering the browser task when an endpoint is hit.
  6. Build the container using Docker: docker build -t chrome-headless .. Test it locally with docker run -p 8080:8080 chrome-headless and access localhost:8080 to verify it works.
  7. Push the container to Google Container Registry or Artifact Registry. First, tag it: docker tag chrome-headless gcr.io/your-project-id/chrome-headless. Then push: docker push gcr.io/your-project-id/chrome-headless.
  8. In the Google Cloud Console, go to Cloud Run and click "Create Service". Select the uploaded container image, set the platform to "Managed", and choose a region.
  9. Set the container port to 8080 and allow unauthenticated requests only if public access is required. Adjust CPU, memory (at least 2 GB recommended), and timeout settings based on task complexity.
  10. Deploy the service. Once deployed, Google provides a public URL. Test it by sending an HTTP request to trigger your browser automation.

Common Mistakes

  • Forgetting to install Chrome inside the container—Puppeteer doesn’t include Chrome by default in slim images.
  • Omitting --no-sandbox and --disable-setuid-sandbox flags, which are required in containerized environments.
  • Using headless: true instead of '--headless' in the args array—some versions require the flag as a string.
  • Running as root without dropping privileges—Cloud Run runs as non-root by default, so set USER node to match.
  • Underestimating memory—headless Chrome can use over 1 GB; set Cloud Run memory to 2 GB to avoid crashes.

Tips

  • Use --single-process to reduce memory overhead in constrained environments.
  • Set --disable-dev-shm-usage to avoid tmpfs issues in containers with limited shared memory.
  • Log Chrome output during development by adding --verbose and capturing stderr.
  • Keep container size small by cleaning apt cache after installation.
  • Use environment variables to control behavior, such as target URLs or output formats.

Troubleshooting

If the service fails to start, check Cloud Run logs in the Google Cloud Console. Common issues include Chrome failing to launch due to missing flags or dependencies. If you see "No usable sandbox", verify --no-sandbox is included. If the container crashes on startup, ensure Chrome is correctly installed and the executable path is set. Memory exhaustion often appears as silent crashes—increase allocated memory in Cloud Run settings. If requests time out, extend the timeout limit (up to 60 minutes for Cloud Run). If Puppeteer fails to connect, confirm Chrome is launched before accepting requests. For permission errors, ensure non-root user has access to necessary directories. If the image fails to build, verify Dockerfile syntax and network access during build.

Frequently Asked Questions

Can I run Chrome headless on Cloud Run for free?

Yes, Cloud Run has a free tier with limited requests and compute time. Heavy usage incurs charges based on resources consumed.

Why does Chrome fail to launch in the container?

Missing flags like --no-sandbox or --headless, or Chrome not being installed in the image, are common causes. Check logs for exact errors.

Is Puppeteer the only option?

No, you can use Playwright or Selenium with ChromeDriver, but Puppeteer is lightweight and designed for headless Chrome.

Can I scrape websites with this setup?

Technically yes, but ensure compliance with the site’s terms of service and robots.txt. Some sites block automated access.

How do I secure the service?

Disable unauthenticated access in Cloud Run settings and use IAM roles. Add authentication at the application level if needed.

Does this work with other serverless platforms?

Similar setups work on AWS Lambda or Azure Functions, but container size and execution time limits may require adjustments.