rstudio / chromote

Chrome Remote Interface for R
https://rstudio.github.io/chromote/
156 stars 20 forks source link

Problems running chromote in Google Cloud Run #180

Open aaelony-fb opened 2 months ago

aaelony-fb commented 2 months ago

Thanks for writing an excellent library!

Chrome is installed via Dockerfile based on rocker/r-ver:4.3.2.

Relevant parts of the Dockerfile for the DISPLAY env variable setting:

ENV DEBIAN_FRONTEND="noninteractive" \
    LANG=en_US.UTF-8 \
    LANGUAGE=en_US.UTF-8 \
    LC_ALL=C.UTF-8 \
    DISPLAY=:0

and chrome installation:

RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
    apt-get update && apt-get install -y ./google-chrome-stable_current_amd64.deb && \
    rm google-chrome-stable_current_amd64.deb && \
    apt-get install -f -y

## Chromium dependencies
RUN apt-get update && apt-get install -y \
    libcurl4-openssl-dev \
    libssl-dev \
    libxml2-dev \
    libxt-dev \
    libfontconfig1-dev \
    xvfb \
    wget \
    curl \
    chromium-driver

# Set the environment variables for Chromium
ENV CHROME_BIN=/usr/bin/chromium-browser
ENV CHROME_ARGS="--no-sandbox --disable-gpu --headless"

And chromote is used as follows from the R session:

library(chromote)
packageVersion("chromote")
[1] ‘0.2.0.9000’
b <- ChromoteSession$new()
b$Page$navigate(url)
b$Page$loadEventFired()
b$screenshot_pdf(
 filename = "test.pdf",
 pagesize = "Letter",
 margins = c(0.1, 0.1, 0.1, 0.1),
 units = "cm",
 landscape = TRUE,
 wait_ = TRUE
)
b$close()

The errors in Google cloud are typically lines stating:

Error in with_random_port()
! Cannot find an available port. Please try again.
Caused by error in startup()
Failed to start chrome. Error
Old Headless mode will be removed from the Chrome binary soon. 
Please use the new Headless mode (https://developer.chrome.com/docs/chromium/new-headless) 
or the chrome-headless-shell which is a standalone implementation of the old Headless mode (https://developer.chrome.com/blog/chrome-headless-shell).

As mentioned previously, this runs correctly from Macosx generally without issue (if the HTML file is too large it may a timeout and fail but works great for HTML files of normal size).

Any insight into how to make this work in Google Cloud Run per the errors above would be keenly appreciated! thank-you in advance.