Chrome Headless on Google Cloud Run: Setup Guide
In this article
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
- Create a new directory on your local machine for the project. Navigate into it using the terminal.
- Create a
main.jsfile (or equivalent for your preferred language) to run Puppeteer or another headless browser library. For Node.js, install Puppeteer withnpm init -yfollowed bynpm install puppeteer. Write a script that launches Chrome, navigates to a page, and performs an action like taking a screenshot. - In the same directory, create a
Dockerfilewith the following content:FROM node:18-slimInstall 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.listInstall Chrome
RUN apt-get update && apt-get install -y google-chrome-stableSet working directory
WORKDIR /appCopy application files
COPY package*.json ./ COPY main.js ./Install Node.js dependencies
RUN npm installAllow use of non-root user
USER nodeExpose port
EXPOSE 8080Start the server
CMD ["node", "main.js"] - Update your
main.jsto 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' }); - 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. - Build the container using Docker:
docker build -t chrome-headless .. Test it locally withdocker run -p 8080:8080 chrome-headlessand accesslocalhost:8080to verify it works. - 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. - 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.
- 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.
- 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-sandboxand--disable-setuid-sandboxflags, which are required in containerized environments. - Using
headless: trueinstead 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 nodeto match. - Underestimating memory—headless Chrome can use over 1 GB; set Cloud Run memory to 2 GB to avoid crashes.
Tips
- Use
--single-processto reduce memory overhead in constrained environments. - Set
--disable-dev-shm-usageto avoid tmpfs issues in containers with limited shared memory. - Log Chrome output during development by adding
--verboseand 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.