【问题标题】:Running a Selenium(Python) based app in Docker在 Docker 中运行基于 Selenium(Python) 的应用程序
【发布时间】:2022-08-11 01:35:20
【问题描述】:

我正在尝试 dockerize 并运行使用 python 中的 selenium 库开发的网络抓取工具。我使用 Windows 10 进行开发。它在那里运行良好。在运行与 docker 映像相同的脚本时,我遇到了多个问题。这就是我在 Windows 中连接驱动程序的方式。

    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

我没有使用选项,因为我没有任何用例。由于在 docker 中运行时出现 root 用户错误,我添加了该选项并运行如下代码。

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(\'--no-sandbox\')
driver = webdriver.Chrome(options = chrome_options, service=Service(ChromeDriverManager().install()))

尽管如此,它并没有开始。所以我通过硬编码驱动程序路径来配置它。

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(\'--no-sandbox\')
driver = webdriver.Chrome(executable_path=driverPath,options=option)

即使那样它也没有开始,因为没有配置显示。所以配置了无头参数并运行,但最后,我得到了以下错误。

**

Tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量

**

所以我尝试通过下面的代码开始显示。

if platform.system() == \'Linux\':
        from pyvirtualdisplay import Display
        display = Display(visible=0, size=(800, 800))  
        display.start()

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(\'--no-sandbox\')
driver = webdriver.Chrome(executable_path=driverPath,options=option)

但它没有运行,它被冻结并且没有创建驱动程序会话。

这是我的 Dockerfile

FROM python
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \\ 
    && echo \"deb http://dl.google.com/linux/chrome/deb/ stable main\" >> /etc/apt/sources.list.d/google.list
RUN apt-get update && apt-get -y install google-chrome-stable
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
RUN apt-get install xvfb mesa-utils -y \\
        && apt install freeglut3-dev -y
ENV DISPLAY=:99
RUN mkdir -p /app/drivers
ADD requirements.txt /app
ADD sample.py /app
COPY run.sh /app
COPY drivers /app/drivers
COPY csv /app/csv
WORKDIR /app
RUN pip3 install -r requirements.txt
CMD ./run.sh

运行.sh

#!/bin/sh

#Xvfb :99 -screen 0 640x480x8 -nolisten tcp &
python3 ./sample.py 

要求.txt

selenium==4.3.0
webdriver-manager==3.8.2
chromedriver-py==103.0.5060.53
pyvirtualdisplay==3.0

我在代码中犯了什么错误?以及如何在 docker 中运行 selenium python 应用程序并显示?谢谢你。

  • 您是否看到任何错误?
  • 我没有看到任何错误。它被冻结了。在检查 bash 时,我看到 chrome 出现崩溃错误。
  • 通常,当像这样运行 selenium 时,您希望使用headless mode,而不是尝试模拟虚拟显示器。无头模式设置起来更快、更容易。你能试试吗?
  • 是的,我试过了,但我需要为某些目的显示。在无头模式下,我收到了这个错误。 Tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量

标签: python docker selenium


【解决方案1】:

似乎无法在 python jar 中启用显示。所以我已经从 ubuntu 图像创建了 python 图像,如site 中所述。我在那里安装了我的应用程序所需的 python 和其他依赖项。现在我可以毫无问题地运行该应用程序。

FROM ubuntu
#Enabling noninteractive environment and setting Timezone to install python3-tk without any interruption
# python
RUN export TZ=Asia/Kolkata
RUN apt-get update
RUN apt-get install -y python3 python3-setuptools python3-pip python3-tk

ENV DEBIAN_FRONTEND noninteractive 
# Essential tools and xvfb
RUN apt-get update && apt-get install -y \
    software-properties-common \
    unzip \
    curl \
    xvfb 
 
# Chrome browser to run the tests
RUN curl https://dl-ssl.google.com/linux/linux_signing_key.pub -o /tmp/google.pub \
    && cat /tmp/google.pub | apt-key add -; rm /tmp/google.pub \
    && echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google.list \
    && mkdir -p /usr/share/desktop-directories \
    && apt-get -y update && apt-get install -y google-chrome-stable
# Disable the SUID sandbox so that chrome can launch without being in a privileged container
RUN dpkg-divert --add --rename --divert /opt/google/chrome/google-chrome.real /opt/google/chrome/google-chrome \
    && echo "#!/bin/bash\nexec /opt/google/chrome/google-chrome.real --no-sandbox --disable-setuid-sandbox \"\$@\"" > /opt/google/chrome/google-chrome \
    && chmod 755 /opt/google/chrome/google-chrome
 
# Chrome Driver
RUN mkdir -p /opt/selenium \
    && curl http://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip -o /opt/selenium/chromedriver_linux64.zip \
    && cd /opt/selenium; unzip /opt/selenium/chromedriver_linux64.zip; rm -rf chromedriver_linux64.zip; ln -fs /opt/selenium/chromedriver /usr/local/bin/chromedriver;

# display
RUN export DISPLAY=:20
RUN Xvfb :20 -screen 0 1366x768x16 &
RUN mkdir -p /app
ADD requirements.txt /app
ADD app.py /app
WORKDIR /app
RUN pip3 install -r requirements.txt
CMD ./run.sh

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 2017-05-03
    • 2021-06-21
    • 1970-01-01
    • 2019-08-07
    • 2019-06-18
    • 1970-01-01
    • 2018-09-16
    相关资源
    最近更新 更多