[Python] Selenium instantly crashes when running in Docker container

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm encountering an issue when trying to run a selenium script in a docker container, I've spent quite a while going back and fourth with several AI's and none could fix it, I'm attaching the message I've sent to Claude, any help would be much appreciated.

    Im quite a begginer with Docker & Linux so most of the docker file was AI generated, and this is the final version after a lot of AI debugging attempts.

    Hi Claude! im working on running an automated web bot that could take actions for me in some site, i want to containerize it with docker so i can run it on AWS Fargate.

    this is my python code for the selenium:

    from selenium import webdriver
    from selenium.webdriver.firefox.service import Service
    from selenium.webdriver.firefox.options import Options
    from selenium.webdriver.common.by import By


    # Docker path's
    profilePath = "/root/.mozilla/firefox/4jqf9xwi.default-release"
    firefoxPath = "/usr/bin/firefox"
    firefoxDriver = "/usr/local/bin/geckodriver"

    upvoteButtonPath = "/html/body/div[2]/div/div[2]/div[2]/main/div/ul/li/article/div[2]/div[2]/div[2]/div[1]/div/div[1]/button"

    options = Options()
    options.profile = profilePath
    options.binary_location = firefoxPath
    options.add_argument("--headless")
    options.add_argument("--disable-gpu") # Force software rendering
    options.add_argument("--no-sandbox") # Avoid sandboxing issues in Docker
    options.add_argument("--disable-dev-shm-usage") # Prevent crashes due to shared memory


    service = Service(firefoxDriver)
    driver = webdriver.Firefox(service=service, options=options)

    driver.get("https://yad2.co.il/my-ads")
    driver.implicitly_wait(5)

    upVoteButton = driver.find_element(By.XPATH, upvoteButtonPath)
    upVoteButton.click()

    input("press Enter to close")

    driver.quit()


    and here is my Dockerfile:

    # Use an official Python runtime as a base image
    FROM python:3.9-slim

    # Set up environment variables for non-interactive installs
    ENV DEBIAN_FRONTEND=noninteractive

    # Install necessary dependencies in a single RUN command to reduce layers
    RUN apt-get update && apt-get install -y \
    wget \
    curl \
    unzip \
    ca-certificates \
    libx11-dev \
    libxcomposite-dev \
    libxrandr-dev \
    libgdk-pixbuf2.0-0 \
    libgtk-3-0 \
    libnss3 \
    libasound2 \
    fonts-liberation \
    libappindicator3-1 \
    libxss1 \
    libxtst6 \
    xdg-utils \
    firefox-esr \
    && apt-get clean && rm -rf /var/lib/apt/lists/* # Clean up apt cache to reduce size

    # Install GeckoDriver manually
    RUN GECKO_VERSION=v0.36.0 && \
    wget https://github.com/mozilla/geckodriver/releases/download/$GECKO_VERSION/geckodriver-$GECKO_VERSION-linux64.tar.gz && \
    tar -xvzf geckodriver-$GECKO_VERSION-linux64.tar.gz && \
    mv geckodriver /usr/local/bin/ && \
    rm geckodriver-$GECKO_VERSION-linux64.tar.gz

    RUN apt-get update && apt-get install -y \
    libgtk-3-0 \
    libx11-xcb1 \
    libdbus-glib-1-2 \
    libxt6 \
    libpci3 \
    xvfb


    # Install Python dependencies
    RUN pip install --no-cache-dir selenium

    # Copy Firefox profile into the container
    COPY 4jqf9xwi.default-release /root/.mozilla/firefox/4jqf9xwi.default-release/

    # Set up the working directory
    WORKDIR /app

    # Copy the Selenium script to the container
    COPY script.py /app/

    # Default command to run the script
    CMD ["python", "script.py"]```


    unfortunately when running the container it immediately crashes with this error, and no matter what i do i cant get it fixed

    2025-03-04 11:34:22 Traceback (most recent call last):
    2025-03-04 11:34:22 File "/app/script.py", line 29, in
    2025-03-04 11:34:22 driver = webdriver.Firefox(service=service, options=options)
    2025-03-04 11:34:22 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 71, in __init__
    2025-03-04 11:34:22 super().__init__(command_executor=executor, options=options)
    2025-03-04 11:34:22 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 250, in __init__
    2025-03-04 11:34:22 self.start_session(capabilities)
    2025-03-04 11:34:22 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 342, in start_session
    2025-03-04 11:34:22 response = self.execute(Command.NEW_SESSION, caps)["value"]
    2025-03-04 11:34:22 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
    2025-03-04 11:34:22 self.error_handler.check_response(response)
    2025-03-04 11:34:22 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
    2025-03-04 11:34:22 raise exception_class(message, screen, stacktrace)
    2025-03-04 11:34:22 selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 0
    2025-03-04 11:34:22


    do you have any insights on what could be the problem?

    Continue reading...

Compartilhe esta Página