ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.68k stars 1.14k forks source link

How can I run undetected-chromedrive in docker? #1358

Open 0xACE3 opened 1 year ago

0xACE3 commented 1 year ago

Code

  self.options = uc.ChromeOptions()
  self.options.arguments.extend(["--no-sandbox", "--disable-setuid-sandbox"])
  self.driver = uc.Chrome(self.options)

Dockerfile

# Dockerfile
FROM ultrafunk/undetected-chromedriver

# Set the working directory
WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt

# Update Chrome
RUN apt-get update -y && apt-get install -y google-chrome-stable && apt-get install -y xvfb fluxbox x11vnc x11-apps

# Update undetected-chromedriver
RUN pip install -U git+https://github.com/ultrafunkamsterdam/undetected-chromedriver

COPY . .

# Start the application
ENV DISPLAY=:0

# Set desired screen resolution
#ENV SCREEN_WIDTH=1920
#ENV SCREEN_HEIGHT=1080
#ENV SCREEN_DEPTH=24

# Start Xvfb and Fluxbox
CMD Xvfb -ac -screen 0 1920x1080x24 & fluxbox -screen 0 &>/dev/null & x11vnc -passwd ${VNC_PASSWORD:-password} -N -forever -rfbport 5900 &> /dev/null

docker build -t my_app . && docker run -it --rm -e VNC_PASSWORD=12346  --volume="/tmp/.X11-unix:/tmp/.X11-unix" --device=/dev/dri:/dev/dri --name app_container my_app python -m src.main 

error

running: python -m src.main
running keepUpScreen()
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/my_app/src/main.py", line 10, in <module>
    main()
  File "/my_app/src/main.py", line 6, in main
    purchaser.listen_for_offers()
  File "/my_app/src/main.py", line 118, in listen_for_offers
    self.driver.get(self.url)
  File "/usr/local/lib/python3.11/site-packages/undetected_chromedriver/__init__.py", line 615, in get
    return super().get(url)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 355, in get
    self.execute(Command.GET, {"url": url})
  File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash
from unknown error: cannot determine loading status
from tab crashed
  (Session info: chrome=114.0.5735.133)
Stacktrace:
#0 0x562699ac24e3 <unknown>
#1 0x5626997f1b00 <unknown>
#2 0x5626997dcb69 <unknown>
#3 0x5626997dc066 <unknown>
#4 0x5626997db168 <unknown>
#5 0x5626997dafa0 <unknown>
#6 0x5626997d99bf <unknown>
#7 0x5626997da162 <unknown>
#8 0x5626997e80d5 <unknown>
#9 0x5626997e9222 <unknown>
#10 0x5626997fbbcb <unknown>
#11 0x5626998003ab <unknown>
#12 0x5626997da703 <unknown>
#13 0x5626997fb810 <unknown>
#14 0x562699865523 <unknown>
#15 0x56269984cde3 <unknown>
#16 0x5626998222dd <unknown>
#17 0x56269982334e <unknown>
#18 0x562699a823e4 <unknown>
#19 0x562699a863d7 <unknown>
#20 0x562699a90b20 <unknown>
#21 0x562699a87023 <unknown>
#22 0x562699a551aa <unknown>
#23 0x562699aab6b8 <unknown>
#24 0x562699aab847 <unknown>
#25 0x562699abb243 <unknown>
#26 0x7f56250cbea7 start_thread

closing chrome...

How can I fix this and run the chrome driver in non-headless mode in docker?

Hudrolax commented 1 year ago

https://github.com/Hudrolax/uc-docker This is my working example of Dockered python script for using undetected chromedriver in GUI mode, based on Ubuntu image. https://github.com/Hudrolax/uc-docker-alpine And this is the same, but based on Alpine Linux and Chromium.

lmaddox commented 1 month ago

https://github.com/Hudrolax/uc-docker This is my working example of Dockered python script for using undetected chromedriver in GUI mode, based on Ubuntu image. https://github.com/Hudrolax/uc-docker-alpine And this is the same, but based on Alpine Linux and Chromium.

Thank you.

FROM python # common base image for most of my images
RUN pip install --no-cache-dir --upgrade pip
# need to install a full GUI, apparently
RUN apt update         \
&&  apt install -y     \
      chromium         \
      chromium-driver  \
      chromium-sandbox \
      xvfb             \
      x11vnc \
      fluxbox \
      xterm \
      libffi-dev \
      git \
      ca-certificates \
&& rm -rf /var/lib/apt/lists/*

# install your project
COPY    ./py/ /tmp/py
WORKDIR       /tmp/py
RUN pip install --no-cache-dir           .
RUN rm -rf    /tmp/py

# not sure if this is necessary
# Create a non-root user
RUN groupadd -r pwuser && useradd -r -g pwuser -G audio,video pwuser \
    && mkdir -p /home/pwuser/Downloads \
    && chown -R pwuser:pwuser /home/pwuser
# Switch to the non-root user
USER pwuser

# run your project
ENTRYPOINT ["python", "-c", "from iaexample import main; main()"]