Skip to content

Instantly share code, notes, and snippets.

@varyonic
Created June 10, 2016 14:14
Show Gist options
  • Star 79 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save varyonic/dea40abcf3dd891d204ef235c6e8dd79 to your computer and use it in GitHub Desktop.
Save varyonic/dea40abcf3dd891d204ef235c6e8dd79 to your computer and use it in GitHub Desktop.
Dockerfile with chromedriver
# See https://codeship.com/documentation/docker/browser-testing/
FROM myapp:base
# We need wget to set up the PPA and xvfb to have a virtual screen and unzip to install the Chromedriver
RUN apt-get install -y wget xvfb unzip
# Set up the Chrome PPA
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
# Update the package list and install chrome
RUN apt-get update -y
RUN apt-get install -y google-chrome-stable
# Set up Chromedriver Environment variables
ENV CHROMEDRIVER_VERSION 2.19
ENV CHROMEDRIVER_DIR /chromedriver
RUN mkdir $CHROMEDRIVER_DIR
# Download and install Chromedriver
RUN wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
RUN unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR
# Put Chromedriver into the PATH
ENV PATH $CHROMEDRIVER_DIR:$PATH
@Foundsheep
Copy link

I'm not quite sure if I'm right, since I'm still trying to solve the problem here.

However, just to raise another point for someone else, I don't agree with @mgtezak's point.

I've got an below error as he mentioned,

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

but I think the problem is not about the actual code in Python.

Here are the points

  • the google chrome version installed through apt-get install google-chrome-stable is, as of now, 119.xxx something
  • driver I can get through the command ...chrome-for-testing/$CHROME_VERSION/linux64/chromedriver-linux64.zip doesn't work as mentioned in the replies above(it only works for version older than 115.xxx)
  • so I tried to install the driver manually with a version 114.xxx something
  • then, it seems like they are all installed, but it crashes when the actual Python code tries to establish the driver
    • and this is where I got the ChromeDriver is assuming that Chrome has crashed. error
    • and this is because the version of installed google chrome is 119.xxx something and the driver is 114.xxx something
  • so the whole point is we should find a way to install a fixed version of google chrome, rather than apt-get install google-chrome-stable, and that fixed version should be available in the API to get the driver

@Foundsheep
Copy link

Ok, so now I've figured it out myself and here is how I did it.

I referenced @Anil86's command, since it was easier to debug when the error had occured.

RUN apt-get install -y wget xvfb unzip
# Set up the Chrome PPA -> (not sure if needed)
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list

# Update the package list
RUN apt-get update -y

# Set up Chromedriver Environment variables and install chrome
ENV CHROMEDRIVER_VERSION 114.0.5735.90
ENV CHROME_VERSION 114.0.5735.90-1
RUN wget --no-verbose -O /tmp/chrome.deb [https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb](https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_$%7BCHROME_VERSION%7D_amd64.deb) \
  && apt install -y /tmp/chrome.deb \
  && rm /tmp/chrome.deb

ENV CHROMEDRIVER_DIR /chromedriver
RUN mkdir $CHROMEDRIVER_DIR

# Download and install Chromedriver
RUN wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
RUN unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR

# Put Chromedriver into the PATH
ENV PATH $CHROMEDRIVER_DIR:$PATH

Here is how I configured in Python selenium

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import os

CHROMEDRIVER_DIR = os.getenv("CHROMEDRIVER_DIR")
DRIVER_PATH = os.path.join(CHROMEDRIVER_DIR, "chromedriver")

options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
service = Service(executable_path=DRIVER_PATH)

driver = webdriver.Chrome(options=options, service=service)

@waracci
Copy link

waracci commented Nov 23, 2023

