flowerchild245
flowerchild245

Reputation: 1

How do I configure Dockerfile to Run Chrome with Selenium?

I'm trying to set up a Docker container to run a Python Selenium script with Google Chrome on Digital Ocean. My Dockerfile is designed to install Google Chrome and Chromedriver so that my Selenium script can scrape data from a webpage. However, I'm encountering an error when trying to install Google Chrome.

# Use a lightweight Python base image
FROM python:3.10

WORKDIR /app

COPY . /app

RUN pip install --trusted-host pypi.python.org -r requirements.txt

# install google chrome
RUN apt-get update && \
    apt-get install -y wget gnupg && \
    wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-linux-signing-key.gpg && \
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-linux-signing-key.gpg] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list && \
    apt-get update && \
    apt-get install -y google-chrome-stable

# install chromedriver
RUN apt-get install -yqq unzip && \
    wget -O /tmp/chromedriver.zip "http://chromedriver.storage.googleapis.com/$(curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE)/chromedriver_linux64.zip" && \
    unzip /tmp/chromedriver.zip -d /usr/local/bin/

# set display port to avoid crash
ENV DISPLAY=:99

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

The error I get is: E: Unable to locate package google-chrome-stable

What I've tried:

Another solution I've tried is:

# Use a lightweight Python base image
FROM python:3.10-slim

# Avoid interactive prompts during package installations
ENV DEBIAN_FRONTEND=noninteractive

# Install system dependencies required by Chrome and ChromeDriver
RUN apt-get update && apt-get install -y \
    chromium-driver \
    chromium \
    fonts-liberation \
    libnss3 \
    libx11-6 \
    libatk-bridge2.0-0 \
    libatspi2.0-0 \
    libgtk-3-0 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxrandr2 \
    libgbm-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Set environment variables for Chromium and ChromeDriver paths
ENV CHROME_BIN=/usr/bin/chromium \
    CHROME_DRIVER_BIN=/usr/bin/chromedriver

# Set the working directory in the container
WORKDIR /app

# Copy the requirements.txt file to the working directory
COPY requirements.txt .

# Install Python dependencies from the requirements file
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code to the working directory
COPY . .

# Expose the port your application will run on
EXPOSE 8080

This solution causes this error during deployment: File "/workspace/app/scraper.py", line 21, in driver = webdriver.Chrome(options=chrome_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in init super().init( File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/chromium/webdriver.py", line 55, in init self.service.start() File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 105, in start self.assert_process_still_running() File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 118, in assert_process_still_running raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}") selenium.common.exceptions.WebDriverException: Message: Service /workspace/.cache/selenium/chromedriver/linux64/130.0.6723.69/chromedriver unexpectedly exited. Status code was: 127

What I've tried:

Another solution I've tried is:

# Use a lightweight Python base image
FROM python:3.10-slim

# Avoid interactive prompts during package installations
ENV DEBIAN_FRONTEND=noninteractive

# Install system dependencies required by Chrome and ChromeDriver
RUN apt-get update && apt-get install -y \
    chromium-driver \
    chromium \
    fonts-liberation \
    libnss3 \
    libx11-6 \
    libatk-bridge2.0-0 \
    libatspi2.0-0 \
    libgtk-3-0 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxrandr2 \
    libgbm-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Set environment variables for Chromium and ChromeDriver paths
ENV CHROME_BIN=/usr/bin/chromium \
    CHROME_DRIVER_BIN=/usr/bin/chromedriver

# Set the working directory in the container
WORKDIR /app

# Copy the requirements.txt file to the working directory
COPY requirements.txt .

# Install Python dependencies from the requirements file
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code to the working directory
COPY . .

# Expose the port your application will run on
EXPOSE 8080

This solution causes this error during deployment: File "/workspace/app/scraper.py", line 21, in driver = webdriver.Chrome(options=chrome_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in init super().init( File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/chromium/webdriver.py", line 55, in init self.service.start() File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 105, in start self.assert_process_still_running() File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 118, in assert_process_still_running raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}") selenium.common.exceptions.WebDriverException: Message: Service /workspace/.cache/selenium/chromedriver/linux64/130.0.6723.69/chromedriver unexpectedly exited. Status code was: 127

Upvotes: 0

Views: 342

Answers (2)

Harry
Harry

Reputation: 1

Why not use an official selenium image as a base image? https://hub.docker.com/r/selenium/standalone-chromium

FROM selenium/standalone-chromium

Upvotes: 0

NayeemJohn
NayeemJohn

Reputation: 46

I used to follow the below step in dockerfile for installing chrome in Linux Ubuntu machine. You can try this

      RUN apt-get update
      RUN mkdir -p /var/lib/apt/lists/partial
      RUN apt install -y libsm6 libxext6 ffmpeg libfontconfig1 libxrender1 libgl1-mesa-glx
      RUN apt -y --force-yes install libu2f-udev fonts-liberation libasound2  libatk-bridge2.0-0 libatk1.0-0  libatspi2.0-0  libcairo2 libcups2 libdrm2 libgbm1 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libx11-6 libxcb1 libxcomposite1 libxdamage1 libxext6 libxfixes3 libxkbcommon0 libxrandr2 xdg-utils
      RUN apt -y --force-yes install wget
      RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
      RUN dpkg -i google-chrome-stable_current_amd64.deb
      RUN apt install -f

You may also refer this simple dockerfile

Upvotes: 0

Related Questions