Selenium within a Docker container can't find chromedriver

Viewed 1417

I need to put into a Docker container my little Flask app that goes and check what type of Google Tags my company's clients have installed. For that i need to have selenium-wire . You supply a website and you get a json back telling you which tags are installed ( a bit like http://gachecker.com/ ). Now it works just fine with the Flask App. The issue arises when i try to put it into Docker, here is my docker script:

FROM python:3.9 WORKDIR /bziiit_checker_app

RUN pip install flask flask_restful requests BeautifulSoup4 selenium-wire undetected-chromedriver chromedriver-py

COPY ./app ./app

CMD ["python", "./app/main.py"]

Once it's in Docker and try to run it, i get that message

"selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH"

Which is a common issue when the chromedriver.exe file is not in the working directory. But it IS. Do i need to set the PATH when i'm creating the virtual environment, and if so how do i do that? Again, i'm good at A.I, terrible at app development.

I'm using Python 3.9 and am on Windows 10, Visual Studio Code, and Flask

Thank you

2 Answers

After a few days of pain and suffering i finally worked it out, so here is the Docker file i created to get chromedriver to work in a Docker container.

This works on Windows 10 using VS code

FROM python:3.8

# Adding trusting keys to apt for repositories
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -

# Adding Google Chrome to the repositories
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'

# Updating apt to see and install Google Chrome
RUN apt-get -y update

# Magic happens
RUN apt-get install -y google-chrome-stable

# Installing Unzip
RUN apt-get install -yqq unzip

# Download the Chrome Driver
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/

# Set display port as an environment variable
ENV DISPLAY=:99

COPY ./app ./app
WORKDIR /app 

RUN pip install --upgrade pip

RUN pip install -r requirements.txt

CMD ["python", "./main.py"]

Then, in your script, add those arguments to Chromedriver's options otherwise it'll give you an error message telling you that "Chromedriver has exited abnormally"

option = webdriver.ChromeOptions()

option.add_argument("--disable-gpu")
option.add_argument("--disable-extensions")
option.add_argument("--disable-infobars")
option.add_argument("--start-maximized")
option.add_argument("--disable-notifications")
option.add_argument('--headless')
option.add_argument('--no-sandbox')
option.add_argument('--disable-dev-shm-usage')

I hope this will save someone all the headache that problem gave me

You will also have to install chrome driver and chrome inside your container

RUN add-apt-repository -y ppa:openjdk-r/ppa
RUN apt-get install -y openjdk-12-jre cron wget unzip

ARG CHROME_VERSION=78.0.3904.87-1
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
    && apt-get update -qqy \
    && apt-get -qqy install google-chrome-stable=$CHROME_VERSION \
    && rm /etc/apt/sources.list.d/google-chrome.list \
    && rm -rf /var/lib/apt/lists/* /var/cache/apt/* \
        && sed -i 's/"$HERE\/chrome"/"$HERE\/chrome" --no-sandbox/g' /opt/google/chrome/google-chrome


ARG CHROME_DRIVER_VERSION=78.0.3904.70
RUN wget --no-verbose -O /tmp/chromedriver_linux64.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
    && rm -rf /opt/chromedriver \
    && unzip /tmp/chromedriver_linux64.zip -d /opt \
    && rm /tmp/chromedriver_linux64.zip \
    && mv /opt/chromedriver /opt/chromedriver-$CHROME_DRIVER_VERSION \
    && chmod 755 /opt/chromedriver-$CHROME_DRIVER_VERSION \
    && ln -fs /opt/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver


Related