DEV Community

Dustin Ingram for Google Cloud

Posted on • Updated on

Using Headless Chrome with Cloud Run

I often see folks trying to use headless Chrome with services like Google Cloud Functions. The phrase "Headless Chrome" might sound very spooky, but it just means the regular Chrome browser, run without a GUI and instead interacted with programatically.

Unfortunately, the necessary Chrome binaries are not installed in the Cloud Functions runtime, and there isn't a way to modify the runtime besides installing Python dependencies.

However, one alternative would be to use Cloud Run, which lets you fully customize the runtime, including installing Chrome! So let's do that.

Configuring: Dockerfile

First, we'll create a Dockerfile. This uses the official Python base image, installs some additional dependencies, installs Chrome, and installs the dependencies for our application.

# Use the official Python image.
FROM python:3.7

# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils

# Install Chrome
RUN wget
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install

# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

# Copy local code to the container image.
COPY . .

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
Enter fullscreen mode Exit fullscreen mode

Configuring: requirements.txt

This Dockerfile uses a requirements.txt file with specific versions of all our Python dependencies. We'll need to install selenium as well as the specific version of the chromedriver-binary project that corresponds with the version of Chrome that we've installed:

# requirements.txt

Enter fullscreen mode Exit fullscreen mode


Finally, we'll write a Python application using Flask, Selenium


from flask import Flask, send_file
from selenium import webdriver
import chromedriver_binary  # Adds chromedriver binary to path

app = Flask(__name__)

# The following options are required to make headless Chrome
# work in a Docker container
chrome_options = webdriver.ChromeOptions()

# Initialize a new browser
browser = webdriver.Chrome(chrome_options=chrome_options)

def hello_world():
    return send_file("spooky.png")
Enter fullscreen mode Exit fullscreen mode

Testing & Deploying

If we have Docker installed locally, we can run this to test it:

$ docker build -t my_screenshot_service .
$ docker run --rm -p 8080:8080 -e PORT=8080 my_screenshot_service
Enter fullscreen mode Exit fullscreen mode

And view it at http://localhost:8080

Alt Text

Otherwise, we can deploy it directly to Cloud Run:

$ gcloud builds submit --tag
$ gcloud beta run deploy my_screenshot_service --image --region us-central1 --platform managed
Enter fullscreen mode Exit fullscreen mode

And that's it!

A few notes:

  • We're using --no-sandbox to ensure compatibility with the Docker container, so only point such a service towards URLs you trust.
  • Be careful when exposing such a service to user input: For example, if the URL we were screenshotting was supplied by the user, they could potentially take a screenshot of any file on the filesystem as well!
  • Be sure to create a new service account with no permission and use it as the identity of the service, for better security. See for an example.

Top comments (15)

kev26 profile image

If my local code from windows machine (C:\Users\KEV\pathmycode), how will i set path for this case ? I tried on linux is OK, but with windows, i can't to set them ! I'm newbie and Thanks for your help !!!

johannesoak profile image
Johannes Eklund

I ran into issues when deploying it to Cloud Run. The fix for me was too add that "wget" should be installed otherwise chrome wasn't able to be downloaded.

So this was the whole line I ran in the Docker file
RUN apt-get install -y wget gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils

shreyasashtamkar profile image
Shreyas Ashtamkar

If you are using anything after python 3.7 you would face that issue, since it uses Debian Bullseye from when that package is unavailable.

try this -
FROM python: 3.8-buster

worked for me.

alex512byte profile image

I am getting the following error when I try to implement this, is anyone else?

Update failed with error code BUILD_USER_ERROR
unable to stream build output: The command '/bin/sh -c apt-get install -y wget gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils' returned a non-zero code: 100. Please fix the Dockerfile and try again..
Exited with code 1.

derek_p_baker profile image
Derek Baker

If anyone is interesting in an updated version of this content that outlines some pitfalls involved when running locally (outside of Docker), I threw this repo on Github:

matti__4247fc773325 profile image
Matti Jensen

Local docker - I tried python 3.8-buster which solves the issue that Alex512* has.
But running the container still fails;

[2022-01-10 15:59:26 +0000] [1] [INFO] Using worker: threads
/usr/local/lib/python3.8/ RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
return, *args, **kwargs)
[2022-01-10 15:59:26 +0000] [9] [INFO] Booting worker with pid: 9
[2022-01-10 15:59:27 +0000] [9] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/gunicorn/", line 583, in spawn_worker

Justin, would you be able to update/fix the guide - thanks!

ahmetb profile image
Ahmet Alp Balkan • Edited

Nice post, thanks! When I deployed this by copy-pasting, I got HTTP 500. Logs show "Memory limit of 256M exceeded with 282M used. Consider increasing the memory limit." You might want to add this argument (and maybe make the URL configurable with ?url=).

chongleichen profile image

This comment is for anyone finding that chrome keeps crashing inside their container, it could be due to the machine you're running on. For me it was an M1 mac. I found later that it works fine on Cloud Run itself. There may be some compatibility issues with M1 macs that I need to figure out.

manishfoodtechs profile image
manish srivastava

Thanks Dustin, for this article

sulun profile image

Saved my day, thanks!

estbelari profile image
Esteban Beltrán

I did not know what version of chrome I have installed. It helped me install chromedriver-binary-auto. Automatically detects the required version.

pip install chromedriver-binary-auto


zhenyang0405 profile image
Zhen Yang

I'm using windows, when I build the docker file, It failed.

Unable to locate package libappindicator1

I tried online, but couldn't find any solution. Please help.
Thank you

kev26 profile image

Thank you so much !!!

danielsanthiago profile image

Thank you Dustin!
How can I download the Google Chrome 77 with wget?

jon0010 profile image
Jon Nahuel Pereyra

in my local environment it works but in cloud run the deployed version gives me a timeError waiting for selector waiting failed: 30000ms exceeded