Skip to content

Instantly share code, notes, and snippets.

@varyonic
Created June 10, 2016 14:14
  • Star 76 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
@JoaRiski
Copy link

I adapted this to fetch the latest version of chrome driver appropriate for the chrome installation, rather than having a fixed value. Here's the snippet I use for installing:

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROMEVER=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    DRIVERVER=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROMEVER") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$DRIVERVER/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /chromedriver

@Benji4
Copy link

Benji4 commented Feb 25, 2020

The mkdir command needs option -p:
RUN mkdir -p $CHROMEDRIVER_DIR

The Chromedriver must be made executable:
RUN chmod +x $CHROMEDRIVER_DIR/chromedriver

Chromedriver might need these packages:
RUN apt-get install libxi6 libgconf-2-4 -y

Also possibly, chrome might need to be run with option --no-sandbox, but not sure; might be a security issue.

Thank you for the Dockerfile! Really made my day!

@jagruthim
Copy link

jagruthim commented Mar 4, 2020

I am using below docker and I face the issue driver not found and 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 /usr/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
How can this issue be resolved?

FROM python:3.8-buster RUN apt-get update && apt-get install -yq \ chromium \ git-core \ xvfb=2:1.20.4-1 \ xsel=1.2.0+git9bfc13d.20180109-1 \ unzip=6.0-23+deb10u1 \ python-pytest=3.10.1-2 \ libgconf-2-4=3.2.6-5 \ libncurses5=6.1+20181013-2+deb10u2 \ libxml2-dev \ libxslt-dev \ libz-dev \ xclip=0.13-1

@bruno-ds
Copy link

FYI, Selenium provide a way similar Dockerfile :
https://github.com/SeleniumHQ/docker-selenium/blob/master/NodeChrome/Dockerfile

Personnaly, I'm still trying to understand how to interract with the container once built (an example would be great!)

@shogester
Copy link

@JoaRiski that is brilliant. Was looking for a way line up stable_dev of chrome with chromedriver but for some reason they don't offer anything of the sort. Thanks for adding this!

@Vasavi-MN
Copy link

Could anyone help to modify docker file for chrome and chrome driver install on windows 2016 server

@avinashpatel57
Copy link

I adapted this to fetch the latest version of chrome driver appropriate for the chrome installation, rather than having a fixed value. Here's the snippet I use for installing:

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROMEVER=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    DRIVERVER=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROMEVER") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$DRIVERVER/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /chromedriver

Thanks @JoaRiski

@ppetruneac
Copy link

I adapted this to fetch the latest version of chrome driver appropriate for the chrome installation, rather than having a fixed value. Here's the snippet I use for installing:

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROMEVER=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    DRIVERVER=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROMEVER") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$DRIVERVER/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /chromedriver

Thanks @JoaRiski

@AmoghKatwe
Copy link

AmoghKatwe commented Apr 21, 2021

I still get an error. Can someone help me with it? @JoaRiski

This is my dockerfile,
`FROM python:3.6

RUN pip install --upgrade pip && pip install pytest && pip install pytest-mock && pip install pytest-smtp && pip install mock
pip install schedule && pip install selenium && pip install Selenium-Screenshot && pip install python-dateutil

COPY src/screenshotcode.py /

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
RUN apt-get install -y google-chrome-stable
RUN apt-get install libxi6 libgconf-2-4 -y

ENV CHROMEDRIVER_VERSION 2.19
ENV CHROMEDRIVER_DIR /chromedriver
RUN mkdir -p $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

CMD [ "python", "screenshotcode.py" ]`

This is my python code,

import time
from Screenshot import Screenshot_Clipping
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from email_it import email_it
from environmental_variables import environmental_variables
from error_alert_email import error_alert_email
from selenium import webdriver

def screenshot():
    ob=Screenshot_Clipping.Screenshot()
    
    chrome_options = Options()
    
    chrome_options.add_argument('--start-maximized')    
    chrome_options.add_argument('--start-fullscreen')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    
    driver = webdriver.Chrome(executable_path = '$CHROMEDRIVER_DIR:$PATH')

    print('taking screenshot...')
    img_url=ob.full_Screenshot(driver, path = path, image_name = label)

    print('closing driver...')    
    driver.close()

screenshot()

@JoaRiski
Copy link

Did you try it with the example provided in a single RUN block instead of being spread across multiple?

@rootlinux2
Copy link

Hi guys, I'm trying to use the single RUN but I'm facing this error:

`debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
Configuring tzdata

Please select the geographic area in which you live. Subsequent configuration
questions will narrow this down by presenting a list of cities, representing
the time zones in which they are located.

  1. Africa 6. Asia 11. System V timezones
  2. America 7. Atlantic Ocean 12. US
  3. Antarctica 8. Europe 13. None of the above
  4. Australia 9. Indian Ocean
  5. Arctic Ocean 10. Pacific Ocean
    Geographic area: 12
    `
    After selecting the Area the build process is frozen
    I'm using the UBUNTU 20.04 image

FROM public.ecr.aws/ubuntu/ubuntu:20.04

@FL1NT233
Copy link

FL1NT233 commented Jul 28, 2021

@rootlinux I literally have not used github public in so long but I was running into the same issue trying to install chrome in my Dockerfile. Below is what fixed it for me.

# Set up timezone for google-chrom-stable question
ENV TZ=US
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Hopefully this helps!

@jquinngithub
Copy link

@JoaRiski that is brilliant. Was looking for a way line up stable_dev of chrome with chromedriver but for some reason they don't offer anything of the sort. Thanks for adding this!

+1 - thank you!

Copy link

ghost commented Sep 15, 2021

pull access denied for myapp, repository does not exist or may require 'docker login'
I got that error , what should i do ?

Were you able to fix this issue?
I am also getting same error

@johannstark
Copy link

I adapted this to fetch the latest version of chrome driver appropriate for the chrome installation, rather than having a fixed value. Here's the snippet I use for installing:

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROMEVER=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    DRIVERVER=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROMEVER") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$DRIVERVER/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /chromedriver

This works perfectly !!! 🚀
Thank you so much @JoaRiski

@robertkhaldar
Copy link

I need this code for GCP as app engine that automatically run the chrome driver as Docker . I put this code but still has error. what should I put the path for chrome driver?

@ian-maurer
Copy link

@robertkhaldar I am having the same issue, I am able to run the dockerfile and I reference the path (/usr/local/bin/chromedriver) in webdriver.ChromeOptions(). I still receive a "Error: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home" when executing in my function app.

@danilsmirnov
Copy link

danilsmirnov commented Nov 28, 2021

uname -a local 21.1.0 Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:24 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T8101 arm64
my app FROM python:3.9
all code similar, but i get executor failed running [/bin/sh -c apt-get install -y wget xvfb unzip]: exit code: 100 cause of missing apt-get update before
ok, thats fine and easy fixable, but i get Unable to locate package google-chrome-stable


upd
fixed that using --platform linux/x86_64

@petrisorionel
Copy link

this works in my case, FROM python:3.9-slim-bullseye with Debian 11 and Python 3.9.9 and I'm using https://github.com/mherrmann/selenium-python-helium

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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 \
      ${CHROME_VERSION:-google-chrome-stable} &&  \
    rm /etc/apt/sources.list.d/google-chrome.list &&  \
    rm -rf /var/lib/apt/lists/* /var/cache/apt/*

ARG CHROME_DRIVER_VERSION
RUN if [ -z "$CHROME_DRIVER_VERSION" ]; \
  then CHROME_MAJOR_VERSION=$(google-chrome --version | sed -E "s/.* ([0-9]+)(\.[0-9]+){3}.*/\1/") \
    && CHROME_DRIVER_VERSION=$(wget --no-verbose -O - "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_MAJOR_VERSION}"); \
  fi \
  && echo "Using chromedriver version: "$CHROME_DRIVER_VERSION \
  && wget --no-verbose -O /tmp/chromedriver_linux64.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
  && rm -rf /opt/selenium/chromedriver \
  && unzip /tmp/chromedriver_linux64.zip -d /opt/selenium \
  && rm /tmp/chromedriver_linux64.zip \
  && mv /opt/selenium/chromedriver /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION \
  && chmod 755 /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION \
  && ln -fs /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver

@lane-eb
Copy link

lane-eb commented Jan 25, 2022

Guys, this works for me.

FROM python:3.10.2-bullseye

RUN apt-get update -y
# 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 97.0.4692.71
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

@mnaumann-plenty
Copy link

To achieve the proper Google Chrome/chromedriver combination, you'll want to heed the instructions on this page: https://chromedriver.chromium.org/downloads/version-selection

Basically, you'll want to get the latest Chrome and install it, then get the product version from that Chrome installation (google-chrome -product-version), use that version information to build the URL to get the correct chromedriver version information from chromedriver.storage.googleapis.com, allowing you to build the specific download URL for the correct chromedriver version.

@bourbiaa
Copy link

bourbiaa commented Mar 15, 2022

@Amogh Katwe
did you get a solutionn ?!

@Anil86
Copy link

Anil86 commented May 3, 2022

Guys, this works for me.

FROM python:3.10.2-bullseye

RUN apt-get update -y
# 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 97.0.4692.71
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

@lane-eb Thank you

@mnaumann-plenty
Copy link

This ^^ approach will not work for the long term. If you were to install Google Chrome on most platforms today, your version would be 101.xxx.xxx.xxx -- a version 97 chromedriver won't work with today's Chrome without having to re-determine the chromedriver version and update the Dockerfile accordingly.

Currently, I do this:

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROME_VERSION=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    CHROMEDRIVER_VERSION=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_VERSION") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /usr/local/bin/

It has worked consistently for several months over several Chrome updates.

@Faq
Copy link

Faq commented Jun 7, 2022

This ^^ approach will not work for the long term. If you were to install Google Chrome on most platforms today, your version would be 101.xxx.xxx.xxx -- a version 97 chromedriver won't work with today's Chrome without having to re-determine the chromedriver version and update the Dockerfile accordingly.

Currently, I do this:

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROME_VERSION=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    CHROMEDRIVER_VERSION=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_VERSION") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /usr/local/bin/

It has worked consistently for several months over several Chrome updates.

Thank You @mnaumann-plenty, saved my day.

This is up to date version what worked for me with ruby container as it sets correct path with: /usr/local/bin/
Now in container chromedriver responds to: chromedriver -v
And updated it a bit https://gist.github.com/Faq/8821c5fd18dd01da4f80d7435158096d

Copy link

ghost commented Aug 31, 2022

No longer works, i get this error:

#6 85.98 -----END PGP PUBLIC KEY BLOCK-----
#6 85.99 gpg: keyring '/etc/apt/trusted.gpg.d/google.gpg' created
#6 85.99 gpg: no valid OpenPGP data found.
#6 85.99 gpg: Total number processed: 0

@Shk337
Copy link

Shk337 commented Sep 14, 2022

Nono, dude @KexoTV

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROME_VERSION=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    CHROMEDRIVER_VERSION=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_VERSION") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /usr/local/bin/

its work anyway,

Copy link

ghost commented Sep 16, 2022

this works ty

Nono, dude @KexoTV

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROME_VERSION=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    CHROMEDRIVER_VERSION=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_VERSION") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /usr/local/bin/

its work anyway,

@bjarneschroeder
Copy link

bjarneschroeder commented Sep 21, 2022

Nono, dude @KexoTV

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROME_VERSION=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    CHROMEDRIVER_VERSION=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_VERSION") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /usr/local/bin/

its work anyway,

No longer works for me:

 => [internal] load build definition from Dockerfile                                                                                                                                            0.0s
 => => transferring dockerfile: 1.79kB                                                                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                                                                               0.0s
 => => transferring context: 35B                                                                                                                                                                0.0s
 => [internal] load metadata for docker.io/library/python:3.10                                                                                                                                  1.2s
 => CACHED [ 1/11] FROM docker.io/library/python:3.10@sha256:e9c35537103a2801a30b15a77d4a56b35532c964489b125ec1ff24f3d5b53409                                                                   0.0s
 => => resolve docker.io/library/python:3.10@sha256:e9c35537103a2801a30b15a77d4a56b35532c964489b125ec1ff24f3d5b53409                                                                            0.0s
 => [internal] load build context                                                                                                                                                               0.0s
 => => transferring context: 1.17kB                                                                                                                                                             0.0s
 => ERROR [ 2/11] RUN apt-get update &&     apt-get install -y gnupg wget curl unzip --no-install-recommends &&     wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-k  4.7s
------                                                                                                                                                                                               
 > [ 2/11] RUN apt-get update &&     apt-get install -y gnupg wget curl unzip --no-install-recommends &&     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.list &&     apt-get update -y &&     apt-get install -y google-chrome-stable &&     CHROME_VERSION=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") &&     CHROMEDRIVER_VERSION=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_VERSION") &&     wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" &&     unzip /chromedriver/chromedriver* -d /usr/local/bin/:             
#5 0.269 Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]                                                                                                                              
#5 0.337 Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
#5 0.358 Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
#5 0.410 Get:4 http://deb.debian.org/debian bullseye/main arm64 Packages [8071 kB]
#5 1.744 Get:5 http://deb.debian.org/debian-security bullseye-security/main arm64 Packages [182 kB]
#5 1.769 Get:6 http://deb.debian.org/debian bullseye-updates/main arm64 Packages [2604 B]
#5 2.391 Fetched 8464 kB in 2s (3813 kB/s)
#5 2.391 Reading package lists...
#5 2.658 Reading package lists...
#5 2.919 Building dependency tree...
#5 2.991 Reading state information...
#5 3.074 curl is already the newest version (7.74.0-1.3+deb11u3).
#5 3.074 gnupg is already the newest version (2.2.27-2+deb11u2).
#5 3.074 unzip is already the newest version (6.0-26+deb11u1).
#5 3.074 wget is already the newest version (1.21-1+deb11u1).
#5 3.074 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
#5 3.113 Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
#5 3.685 OK
#5 3.759 Hit:1 http://deb.debian.org/debian bullseye InRelease
#5 3.777 Hit:2 http://deb.debian.org/debian-security bullseye-security InRelease
#5 3.804 Hit:3 http://deb.debian.org/debian bullseye-updates InRelease
#5 3.912 Get:4 http://dl.google.com/linux/chrome/deb stable InRelease [1811 B]
#5 4.029 Fetched 1811 B in 0s (5594 B/s)
#5 4.029 Reading package lists...
#5 4.289 Reading package lists...
#5 4.543 Building dependency tree...
#5 4.611 Reading state information...
#5 4.657 E: Unable to locate package google-chrome-stable

Update: I dug into the problem a little bit more and figured out that the command only does not work on my Docker instance running on my M1 Mac. I guess it has to do with this?
https://askubuntu.com/questions/1383789/install-chrome-on-ubuntu-debian-with-arm64

The command still works on docker instances on other devices. 👍

@mnaumann-plenty
Copy link

@bjarneschroeder :
That seems likely. You can likely locally amend the solution to be geared toward Chromium, specifically these two lines:

    apt-get install -y google-chrome-stable && \
    CHROME_VERSION=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \

...having the first of the two install Chromium and the second to use the Chromium executable to yield the product version. I haven't explored this locally but exchanging Chromium for Chrome ought to work.

@jmart1
Copy link

jmart1 commented Nov 14, 2022

I adapted this to fetch the latest version of chrome driver appropriate for the chrome installation, rather than having a fixed value. Here's the snippet I use for installing:

RUN apt-get update && \
    apt-get install -y gnupg wget curl unzip --no-install-recommends && \
    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.list && \
    apt-get update -y && \
    apt-get install -y google-chrome-stable && \
    CHROMEVER=$(google-chrome --product-version | grep -o "[^\.]*\.[^\.]*\.[^\.]*") && \
    DRIVERVER=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROMEVER") && \
    wget -q --continue -P /chromedriver "http://chromedriver.storage.googleapis.com/$DRIVERVER/chromedriver_linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /chromedriver

@JoaRiski Epic. Thank you for sharing.

