Docker 文件运行 Seleniam 作为基础时出错

问题描述 投票:0回答:1

嗨,我正在尝试创建一个以 seleniarm 作为基础映像的 docker 文件(使用树莓派,所以需要多架构),并向其中添加 python。这是我的文件:

# Use the Selenium base image
FROM seleniarm/standalone-chromium

# Switch to the root user
USER root

# Install Python and pip
RUN apt-get update && \
    apt-get install -y python3 python3-pip

# Set up a working directory
WORKDIR /app

# Copy your Python code or scripts into the container
COPY . /app

# Install required Python packages using pip
RUN pip3 install selenium requests==2.26.0 beautifulsoup4==4.9.3

# Specify the command to run when the container starts
CMD ["python3", "myscript.py"]

每次尝试构建容器时,安装 python 包时都会出错。已尝试通过requirement.txt文件并按上述方式直接操作,但它不起作用。

这些是我需要的所有库,用于在我的俱乐部中创建预订的 python 脚本:

# Import all the required libraries
from datetime import datetime, timedelta, time
import time as tm
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains


# initiate Chrome driver and open website
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService

我做错了什么吗?

容器无法构建。

添加更多细节:

使用此命令

# Install required Python packages using pip
RUN pip3 install selenium requests==2.26.0 beautifulsoup4==4.9.3

我收到此错误

=> ERROR [5/5] RUN pip3 install selenium requests==2.26.0 beautifulsoup4==4.9.3                                                                                                                                   3.5s
------                                                                                                                                                                                                                  
 > [5/5] RUN pip3 install selenium requests==2.26.0 beautifulsoup4==4.9.3:                                                                                                                                              
1.572 WARNING: The directory '/home/seluser/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.                                                                                                                                                                 
1.588 error: externally-managed-environment                                                                                                                                                                             
1.588 
1.588 × This environment is externally managed
1.588 ╰─> To install Python packages system-wide, try apt install
1.588     python3-xyz, where xyz is the package you are trying to
1.588     install.
1.588     
1.588     If you wish to install a non-Debian-packaged Python package,
1.588     create a virtual environment using python3 -m venv path/to/venv.
1.588     Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
1.588     sure you have python3-full installed.
1.588     
1.588     If you wish to install a non-Debian packaged Python application,
1.588     it may be easiest to use pipx install xyz, which will manage a
1.588     virtual environment for you. Make sure you have pipx installed.
1.588     
1.588     See /usr/share/doc/python3.11/README.venv for more information.
1.588 
1.588 note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
1.588 hint: See PEP 668 for the detailed specification.
------
Dockerfile:18
--------------------
  16 |     
  17 |     # Install required Python packages using pip
  18 | >>> RUN pip3 install selenium requests==2.26.0 beautifulsoup4==4.9.3
  19 |     
  20 |     # Specify the command to run when the container starts
--------------------
ERROR: failed to solve: process "/bin/sh -c pip3 install selenium requests==2.26.0 beautifulsoup4==4.9.3" did not complete successfully: exit code: 1

以及以下requirements.txt文件

selenium
requests==2.26.0
beautifulsoup4==4.9.3

我得到:

=> ERROR [5/5] RUN pip3 install -r requirements.txt                                                                                                                                                               2.0s
------                                                                                                                                                                                                                  
 > [5/5] RUN pip3 install -r requirements.txt:                                                                                                                                                                          
1.618 WARNING: The directory '/home/seluser/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.                                                                                                                                                                 
1.635 error: externally-managed-environment                                                                                                                                                                             
1.635 
1.635 × This environment is externally managed
1.635 ╰─> To install Python packages system-wide, try apt install
1.635     python3-xyz, where xyz is the package you are trying to
1.635     install.
1.635     
1.635     If you wish to install a non-Debian-packaged Python package,
1.635     create a virtual environment using python3 -m venv path/to/venv.
1.635     Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
1.635     sure you have python3-full installed.
1.635     
1.635     If you wish to install a non-Debian packaged Python application,
1.635     it may be easiest to use pipx install xyz, which will manage a
1.635     virtual environment for you. Make sure you have pipx installed.
1.635     
1.635     See /usr/share/doc/python3.11/README.venv for more information.
1.635 
1.635 note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
1.635 hint: See PEP 668 for the detailed specification.
------
Dockerfile:21
--------------------
  19 |     
  20 |     # Install any required Python packages using pip
  21 | >>> RUN pip3 install -r requirements.txt
  22 |     
  23 |     # Specify the command to run when the container starts
--------------------
ERROR: failed to solve: process "/bin/sh -c pip3 install -r requirements.txt" did not complete successfully: exit code: 1
python docker raspberry-pi selenium-grid multiarch
1个回答
0
投票

我设法让这个示例与一些挖掘一起工作......我的用例是我想将一些 Selenium 代码移植到运行 Graviton ARM 节点实例的 EKS 集群,而我们在 x86 上工作的 Chrome/ChromeDriver 设置不会在那里工作。我发现你的帖子对我开始转向 Chromium 很有帮助,所以谢谢你:)

解决方案的关键是要小心用户在做什么并使用虚拟环境。 (希望当我引入更复杂的依赖项时,这个解决方案仍然适用于我)。

这是我的 Dockerfile:

# Use the ARM-compatible Selenium standalone Chromium image
FROM seleniarm/standalone-chromium:latest

# Switch to the root user to install packages
USER root

# Update package lists and install Python and pip
RUN apt-get update && \
    apt-get install -y python3 python3-pip python3-venv && \
    rm -rf /var/lib/apt/lists/*

# Switch back to the Docker Selenium's default non-root user. 1200 is a magic number. The default group is 1201
USER 1200
WORKDIR /home/seluser

# Create a virtual and and install selenium there
RUN /usr/bin/python3 -m venv .venv && . .venv/bin/activate && pip install selenium

# Set the virtual environment as the default Python
ENV PATH="/home/seluser/.venv/bin:$PATH"

# Copy your test scripts
COPY script.py .

# Run your tests
CMD ["python", "script.py"]

这是我测试过的 Python 示例

script.py
...

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# Set up Chrome options to use Chromium for headless execution
options = Options()
# Specify Chromium path
options.binary_location = "/usr/bin/chromium"
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--remote-debugging-port=9222")

# Specify ChromeDriver path and initialize the WebDriver
service = Service('/usr/bin/chromedriver')
driver = webdriver.Chrome(service=service, options=options)

# Example usage of Selenium
driver.get("https://www.example.com")
print(driver.title)

# Close the browser
driver.quit()

在我的 Mac 上,我使用

docker build -f Dockerfile.webdriverx -t selenium-arm .
构建了映像并使用
docker run selenium-arm
运行它。

© www.soinside.com 2019 - 2024. All rights reserved.