skip to Main Content

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


  1. In your dockerfile, you can try replacing the chrome and chromedriver installation script with the following yaml:

    # install google chrome
    RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
    RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
    RUN apt-get -y update
    RUN apt-get install -y google-chrome-stable
    # install chromedriver
    RUN apt-get install -yqq unzip
    RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
    RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
    RUN python3 --version
    RUN pip3 --version
    RUN pip install --no-cache-dir --upgrade pip
    WORKDIR /app
    COPY ./requirements.txt /app/requirements.txt
    RUN pip3 install --no-cache-dir -r requirements.txt
    COPY . .
    EXPOSE 8080
    CMD ["gunicorn", "--bind", "0.0.0.0:8080","--timeout", "90", "app:app"]
    

    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.

    Login or Signup to reply.
  2. The binary location should point to chrome:

    options.binary_location = "/usr/bin/google-chrome"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search