1

I'm trying to use selenium in a cloud run app that I'm working on. The app works correctly on my local machine but it's not working after I deploy it on google cloud run. The error I'm getting is related to not having chromedriver.exe installed on google cloud.

This is the traceback error from the logs:

Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise raise value File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/app/main.py", line 22, in daily_login login.create_access_token() File "/app/scripts/loginFlow/login.py", line 30, in create_access_token request_token = generate_request_token() File "/app/scripts/loginFlow/login.py", line 61, in generate_request_token driver = webdriver.Chrome() File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__ self.service.start() File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

The relevant part of that traceback seems to be Message: 'chromedriver' executable needs to be in PATH. How do I get the chromedriver executable in PATH for this google cloud run app?


Now, what have I tried to do about it?

First, I found this post: unable to run selenium chrome-driver on google-cloud-run and followed the instructions left in the comments. I looked at this post on dev.to and modeled my Dockerfile similar to what was suggested. The only difference is that I'm using Python3.6 instead of Python3.7:

# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.6-slim

# 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 https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
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.
ENV APP_HOME /app
WORKDIR $APP_HOME
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

I also added chromedriver-binary==77.0.3865.40.0 to my requirements.txt file.

When I try to deploy it using gcloud builds submit, I get this error:

/bin/sh: 1: wget: not found The command '/bin/sh -c wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb' returned a non-zero code: 127 ERROR ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 127

I'm been trying to find a solution for this error online but I'm lost. Your help would be greatly appreciated!

Abhay
  • 827
  • 9
  • 34
  • did you end up finding a solution for this? I'm trying to do the same thing. – ranni rabadi Jan 13 '21 at 12:41
  • Nope, I couldn't find a solution for this. From what I could understand, my options were to either write the script in nodejs with the pupetter library OR get a virtual machine and install selenium on it. I ended up getting a virtual machine on google compute engine and working with that. – Abhay Jan 15 '21 at 05:35

2 Answers2

3

Add a command to install wget.

# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install wget
John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • Thank you! That solved the problem with deploying the new build to cloud run. However, the fix I was trying didn't work as the error around `WebDriverException: Message: 'chromedriver' executable needs to be in PATH.` is still coming up. Any idea how to fix that? – Abhay May 18 '20 at 10:35
  • You should add this in the Dockerfile: # Install Python dependencies COPY ./ ./ RUN apk update RUN apk add curl RUN apk add unzip nano bash chromium chromium-chromedriver Because it seems that chromedriver is not found. – Stefan Neacsu May 18 '20 at 12:35
0

I was facing the same problem, finally got this to work! here is my Dockfile:

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 default-jdk

# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
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.
WORKDIR /app
COPY . .
CMD gunicorn --bind :$PORT --workers 1 --threads 3 main:app --timeout 90
ranni rabadi
  • 194
  • 2
  • 9