python-3.xdockerselenium-webdriverselenium-chromedriver

Selenium Chrome Driver in Docker Container Not Running


I am new to docker. I need to scrape a website using Selenium chrome driver by Python from a docker container.

Dockerfile:

FROM python:3.11-slim-buster
# Install dependencies
RUN apt-get update && apt-get install -y wget unzip
# Download ChromeDriver
RUN wget https://storage.googleapis.com/chrome-for-testing-public/128.0.6613.84/win32/chrome-win32.zip
RUN unzip chrome-win32.zip -d /usr/local/bin/
# Copy your project files
COPY . /app
USER root
# Set the working directory
WORKDIR /app
# Install Python dependencies
RUN pip install selenium==3.141.0
RUN pip install urllib3==1.26.16
RUN pip install webdriver-manager
CMD ["python3", "seleniumdriver.py"]

Python Code:

from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
chromeOptions.add_argument("--no-sandbox")
driver = webdriver.Chrome(
    chrome_options=chromeOptions
)
driver.get("https://www.google.com/")
driver.quit()

Docker Build: docker -t seleniumdriver . Docker Run: docker seleniumdriver OS: Windows 10

Docker build is success but docker run is throwing an error.

raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Tried different approaches but nothing worked me.


Solution

  • A small change made it work.

    Dockerfile

    From

    ...
    RUN apt-get update && apt-get install -y wget unzip
    # Download ChromeDriver
    RUN wget https://storage.googleapis.com/chrome-for-testing-public/128.0.6613.84/win32/chrome-win32.zip
    RUN unzip chrome-win32.zip -d /usr/local/bin/
    ...
    

    To

    RUN apt-get update && apt-get install -y wget unzip chromium-driver
    

    Additionally, to verify that these changes work, I make the browser take a screenshot.

    You can use the following steps to verify.

    Dockerfile

    FROM python:3.11-slim-buster
    # Install dependencies
    RUN apt-get update && apt-get install -y wget unzip chromium-driver
    # Copy your project files
    COPY . /app
    USER root
    # Set the working directory
    WORKDIR /app
    # Install Python dependencies
    RUN pip install selenium==3.141.0
    RUN pip install urllib3==1.26.16
    RUN pip install webdriver-manager
    
    VOLUME ["/app"]
    
    CMD ["python3", "seleniumdriver.py"]
    

    seleniumdriver.py

    from selenium import webdriver
    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_argument("--headless")
    chromeOptions.add_argument("--no-sandbox")
    driver = webdriver.Chrome(
        options=chromeOptions
    )
    driver.get("https://www.google.com/")
    driver.get_screenshot_as_file("screenshot.png")
    driver.quit()
    

    Commands

    $ docker build -t seleniumdriver .
    $ docker run --name seleniumdriver -p 80:80 -v .:/app seleniumdriver 
    

    You could see screenshot.png in the same folder.