I have a Flask backend that uses Selenium to scrape some information, and I’ve created a Docker image that downloads Chrome and the Chromedriver necessary to do this. This is my Dockerfile:
FROM python:3.11.4-slim-bookworm
RUN apt-get update
&& apt-get install -y wget unzip
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update
&& apt-get install -y libglib2.0-0
libnss3
libgconf-2-4
libfontconfig1
libxcb1
# install chrome directly
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb --fix-missing; apt-get -fy install
# install chromedriver
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "-m", "flask", "run", "--host=0.0.0.0"]
Previously, I had installed Chromium with apt-get install rather than directly downloading it, which works, but I want to save some space for the Docker image, so I tried this method instead. When I run the script that create the Chromedriver, I get this error:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /google-chrome-stable_current_amd64.deb is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
The Chrome that I’ve installed gets downloaded to the root directory "/google-chrome-stable_current_amd64.deb", rather than the default path that the Chromedriver looks for at "/usr/bin/chromium", I’ve set the binary location, but it still doesn’t seem to work.
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--headless=new')
options.binary_location = "/google-chrome-stable_current_amd64.deb"
driver = webdriver.Chrome(options=options)
I’m not sure what the issue is, maybe with the Chrome that I’m downloading, or maybe the path that I gave.
2
Answers
In your dockerfile, you can try replacing the chrome and chromedriver installation script with the following yaml:
This installs both chrome and chromium at their default locations and uses gunicorn server which is necessary when dockerizing flask based backends as flask doesnt have the capacity to handle production traffic.
The binary location should point to chrome: