-
-
Save varyonic/dea40abcf3dd891d204ef235c6e8dd79 to your computer and use it in GitHub Desktop.
# 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 |
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!
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()
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.
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
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.debotherwise 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:
Anyone knows where is the problem?
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.
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.
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.
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...
Does anyone have a working code for M1 chip?
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"]
If you somehow have docker running directly on the M1 mac, you may run into compatibility issues with the other software the dockerfile is trying to use/obtain, even if you change all the chrome/chromedriver references to use mac-arm64. I didn't attempt it myself, and went with a VM.
If you are running through a VM, your dockerfile should match the architecture of the VM. I had trouble trying to use an arm64 linux VM and changing the dockerfile to match (there is not a "non mac" arm64 chrome/chromedriver), but did have success with an x86 VM on the M1 by installing lima. The dockerfile for linux64 mentioned here worked fine for that.
THIS WORKED FOR ME, THANKS SO MUCH! @ankitarya1019
This is my current Docker file:
FROM python:3.11-slim
RUN apt-get update -y && apt-get install -y wget xvfb curl 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 . /app
RUN pip install --default-timeout=200 -r requirements.txt
RUN python -m spacy download en_core_web_sm
CMD uvicorn main:app --port=8080 --host=0.0.0.0
but this gives error:
start chrome An error occurred: Message: session not created: cannot connect to chrome at 127.0.0.1:37177 from session not created: This version of ChromeDriver only supports Chrome version 127 Current browser version is 126.0.6478.61 Stacktrace: #0 0x5bf739dfb6ba <unknown> #1 0x5bf739acb730 <unknown> #2 0x5bf739b0a2bc <unknown> #3 0x5bf739b09242 <unknown> #4 0x5bf739afeccc <unknown> #5 0x5bf739b49e88 <unknown> #6 0x5bf739b3d7f3 <unknown> #7 0x5bf739b0dec9 <unknown> #8 0x5bf739b0e91e <unknown> #9 0x5bf739dc19eb <unknown> #10 0x5bf739dc5972 <unknown> #11 0x5bf739daee15 <unknown> #12 0x5bf739dc6502 <unknown> #13 0x5bf739d93d2f <unknown> #14 0x5bf739dea578 <unknown> #15 0x5bf739dea750 <unknown> #16 0x5bf739dfa48c <unknown> #17 0x75e95f494ac3 <unknown>
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()
This is really good work, however update
Set up Chromedriver Environment variables
ENV CHROMEDRIVER_DIR /opt/chromedriver
ENV PATH $CHROMEDRIVER_DIR:$PATH
to:
Set up Chromedriver Environment variables
ENV CHROMEDRIVER_DIR=/opt/chromedriver
ENV PATH=$CHROMEDRIVER_DIR:$PATH
and it works.
Thank you so much @ankitarya1019
I get this error do you really need so many dependencies, i am using python:3.11.2-slim-bullseye as base:
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
---> Running in ef18a231d6fa
Reading package lists...
Building dependency tree...
Reading state information...
Package fonts-liberation is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
Package libasound2 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
Package xdg-utils is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Unable to locate package libxss1
E: Unable to locate package libappindicator1
E: Unable to locate package libgconf-2-4
E: Package 'fonts-liberation' has no installation candidate
E: Package 'libasound2' has no installation candidate
E: Unable to locate package libnspr4
E: Unable to locate package libnss3
E: Unable to locate package libxtst6
E: Unable to locate package lsb-release
E: Package 'xdg-utils' has no installation candidate
E: Unable to locate package libgbm1
E: Unable to locate package libnss3
E: Unable to locate package libatk-bridge2.0-0
E: Couldn't find any package by glob 'libatk-bridge2.0-0'
E: Couldn't find any package by regex 'libatk-bridge2.0-0'
E: Unable to locate package libgtk-3-0
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()This is really good work, however update
Set up Chromedriver Environment variables
ENV CHROMEDRIVER_DIR /opt/chromedriver
ENV PATH $CHROMEDRIVER_DIR:$PATHto:
Set up Chromedriver Environment variables
ENV CHROMEDRIVER_DIR=/opt/chromedriver
ENV PATH=$CHROMEDRIVER_DIR:$PATHand it works.
Thank you so much @ankitarya1019
For posterity: the Dockerfile in the comment above mine didn't work for me on a Mac M2 chip, so with the liberal help of ChatGPT I was able to find one that works by just apt-installing chromium and chromedriver:
# Debian-based Python image (multi-arch)
FROM python:3.10-slim-bullseye
ENV DEBIAN_FRONTEND=noninteractive
# Install Chromium, ChromeDriver, and any needed libs
RUN apt-get update && apt-get install -y \
chromium chromium-driver \
libgl1-mesa-glx \
libglib2.0-0 libnss3 libx11-6 libxcb1 libxcomposite1 libxcursor1 libxdamage1 \
libxext6 libxfixes3 libxi6 libxtst6 libatk1.0-0 libatk-bridge2.0-0 libcups2 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY . .
# python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# We expect the user to volume-mount the file into place and buffering can be a
# problem, so use unbuffered mode for reading the input.csv
ENV PYTHONUNBUFFERED=1
# NOTE: depends on a /tmp/input.csv existing
# ENV CHROMEDRIVER_PATH=$CHROMEDRIVER_DIR
ENTRYPOINT [ "python", "./calculate.py" ]
Thanks @mieubrisse.
If you need to run chrome in headless mode, this config is enough:
# Install Chromium, ChromeDriver, and minimal dependencies for headless mode
RUN apt-get update && apt-get install -y \
chromium chromium-driver \
&& rm -rf /var/lib/apt/lists/*
Also, I don't think you need to install all of that dependencies manually, they are downloaded and installed automatically:
Here's the log of my app deploy with the config above:
DEBUG [c77ebb45] #13 [base 4/4] RUN apt-get update && apt-get install -y chromium chromium-driver && rm -rf /var/lib/apt/lists/*
DEBUG [c77ebb45] #13 0.089 Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
DEBUG [c77ebb45] #13 0.098 Get:2 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
DEBUG [c77ebb45] #13 0.099 Get:3 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
DEBUG [c77ebb45] #13 0.155 Get:4 http://deb.debian.org/debian bookworm/main amd64 Packages [8792 kB]
DEBUG [c77ebb45] #13 0.227 Get:5 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [13.5 kB]
DEBUG [c77ebb45] #13 0.287 Get:6 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [246 kB]
DEBUG [c77ebb45] #13 0.975 Fetched 9306 kB in 1s (10.5 MB/s)
DEBUG [c77ebb45] #13 0.975 Reading package lists...
DEBUG [c77ebb45] #13 1.360 Reading package lists...
DEBUG [c77ebb45] #13 1.739 Building dependency tree...
DEBUG [c77ebb45] #13 1.813 Reading state information...
DEBUG [c77ebb45] #13 1.908 The following additional packages will be installed:
DEBUG [c77ebb45] #13 1.908 adwaita-icon-theme alsa-topology-conf alsa-ucm-conf at-spi2-common
DEBUG [c77ebb45] #13 1.908 at-spi2-core avahi-daemon avahi-utils bsdutils chromium-common
DEBUG [c77ebb45] #13 1.908 chromium-sandbox cpp cpp-12 cups-pk-helper dbus dbus-bin dbus-daemon
DEBUG [c77ebb45] #13 1.908 dbus-session-bus-common dbus-system-bus-common dbus-user-session
DEBUG [c77ebb45] #13 1.908 dconf-gsettings-backend dconf-service dmsetup fonts-liberation
DEBUG [c77ebb45] #13 1.908 gir1.2-atk-1.0 gir1.2-freedesktop gir1.2-gdkpixbuf-2.0 gir1.2-glib-2.0
DEBUG [c77ebb45] #13 1.908 gir1.2-gtk-3.0 gir1.2-handy-1 gir1.2-harfbuzz-0.0 gir1.2-notify-0.7
DEBUG [c77ebb45] #13 1.908 gir1.2-packagekitglib-1.0 gir1.2-pango-1.0 gir1.2-polkit-1.0 gir1.2-secret-1
DEBUG [c77ebb45] #13 1.908 gsettings-desktop-schemas gtk-update-icon-cache hicolor-icon-theme
DEBUG [c77ebb45] #13 1.908 libapparmor1 libargon2-1 libasound2 libasound2-data libasyncns0
DEBUG [c77ebb45] #13 1.908 libatk-bridge2.0-0 libatk1.0-0 libatspi2.0-0 libauthen-sasl-perl
DEBUG [c77ebb45] #13 1.908 libavahi-client3 libavahi-common-data libavahi-common3 libavahi-core7
DEBUG [c77ebb45] #13 1.908 libblkid1 libclone-perl libcolord2 libcryptsetup12 libcups2 libdaemon0
DEBUG [c77ebb45] #13 1.908 libdata-dump-perl libdbus-1-3 libdconf1 libdevmapper1.02.1
DEBUG [c77ebb45] #13 1.908 libdouble-conversion3 libdrm-amdgpu1 libdrm-common libdrm-intel1
DEBUG [c77ebb45] #13 1.908 libdrm-nouveau2 libdrm-radeon1 libdrm2 libduktape207 libedit2 libegl-mesa0
DEBUG [c77ebb45] #13 1.908 libegl1 libelf1 libencode-locale-perl libepoxy0 libfdisk1
DEBUG [c77ebb45] #13 1.908 libfile-basedir-perl libfile-desktopentry-perl libfile-listing-perl
DEBUG [c77ebb45] #13 1.908 libfile-mimeinfo-perl libflac12 libfont-afm-perl libfontenc1 libgbm1
DEBUG [c77ebb45] #13 1.908 libgdbm-compat4 libgdbm6 libgirepository-1.0-1 libgl1 libgl1-mesa-dri
DEBUG [c77ebb45] #13 1.908 libglapi-mesa libgles2 libglvnd0 libglx-mesa0 libglx0 libgtk-3-0
DEBUG [c77ebb45] #13 1.908 libgtk-3-bin libgtk-3-common libgudev-1.0-0 libhandy-1-0 libharfbuzz-subset0
DEBUG [c77ebb45] #13 1.908 libhtml-form-perl libhtml-format-perl libhtml-parser-perl
DEBUG [c77ebb45] #13 1.908 libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl
DEBUG [c77ebb45] #13 1.908 libhttp-daemon-perl libhttp-date-perl libhttp-message-perl
DEBUG [c77ebb45] #13 1.908 libhttp-negotiate-perl libice6 libio-html-perl libio-socket-ssl-perl
DEBUG [c77ebb45] #13 1.908 libio-stringy-perl libip4tc2 libipc-system-simple-perl libisl23 libjansson4
DEBUG [c77ebb45] #13 1.908 libjson-c5 libkmod2 libldb2 libllvm15 liblmdb0 liblwp-mediatypes-perl
DEBUG [c77ebb45] #13 1.908 liblwp-protocol-https-perl libmailtools-perl libminizip1 libmount1
DEBUG [c77ebb45] #13 1.908 libmp3lame0 libmpc3 libmpfr6 libmpg123-0 libnet-dbus-perl libnet-http-perl
DEBUG [c77ebb45] #13 1.908 libnet-smtp-ssl-perl libnet-ssleay-perl libnotify4 libnss-mdns
DEBUG [c77ebb45] #13 1.908 libnss-systemd libogg0 libopenh264-7 libopus0 libpackagekit-glib2-18
DEBUG [c77ebb45] #13 1.908 libpam-systemd libpangoxft-1.0-0 libpciaccess0 libperl5.36
DEBUG [c77ebb45] #13 1.908 libpolkit-agent-1-0 libpolkit-gobject-1-0 libpopt0 libpulse0
DEBUG [c77ebb45] #13 1.908 libpython3-stdlib libpython3.11-minimal libpython3.11-stdlib
DEBUG [c77ebb45] #13 1.909 libregexp-ipv6-perl librsvg2-common libsecret-1-0 libsecret-common
DEBUG [c77ebb45] #13 1.909 libsensors-config libsensors5 libsm6 libsmartcols1 libsmbclient libsndfile1
DEBUG [c77ebb45] #13 1.909 libsystemd-shared libsystemd0 libtalloc2 libtdb1 libtevent0
DEBUG [c77ebb45] #13 1.909 libtext-iconv-perl libtie-ixhash-perl libtimedate-perl libtry-tiny-perl
DEBUG [c77ebb45] #13 1.909 libudev1 libupower-glib3 liburi-perl libusb-1.0-0 libuuid1 libvorbis0a
DEBUG [c77ebb45] #13 1.909 libvorbisenc2 libwayland-client0 libwayland-cursor0 libwayland-egl1
DEBUG [c77ebb45] #13 1.909 libwayland-server0 libwbclient0 libwww-perl libwww-robotrules-perl
DEBUG [c77ebb45] #13 1.909 libx11-protocol-perl libx11-xcb1 libxaw7 libxcb-dri2-0 libxcb-dri3-0
DEBUG [c77ebb45] #13 1.909 libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-shape0 libxcb-sync1
DEBUG [c77ebb45] #13 1.909 libxcb-xfixes0 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxft2
DEBUG [c77ebb45] #13 1.909 libxi6 libxinerama1 libxkbcommon0 libxkbfile1 libxml-parser-perl
DEBUG [c77ebb45] #13 1.909 libxml-twig-perl libxml-xpathengine-perl libxmu6 libxmuu1 libxnvctrl0
DEBUG [c77ebb45] #13 1.909 libxpm4 libxrandr2 libxshmfence1 libxslt1.1 libxt6 libxtst6 libxv1
DEBUG [c77ebb45] #13 1.909 libxxf86dga1 libxxf86vm1 libz3-4 media-types mount netbase
DEBUG [c77ebb45] #13 1.909 notification-daemon perl perl-modules-5.36 perl-openssl-defaults polkitd
DEBUG [c77ebb45] #13 1.909 python3 python3-cairo python3-cups python3-cupshelpers python3-dbus
DEBUG [c77ebb45] #13 1.909 python3-gi python3-minimal python3-smbc python3.11 python3.11-minimal
DEBUG [c77ebb45] #13 1.909 samba-libs sgml-base system-config-printer system-config-printer-common
DEBUG [c77ebb45] #13 1.909 system-config-printer-udev systemd systemd-sysv systemd-timesyncd udev
DEBUG [c77ebb45] #13 1.909 upower util-linux util-linux-extra x11-common x11-utils x11-xserver-utils
DEBUG [c77ebb45] #13 1.909 xdg-utils xkb-data xml-core zutty
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.
Here is how I configured in Python selenium