I have a Flask app that uses Selenium WebDriver deployed on Google Cloud in a Docker container. When ran locally (without Docker), the app works fine. However, once deployed, it throws the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask_cors/extension.py", line 178, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
File "/backend/app.py", line 52, in login
driver = webdriver.Chrome(options)
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
super().__init__(
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 55, in __init__
self.service.start()
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 102, in start
self.assert_process_still_running()
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 115, in assert_process_still_running
raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service /home/.cache/selenium/chromedriver/linux64/127.0.6533.72/chromedriver unexpectedly exited. Status code was: 127
The line causing the error is the last line in the snippet below of the Flask app:
@app.route('/login', methods=['POST'])
def login():
data = request.get_json()
username = data.get('username')
password = data.get('password')
options = Options()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options)
Since the app works when I run it locally, my guess is that it has something to do with how I’m configuring my Dockerfile (e.g. not installing Chrome properly, not installing compatible versions of Chrome and ChromeDriver, etc.). I’m quite new to Docker so I’m still not very familiar with creating Dockerfiles, but I think the general idea is to install a fixed version of Chrome with a compatible ChromeDriver so that the app (contained within the container) can run anywhere after being deployed.
I’ve tried modifying my Dockerfile to install compatible versions of Chrome and ChromeDriver to no avail, so either I did it wrong or it was not the correct solution anyways. I’ve since reverted my Dockerfile back to a previous, “cleaner” state:
FROM python:3.9-slim
WORKDIR /backend
RUN apt-get update && apt-get install -y
build-essential
libffi-dev
libssl-dev
&& rm -rf /var/lib/apt/lists/*
COPY . /backend
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8080
CMD ["gunicorn", "-b", ":8080", "app:app"]
I would greatly appreciate any help. Thank you.