RUN wget --no-verbose -O /tmp/chrome.deb [https://dl.google.com/linux/chrome/deb/pool/main

The line RUN wget --no-verbose -O /tmp/chrome.deb [https://dl.google.com/linux/chrome/deb/pool/main seems to have been copied with some markdown formatting. Correct code should be:

RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
  && apt install -y /tmp/chrome.deb \
  && rm /tmp/chrome.deb

otherwise you will run into an error: 0.184 /bin/sh: 1: Syntax error: "(" unexpected if you run the code as is.

Thank you @Foundsheep for your solution, worked well for me!

@ankitarya1019
Copy link

ankitarya1019 commented Feb 6, 2024

Here is my working version, a slight different take incase it helps:

FROM python:3.10.2-bullseye

# Install dependencies
RUN apt-get update -y && apt-get install -y wget xvfb unzip jq

# Install Google Chrome dependencies
RUN apt-get install -y libxss1 libappindicator1 libgconf-2-4 \
    fonts-liberation libasound2 libnspr4 libnss3 libx11-xcb1 libxtst6 lsb-release xdg-utils \
    libgbm1 libnss3 libatk-bridge2.0-0 libgtk-3-0 libx11-xcb1 libxcb-dri3-0


# Fetch the latest version numbers and URLs for Chrome and ChromeDriver
RUN curl -s https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json > /tmp/versions.json

RUN CHROME_URL=$(jq -r '.channels.Stable.downloads.chrome[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chrome-linux64.zip $CHROME_URL && \
    unzip /tmp/chrome-linux64.zip -d /opt/chrome

RUN chmod +x /opt/chrome/chrome-linux64/chrome


RUN CHROMEDRIVER_URL=$(jq -r '.channels.Stable.downloads.chromedriver[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chromedriver-linux64.zip $CHROMEDRIVER_URL && \
    unzip /tmp/chromedriver-linux64.zip -d /opt/chromedriver && \
    chmod +x /opt/chromedriver/chromedriver-linux64/chromedriver

# Set up Chromedriver Environment variables
ENV CHROMEDRIVER_DIR /opt/chromedriver
ENV PATH $CHROMEDRIVER_DIR:$PATH

# Clean upa
RUN rm /tmp/chrome-linux64.zip /tmp/chromedriver-linux64.zip /tmp/versions.json

# python dependencies
RUN pip install selenium

# Copy your Python script into the container
COPY test_chromedriver.py /opt/test_chromedriver.py

# Command to run the script
CMD ["python", "/opt/test_chromedriver.py"]
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
import time

def test_chromedriver_installation():
    # Setup Chrome options
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--disable-gpu")  # This is important for some versions of Chrome
    chrome_options.add_argument("--remote-debugging-port=9222")  # This is recommended

    # Set path to Chrome binary
    chrome_options.binary_location = "/opt/chrome/chrome-linux64/chrome"

    # Set path to ChromeDriver
    chrome_service = ChromeService(executable_path="/opt/chromedriver/chromedriver-linux64/chromedriver")

    # Set up driver
    driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

    try:
        # URL to test
        driver.get("http://example.com")

        # Give the browser time to load all content.
        time.sleep(2)

        # Find element by tag
        element = driver.find_element(By.TAG_NAME, "h1")

        # Print the text of the element
        print(element.text)

        # Check if the text is as expected
        assert "Example Domain" in element.text
        print("ChromeDriver is installed and working as expected.")

    except Exception as e:
        print(f"An error occurred: {e}")

    finally:
        # Close the browser
       # Close the browser
        driver.quit()

test_chromedriver_installation()

@leletaurino
Copy link

leletaurino commented Feb 6, 2024

Hi, I'm using this dockerfile

    FROM --platform=linux/amd64 python:3.11.5 as build-stage
    
    RUN apt-get update -y && apt-get install -y wget unzip xserver-xorg-video-dummy
    
    RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
    RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
    
    RUN apt-get update -y
    
    ENV CHROMEDRIVER_VERSION 114.0.5735.90
    ENV CHROME_VERSION 114.0.5735.90-1
    RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
      && apt install -y /tmp/chrome.deb \
      && rm /tmp/chrome.deb
    
    ENV CHROMEDRIVER_DIR /chromedriver
    RUN mkdir $CHROMEDRIVER_DIR
    
    RUN wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
    RUN unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR
    
    ENV PATH $CHROMEDRIVER_DIR:$PATH
    
    WORKDIR /app
    
    ENV PYTHONDONTWRITEBYTECODE 1
    ENV PYTHONUNBUFFERED 1
    
    RUN pip install --upgrade pip
    COPY ./requirements.txt .
    RUN pip install -r requirements.txt
    
    ENV DISPLAY=:1
    ENV HEADLESS=1
    
    COPY . ./

e that is my code:

         options = Options()
      
          options.add_argument("--disable-gpu")
      
          options.add_argument("--disable-extensions") 
      
          options.add_argument("--disable-infobars")
      
          options.add_argument("--start-maximized")
      
          options.add_argument("--disable-notifications")
      
          options.add_argument('--no-sandbox')
      
          options.add_argument('--disable-dev-shm-usage')

But since yesterday it's not working anymore:

          (chrome not reachable)
          
          fastapiproject-web-1    |   (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver
          is assuming that Chrome has crashed.)

can someone help me?

Thanks in advance.

@manel00
Copy link

manel00 commented Feb 9, 2024

Hi, I'm using this dockerfile

    FROM --platform=linux/amd64 python:3.11.5 as build-stage
    
    RUN apt-get update -y && apt-get install -y wget unzip xserver-xorg-video-dummy
    
    RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
    RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
    
    RUN apt-get update -y
    
    ENV CHROMEDRIVER_VERSION 114.0.5735.90
    ENV CHROME_VERSION 114.0.5735.90-1
    RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
      && apt install -y /tmp/chrome.deb \
      && rm /tmp/chrome.deb
    
    ENV CHROMEDRIVER_DIR /chromedriver
    RUN mkdir $CHROMEDRIVER_DIR
    
    RUN wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
    RUN unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR
    
    ENV PATH $CHROMEDRIVER_DIR:$PATH
    
    WORKDIR /app
    
    ENV PYTHONDONTWRITEBYTECODE 1
    ENV PYTHONUNBUFFERED 1
    
    RUN pip install --upgrade pip
    COPY ./requirements.txt .
    RUN pip install -r requirements.txt
    
    ENV DISPLAY=:1
    ENV HEADLESS=1
    
    COPY . ./

e that is my code:

         options = Options()
      
          options.add_argument("--disable-gpu")
      
          options.add_argument("--disable-extensions") 
      
          options.add_argument("--disable-infobars")
      
          options.add_argument("--start-maximized")
      
          options.add_argument("--disable-notifications")
      
          options.add_argument('--no-sandbox')
      
          options.add_argument('--disable-dev-shm-usage')

But since yesterday it's not working anymore:

          (chrome not reachable)
          
          fastapiproject-web-1    |   (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver
          is assuming that Chrome has crashed.)

can someone help me?

Thanks in advance.

Same problem... i just saw your report

@manel00
Copy link

manel00 commented Feb 9, 2024

RUN wget --no-verbose -O /tmp/chrome.deb [https://dl.google.com/linux/chrome/deb/pool/main

The line RUN wget --no-verbose -O /tmp/chrome.deb [https://dl.google.com/linux/chrome/deb/pool/main seems to have been copied with some markdown formatting. Correct code should be:

RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
  && apt install -y /tmp/chrome.deb \
  && rm /tmp/chrome.deb

otherwise you will run into an error: 0.184 /bin/sh: 1: Syntax error: "(" unexpected if you run the code as is.

Thank you @Foundsheep for your solution, worked well for me!

still it doesnt work... getting this error:

image

Anyone knows where is the problem?

@leletaurino
Copy link

leletaurino commented Feb 9, 2024

it's working for me in this way:

        # pull official base image
        FROM --platform=linux/amd64 python:3.11.5 as build-stage
        
        RUN apt-get update -y && apt-get install -y wget unzip xserver-xorg-video-dummy
        
        # RUN apt-get install -y wget xvfb unzip
        # Set up the Chrome PPA -> (not sure if needed)
        RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
        RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
        
        # Update the package list
        RUN apt-get update -y
        
        # Set up Chromedriver Environment variables and install chrome
        ENV CHROMEDRIVER_VERSION 114.0.5735.90
        ENV CHROME_VERSION 114.0.5735.90-1
        RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
          && apt install -y /tmp/chrome.deb \
          && rm /tmp/chrome.deb
        
        ENV CHROMEDRIVER_DIR /chromedriver
        RUN mkdir $CHROMEDRIVER_DIR
        
        # Download and install Chromedriver
        RUN wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
        RUN unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR
        
        # Put Chromedriver into the PATH
        ENV PATH $CHROMEDRIVER_DIR:$PATH
        
        # set working directory
        WORKDIR /app
        
        # set environment variables
        ENV PYTHONDONTWRITEBYTECODE 1
        ENV PYTHONUNBUFFERED 1
        
        # install dependencies
        RUN pip install --upgrade pip
        COPY ./requirements.txt .
        RUN pip install -r requirements.txt
        
        # Set an environment variable to run headless (optional but useful for containerized environments)
        ENV DISPLAY=:1
        
        # add app
        COPY . ./

The issue appears to be related to the Docker version. It works with Docker 20.10.14 on both Mac and Linux.

@emgullufsen-lii
Copy link

Thanks @varyonic - nice to have this in a gist instead of digging around in my shell history to get the install lines and populate the Dockerfile myself.

@ketank1000
Copy link

Here is my working version, a slight different take incase it helps:

FROM python:3.10.2-bullseye

# Install dependencies
RUN apt-get update -y && apt-get install -y wget xvfb unzip jq

# Install Google Chrome dependencies
RUN apt-get install -y libxss1 libappindicator1 libgconf-2-4 \
    fonts-liberation libasound2 libnspr4 libnss3 libx11-xcb1 libxtst6 lsb-release xdg-utils \
    libgbm1 libnss3 libatk-bridge2.0-0 libgtk-3-0 libx11-xcb1 libxcb-dri3-0


# Fetch the latest version numbers and URLs for Chrome and ChromeDriver
RUN curl -s https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json > /tmp/versions.json

RUN CHROME_URL=$(jq -r '.channels.Stable.downloads.chrome[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chrome-linux64.zip $CHROME_URL && \
    unzip /tmp/chrome-linux64.zip -d /opt/chrome

RUN chmod +x /opt/chrome/chrome-linux64/chrome


RUN CHROMEDRIVER_URL=$(jq -r '.channels.Stable.downloads.chromedriver[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chromedriver-linux64.zip $CHROMEDRIVER_URL && \
    unzip /tmp/chromedriver-linux64.zip -d /opt/chromedriver && \
    chmod +x /opt/chromedriver/chromedriver-linux64/chromedriver

# Set up Chromedriver Environment variables
ENV CHROMEDRIVER_DIR /opt/chromedriver
ENV PATH $CHROMEDRIVER_DIR:$PATH

# Clean upa
RUN rm /tmp/chrome-linux64.zip /tmp/chromedriver-linux64.zip /tmp/versions.json

# python dependencies
RUN pip install selenium

# Copy your Python script into the container
COPY test_chromedriver.py /opt/test_chromedriver.py

# Command to run the script
CMD ["python", "/opt/test_chromedriver.py"]
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
import time

def test_chromedriver_installation():
    # Setup Chrome options
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--disable-gpu")  # This is important for some versions of Chrome
    chrome_options.add_argument("--remote-debugging-port=9222")  # This is recommended

    # Set path to Chrome binary
    chrome_options.binary_location = "/opt/chrome/chrome-linux64/chrome"

    # Set path to ChromeDriver
    chrome_service = ChromeService(executable_path="/opt/chromedriver/chromedriver-linux64/chromedriver")

    # Set up driver
    driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

    try:
        # URL to test
        driver.get("http://example.com")

        # Give the browser time to load all content.
        time.sleep(2)

        # Find element by tag
        element = driver.find_element(By.TAG_NAME, "h1")

        # Print the text of the element
        print(element.text)

        # Check if the text is as expected
        assert "Example Domain" in element.text
        print("ChromeDriver is installed and working as expected.")

    except Exception as e:
        print(f"An error occurred: {e}")

    finally:
        # Close the browser
       # Close the browser
        driver.quit()

test_chromedriver_installation()

The builds passes perfectly for me but for testing it fails with driver creation
root@ffb9dfc2e30a:/workspace# python yoda/utils/test.py Traceback (most recent call last): File "/workspace/yoda/utils/test.py", line 50, in <module> test_chromedriver_installation() File "/workspace/yoda/utils/test.py", line 23, in test_chromedriver_installation driver = webdriver.Chrome(service=chrome_service, options=chrome_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__ super().__init__( File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/chromium/webdriver.py", line 50, in __init__ self.service.start() File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 102, in start self.assert_process_still_running() File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 115, in assert_process_still_running raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}") selenium.common.exceptions.WebDriverException: Message: Service /opt/chromedriver/chromedriver-linux64/chromedriver unexpectedly exited. Status code was: 255

I used M1 chip.

@jamesputtmann
Copy link

Here is my working version, a slight different take incase it helps:

FROM python:3.10.2-bullseye

# Install dependencies
RUN apt-get update -y && apt-get install -y wget xvfb unzip jq

# Install Google Chrome dependencies
RUN apt-get install -y libxss1 libappindicator1 libgconf-2-4 \
    fonts-liberation libasound2 libnspr4 libnss3 libx11-xcb1 libxtst6 lsb-release xdg-utils \
    libgbm1 libnss3 libatk-bridge2.0-0 libgtk-3-0 libx11-xcb1 libxcb-dri3-0


# Fetch the latest version numbers and URLs for Chrome and ChromeDriver
RUN curl -s https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json > /tmp/versions.json

RUN CHROME_URL=$(jq -r '.channels.Stable.downloads.chrome[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chrome-linux64.zip $CHROME_URL && \
    unzip /tmp/chrome-linux64.zip -d /opt/chrome

RUN chmod +x /opt/chrome/chrome-linux64/chrome


RUN CHROMEDRIVER_URL=$(jq -r '.channels.Stable.downloads.chromedriver[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chromedriver-linux64.zip $CHROMEDRIVER_URL && \
    unzip /tmp/chromedriver-linux64.zip -d /opt/chromedriver && \
    chmod +x /opt/chromedriver/chromedriver-linux64/chromedriver

# Set up Chromedriver Environment variables
ENV CHROMEDRIVER_DIR /opt/chromedriver
ENV PATH $CHROMEDRIVER_DIR:$PATH

# Clean upa
RUN rm /tmp/chrome-linux64.zip /tmp/chromedriver-linux64.zip /tmp/versions.json

# python dependencies
RUN pip install selenium

# Copy your Python script into the container
COPY test_chromedriver.py /opt/test_chromedriver.py

# Command to run the script
CMD ["python", "/opt/test_chromedriver.py"]
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
import time

def test_chromedriver_installation():
    # Setup Chrome options
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--disable-gpu")  # This is important for some versions of Chrome
    chrome_options.add_argument("--remote-debugging-port=9222")  # This is recommended

    # Set path to Chrome binary
    chrome_options.binary_location = "/opt/chrome/chrome-linux64/chrome"

    # Set path to ChromeDriver
    chrome_service = ChromeService(executable_path="/opt/chromedriver/chromedriver-linux64/chromedriver")

    # Set up driver
    driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

    try:
        # URL to test
        driver.get("http://example.com")

        # Give the browser time to load all content.
        time.sleep(2)

        # Find element by tag
        element = driver.find_element(By.TAG_NAME, "h1")

        # Print the text of the element
        print(element.text)

        # Check if the text is as expected
        assert "Example Domain" in element.text
        print("ChromeDriver is installed and working as expected.")

    except Exception as e:
        print(f"An error occurred: {e}")

    finally:
        # Close the browser
       # Close the browser
        driver.quit()

test_chromedriver_installation()

The builds passes perfectly for me but for testing it fails with driver creation root@ffb9dfc2e30a:/workspace# python yoda/utils/test.py Traceback (most recent call last): File "/workspace/yoda/utils/test.py", line 50, in <module> test_chromedriver_installation() File "/workspace/yoda/utils/test.py", line 23, in test_chromedriver_installation driver = webdriver.Chrome(service=chrome_service, options=chrome_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__ super().__init__( File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/chromium/webdriver.py", line 50, in __init__ self.service.start() File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 102, in start self.assert_process_still_running() File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 115, in assert_process_still_running raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}") selenium.common.exceptions.WebDriverException: Message: Service /opt/chromedriver/chromedriver-linux64/chromedriver unexpectedly exited. Status code was: 255

I used M1 chip.

Yes I am in the same boat here...

@ikotun-dev
Copy link

Does anyone have a working code for M1 chip?

@ikotun-dev
Copy link

ikotun-dev commented Apr 26, 2024

This is my current dockerfile

FROM python:3.10.2-bullseye

RUN apt-get update -y && apt-get install -y wget xvfb unzip jq
RUN apt-get install -y libxss1 libappindicator1 libgconf-2-4 \
  fonts-liberation libasound2 libnspr4 libnss3 libx11-xcb1 libxtst6 lsb-release xdg-utils \
  libgbm1 libnss3 libatk-bridge2.0-0 libgtk-3-0 libx11-xcb1 libxcb-dri3-0

RUN curl -s https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json > /tmp/versions.json

RUN CHROME_URL=$(jq -r '.channels.Stable.downloads.chrome[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
  wget -q --continue -O /tmp/chrome-linux64.zip $CHROME_URL && \
  unzip /tmp/chrome-linux64.zip -d /opt/chrome

RUN chmod +x /opt/chrome/chrome-linux64/chrome


RUN CHROMEDRIVER_URL=$(jq -r '.channels.Stable.downloads.chromedriver[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
  wget -q --continue -O /tmp/chromedriver-linux64.zip $CHROMEDRIVER_URL && \
  unzip /tmp/chromedriver-linux64.zip -d /opt/chromedriver && \
  chmod +x /opt/chromedriver/chromedriver-linux64/chromedriver

ENV CHROMEDRIVER_DIR /opt/chromedriver
ENV PATH $CHROMEDRIVER_DIR:$PATH

RUN rm /tmp/chrome-linux64.zip /tmp/chromedriver-linux64.zip /tmp/versions.json

WORKDIR /app

COPY requirements.txt ./ 

RUN pip install --no-cache-dir -r requirements.txt

COPY . . 

WORKDIR /app/index/api 

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment