【问题标题】:Webdriver not working in Docker containerWebdriver 在 Docker 容器中不起作用
【发布时间】:2020-12-20 18:59:07
【问题描述】:

我正在尝试在 Google Cloudrun 上部署我的爬虫,但是我遇到了错误。

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to 

start: exited abnormally.
at check_response (/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py:242)
at execute (/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py:321)
at start_session (/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py:252)
at __init__ (/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py:157)
at __init__ (/usr/local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py:76)
at scrape_refresher (/app/refresher_news_crawler.py:32)
at run_crawlers (/app/main.py:30)
at dispatch_request (/usr/local/lib/python3.8/site-packages/flask/app.py:1936)
at full_dispatch_request (/usr/local/lib/python3.8/site-packages/flask/app.py:1950)
at reraise (/usr/local/lib/python3.8/site-packages/flask/_compat.py:39)
at handle_user_exception (/usr/local/lib/python3.8/site-packages/flask/app.py:1821)
at wrapped_function (/usr/local/lib/python3.8/site-packages/flask_cors/extension.py:165)
at full_dispatch_request (/usr/local/lib/python3.8/site-packages/flask/app.py:1952)
at wsgi_app (/usr/local/lib/python3.8/site-packages/flask/app.py:2447)

我假设这将是版本不匹配,但由于我的 docker 文件正在下载最新的稳定版本 85.0.4183.83,即使在匹配到 chromedriver-binary==85.0.4183.83.0 之后也是如此 错误不断发生,我也尝试了最新版本的 chromdriver-binary,它是85.0.4183.87.0,但结果是一样的。

Docker 文件:

# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.8-slim

# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y gconf-service wget libasound2 libgbm1 libappindicator3-1 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils

# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Install production dependencies.
RUN pip install Flask gunicorn
RUN pip install firebase_admin
RUN pip install flask_cors
RUN pip install selenium
RUN pip install firebase_admin
RUN pip install chromedriver-binary==85.0.4183.83.0
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app

在安装过程中没有问题,只有在运行过程中。非常感谢任何帮助。

Chrome 初始化:

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("window-size=1024,768")
    driver = webdriver.Chrome(chrome_options=chrome_options)

【问题讨论】:

  • 您是否以 root 身份运行驱动程序?如果是这样,请尝试使用 --no-sandbox 选项作为第一个参数。尽管由于存在漏洞,我建议不要以 root 身份运行它。
  • @FelipeEmerim using --no-sandbox 已解决,请发布答案,我会接受。

标签: python docker google-chrome selenium-chromedriver


【解决方案1】:

您是否以 root 身份运行驱动程序?如果是这样,请尝试使用 --no-sandbox 选项作为驱动程序的第一个参数。尽管由于存在漏洞,我建议不要以 root 身份运行它。

我从您的评论中看到这解决了问题,但如果安全性对您的情况非常重要,我建议您创建一个非 root 用户来运行您的服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2018-09-01
    相关资源
    最近更新 更多