@raine
Copy link

raine commented Nov 27, 2022

If you have M1 Mac, you have to build with docker build --platform linux/amd64 ....

@nekdan
Copy link

nekdan commented Feb 8, 2023

UPD: this is relevant only for chrome versions less than 115
You can get the latest ChromeDriver version number with the following command chromedriver.storage.googleapis.com/LATEST_RELEASE:

RUN DRIVER_VERSION=$(curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE) && \
wget https://chromedriver.storage.googleapis.com/$DRIVER_VERSION/chromedriver_linux64.zip && \
unzip chromedriver_linux64.zip -d /usr/bin/ && rm chromedriver_linux64.zip && chmod +x /usr/bin/chromedriver

@mnaumann-plenty
Copy link

mnaumann-plenty commented Feb 8, 2023

Yes, that's true. The point is that the Chrome browser and chromedriver need to be compatible. Simply grabbing the latest of both does not absolutely guarantee that.

https://chromedriver.chromium.org/downloads/version-selection

@Xstream007
Copy link

I need DockerFile configuration for Linux. APT-GET is not supported by Linux.

@vladdoster
Copy link

vladdoster commented Aug 19, 2023

I need DockerFile configuration for Linux. APT-GET is not supported by Linux.

@Xstream007,

apt-get is the command-line tool for handling packages, and may be considered the user's
"back-end" to other tools using the APT library. Several "front-end" interfaces exist,
such as aptitude, synaptic and wajig.

It is only available on Linux.

Use Ubuntu as the base docker image.

@SivaAdaveni91
Copy link

Hi Team,
How to add the latest "chrome for testing" browser and driver in the docker file.
Can you please provide the sample code?
Thanks.

@matrso
Copy link

matrso commented Aug 23, 2023

I’m trying to run a container that consists of a python script that uses Selenium. Because of that I also need chromium and chromedriver right?
I'm struggling with this one. On top of that I’m building it using an M1 mac. :/

Any help? None of the above solutions works for me

@mgtezak
Copy link

mgtezak commented Sep 17, 2023

I solved the compatibility problem by updating the link for the driver. I got the new link from docker hub.

Now this should definitely work:

RUN apt-get update && \
    apt-get install -y --no-install-recommends gnupg wget curl unzip && \
    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.list && \
    apt-get update -y && \
    apt-get install -y --no-install-recommends google-chrome-stable && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /var/cache/apt/* && \
    CHROME_VERSION=$(google-chrome --product-version) && \
    wget -q --continue -P /chromedriver "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$CHROME_VERSION/linux64/chromedriver-linux64.zip" && \
    unzip /chromedriver/chromedriver* -d /usr/local/bin/ && \
    rm -rf /chromedriver

However, it's still possible to get this error:

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.)

If this is the case it has nothing to do with the chrome installations but rather the instantiation of the driver inside your actual code. What you need to do is add some options to your webdriver instance. Here's how it's done in python:

options = selenium.webdriver.chrome.options.Options()
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')      ### optional
driver = selenium.webdriver.Chrome(options=options)

Although that last option is not strictly necessary, a lot of sources recommend it. ChatGPT has this to say:

What Does --disable-dev-shm-usage Do?

When you use --disable-dev-shm-usage, Chrome will not use /dev/shm but fall back to using /tmp for its shared memory. Since /tmp is disk-based in most setups, it's generally slower but more reliable when you're running in environments with limited shared memory.
So, in essence, the flag makes Chrome's behavior a bit more reliable at the expense of some performance, making it useful when running in a restricted environment like a Docker container.

I hope this was helpful

@kaspergrubbe
Copy link

kaspergrubbe commented Oct 31, 2023

The code from @mgtezak didn't work for me, but I tweaked it a little bit, I had to find the newest driver version for my version of Chrome like so:

CHROME_VERSION=$(google-chrome --product-version | cut -d '.' -f 1-3) && \
DRIVER_VERSION=$(curl -s "https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_$CHROME_VERSION") && \
wget -q --continue -P /chromedriver "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$DRIVER_VERSION/linux64/chromedriver-linux64.zip"

@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...

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