简体   繁体   English

尝试在 docker 镜像上安装 selenium python3 包

[英]Trying to install selenium python3 package on docker image

I've tried unsuccessfully to re-build a docker image with selenium python package on it.我尝试重新构建一个带有 selenium python 包的 docker 镜像,但没有成功。 I'm not sure how to proceed.我不知道如何继续。

Here's my dockerfile:这是我的 dockerfile:

FROM selenium/standalone-chrome:latest
WORKDIR /usr/local/bin
RUN sudo apt-get update
RUN sudo apt-get install software-properties-common
RUN sudo apt-add-repository universe
RUN sudo apt-get install python-pip
RUN pip3 install -r requirements.txt
RUN pip3 install .
RUN pip3 install -e .
COPY ./* ./

I'm fairly sure it's my dockerfile that's wrong, but after trying various methods of installing pip I'm drawing a blank.我很确定这是我的 dockerfile 出错了,但是在尝试了各种安装 pip 的方法后,我画了一个空白。

In any case I'm then calling: docker build -t webdriver .在任何情况下,我都会调用: docker build -t webdriver 。

Which outputs the following:输出以下内容:

Sending build context to Docker daemon  4.608kB
Step 1/10 : FROM selenium/standalone-chrome:latest
 ---> 11258d1f9aba
Step 2/10 : WORKDIR /usr/local/bin
 ---> Using cache
 ---> 9297517083f6
Step 3/10 : RUN sudo apt-get update
 ---> Using cache
 ---> f9004a85fc1a
Step 4/10 : RUN sudo apt-get install software-properties-common
 ---> Running in bb2336c4ae46
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  apt-utils cron gir1.2-glib-2.0 iso-codes libapt-inst2.0 libcurl3-gnutls
  libgirepository-1.0-1 librtmp1 python-apt-common python3-apt python3-dbus
  python3-gi python3-pycurl python3-software-properties unattended-upgrades
  xz-utils
Suggested packages:
  anacron logrotate checksecurity exim4 | postfix | mail-transport-agent
  isoquery python3-apt-dbg python-apt-doc python-dbus-doc python3-dbus-dbg
  libcurl4-gnutls-dev python-pycurl-doc python3-pycurl-dbg bsd-mailx
  mail-transport-agent
The following NEW packages will be installed:
  apt-utils cron gir1.2-glib-2.0 iso-codes libapt-inst2.0 libcurl3-gnutls
  libgirepository-1.0-1 librtmp1 python-apt-common python3-apt python3-dbus
  python3-gi python3-pycurl python3-software-properties
  software-properties-common unattended-upgrades xz-utils
0 upgraded, 17 newly installed, 0 to remove and 5 not upgraded.
Need to get 3615 kB of archives.
After this operation, 22.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c sudo apt-get install software-properties-common' returned a non-zero code: 1

I'm guessing that "Do you want to continue? [Y/n] Abort."我猜“你想继续吗?[是/否]中止。” needs to be answered "Y", but I don't know how to do this.需要回答“Y”,但我不知道该怎么做。 I suspect I'm missing more though.我怀疑我错过了更多。

Needless to say my python script still doesn't run as it needs selenium:不用说,我的 python 脚本仍然无法运行,因为它需要 selenium:

Traceback (most recent call last):
  File "web-test.py", line 3, in <module>
    from selenium import webdriver

Can anyone tell me where I've gone wrong?谁能告诉我我哪里出错了?

You have identified the error correctly.您已正确识别错误。 You need to answer the question: Do you want to continue? [Y/n]你需要回答这个问题: Do you want to continue? [Y/n] Do you want to continue? [Y/n] with Y and continue. Do you want to continue? [Y/n]Y并继续。 So, change your docker file as follows and add the flag -y因此,按如下方式更改您的 docker 文件并添加标志-y

FROM selenium/standalone-chrome:latest
WORKDIR /usr/local/bin
RUN sudo apt-get -y update
RUN sudo apt-get install -y software-properties-common
RUN sudo apt-add-repository -y universe
RUN sudo apt-get install -y python3-pip
RUN pip3 install -r requirements.txt
RUN pip3 install .
RUN pip3 install -e .
COPY ./* ./

The -y flag can be used to assume YES for all apt installs. -y 标志可用于为所有 apt 安装假定为YES You need to add the -y flag, because docker build command cannot be run in an interactive mode.您需要添加-y标志,因为docker build命令无法在交互模式下运行。 Hence, all interactions in the build process have to be added in the Dockerfile .因此,构建过程中的所有交互都必须添加到Dockerfile ( Read this answer ) 阅读这个答案

Since you are gonna use pip3 you need to install it correctly.由于您要使用pip3您需要正确安装它。 You are trying to install pip2 .您正在尝试安装pip2 I have updated the your docker file accordingly.我已经相应地更新了您的 docker 文件。 You need to use the below command to install pip3.您需要使用以下命令来安装 pip3。

RUN sudo apt-get install -y python3-pip

There are couple of things in your Dockerfile:您的 Dockerfile 中有几件事:

  • Avoid installing or using sudo as it has unpredictable TTY and signal-forwarding behavior that can cause problems.避免安装或使用sudo,因为它具有不可预测的 TTY 和信号转发行为,可能会导致问题。 You can refer to Dockerfile Best Practice可以参考Dockerfile最佳实践
RUN sudo apt-get update
RUN sudo apt-get install software-properties-common
RUN sudo apt-add-repository universe
RUN sudo apt-get install python-pip
  • The message消息
Need to get 3615 kB of archives.
After this operation, 22.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c sudo apt-get install software-properties-common' returned a non-zero code: 1

You need to add the flag -y to your Docker command above, so that during the Docker building, it can automatically say Yes for you.您需要在上面的 Docker 命令中添加标志-y ,以便在 Docker 构建期间,它可以自动为您说Yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM