Führen Sie Selenium in Docker-Compose aus: Sitzung nicht erstellt: DevToolsActivePort-Datei existiert nicht
Posted: 14 Jan 2025, 11:23
Ich möchte Selenium im Docker-Compose auf meinem Ubuntu-Server ausführen.
Ich habe Dockerfile
und Code zum Ausführen von Webdriver
Wenn ich die App starte, erhalte ich eine Fehlermeldung
Auch wenn ich diesen Code auf meinem lokalen MacOS (M1) ausführe, funktioniert er einwandfrei.
Upd: Es funktioniert, wenn ich den erstellten Benutzer entferne. .. Warum? Und wie kann man das Problem beheben und mit Nicht-Root-Benutzern arbeiten?
Ich habe Dockerfile
Code: Select all
FROM python:3.13.1-slim-bookworm
ENV POETRY_VERSION=2.0.1
ENV PYTHONUNBUFFERED=1
ENV POETRY_VIRTUALENVS_CREATE=false
ENV USER=ma
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN groupadd -r $USER && useradd -r -g $USER -d /opt/$USER $USER
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
wget \
unzip \
gnupg \
libglib2.0-0 \
libnss3 \
libgconf-2-4 \
libfontconfig1 \
libxcb1 \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' \
&& apt-get -y update \
&& apt-get install -y google-chrome-stable \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install poetry
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir poetry==${POETRY_VERSION}
WORKDIR /opt/$USER
RUN mkdir -p /opt/$USER/.cache/selenium && chown -R $USER:$USER /opt/$USER/.cache
COPY ./poetry.lock ./pyproject.toml ./
RUN poetry install --no-interaction --no-cache --no-root
COPY ignore_list.txt ./
COPY app app
USER $USER
ENTRYPOINT ["poetry", "run", "python", "-m", "app.get_next_predictions"]
Code: Select all
docker-compose.yaml
Code: Select all
services:
app:
image: portal
container_name: portal
build:
context: .
dockerfile: deployments/Dockerfile
env_file:
- .env
Code: Select all
def start_browser(portal_config: PortalSettings) -> webdriver.Chrome:
options = Options()
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("detach", True)
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-extensions") # Disable browser extensions
options.add_argument("--disable-crash-reporter") # Disable crash reporting
options.add_argument("--disable-infobars") # Disable infobars
options.add_argument("--disable-logging")
options.add_argument("--disable-background-timer-throttling")
options.add_argument("--disable-backgrounding-occluded-windows")
options.add_argument("--disable-renderer-backgrounding")
options.add_argument("--no-sandbox")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--start-maximized")
options.add_argument('log-level=3')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(options=options)
browser.maximize_window()
browser.get('https://www.portal.com/login')
login(browser, portal_config)
return browser
Code: Select all
portal | Skipping virtualenv creation, as specified in config file.
portal | Traceback (most recent call last):
portal | File "", line 198, in _run_module_as_main
portal | File "", line 88, in _run_code
portal | File "/opt/portal/app/get_next_predictions.py", line 267, in
portal | asyncio.run(starter())
portal | ~~~~~~~~~~~^^^^^^^^^^^
portal | File "/usr/local/lib/python3.13/asyncio/runners.py", line 194, in run
portal | return runner.run(main)
portal | ~~~~~~~~~~^^^^^^
portal | File "/usr/local/lib/python3.13/asyncio/runners.py", line 118, in run
portal | return self._loop.run_until_complete(task)
portal | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
portal | File "/usr/local/lib/python3.13/asyncio/base_events.py", line 720, in run_until_complete
portal | return future.result()
portal | ~~~~~~~~~~~~~^^
portal | File "/opt/portal/app/get_next_predictions.py", line 252, in starter
portal | browser = start_browser(portal_config)
portal | File "/opt/portal/app/get_next_predictions.py", line 234, in start_browser
portal | browser = webdriver.Chrome(options=options)
portal | File "/usr/local/lib/python3.13/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
portal | super().__init__(
portal | ~~~~~~~~~~~~~~~~^
portal | browser_name=DesiredCapabilities.CHROME["browserName"],
portal | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
portal | ......
portal | keep_alive=keep_alive,
portal | ^^^^^^^^^^^^^^^^^^^^^^
portal | )
portal | ^
portal | File "/usr/local/lib/python3.13/site-packages/selenium/webdriver/chromium/webdriver.py", line 66, in __init__
portal | super().__init__(command_executor=executor, options=options)
portal | ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
portal | File "/usr/local/lib/python3.13/site-packages/selenium/webdriver/remote/webdriver.py", line 241, in __init__
portal | self.start_session(capabilities)
portal | ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
portal | File "/usr/local/lib/python3.13/site-packages/selenium/webdriver/remote/webdriver.py", line 329, in start_session
portal | response = self.execute(Command.NEW_SESSION, caps)["value"]
portal | ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
portal | File "/usr/local/lib/python3.13/site-packages/selenium/webdriver/remote/webdriver.py", line 384, in execute
portal | self.error_handler.check_response(response)
portal | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
portal | File "/usr/local/lib/python3.13/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
portal | raise exception_class(message, screen, stacktrace)
portal | selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally.
portal | (session not created: DevToolsActivePort file doesn't exist)
portal | (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
portal | Stacktrace:
portal | #0 0x6190ff7701fa
portal | #1 0x6190ff280810
portal | #2 0x6190ff2b7ed8
portal | #3 0x6190ff2b3a36
portal | #4 0x6190ff2ff816
portal | #5 0x6190ff2fee66
portal | #6 0x6190ff2f3323
portal | #7 0x6190ff2c1de0
portal | #8 0x6190ff2c2dbe
portal | #9 0x6190ff73c12b
portal | #10 0x6190ff7400c7
portal | #11 0x6190ff7296cc
portal | #12 0x6190ff740c47
portal | #13 0x6190ff70e67f
portal | #14 0x6190ff75f288
portal | #15 0x6190ff75f450
portal | #16 0x6190ff76f076
portal | #17 0x77ca011d31c4
Upd: Es funktioniert, wenn ich den erstellten Benutzer entferne. .. Warum? Und wie kann man das Problem beheben und mit Nicht-Root-Benutzern arbeiten?