You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在DigitalOcean配置Dockerfile运行Chrome与Selenium遇报错求助

在DigitalOcean Docker容器中部署Python Selenium爬虫的问题与解决方案

初始Google Chrome配置问题

最初编写的Dockerfile尝试直接安装Google Chrome,但构建时失败:

# Use a lightweight Python base image
FROM python:3.10

WORKDIR /app

COPY . /app

RUN pip install --trusted-host pypi.python.org -r requirements.txt

# install google chrome
RUN apt-get update && \
    apt-get install -y wget gnupg && \
    wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-linux-signing-key.gpg && \
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-linux-signing-key.gpg] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list && \
    apt-get update && \
    apt-get install -y google-chrome-stable

# install chromedriver
RUN apt-get install -yqq unzip && \
    wget -O /tmp/chromedriver.zip "http://chromedriver.storage.googleapis.com/$(curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE)/chromedriver_linux64.zip" && \
    unzip /tmp/chromedriver.zip -d /usr/local/bin/

# set display port to avoid crash
ENV DISPLAY=:99

CMD ["python", "app/scraper.py"]

错误信息

E: Unable to locate package google-chrome-stable

已尝试的无效操作:

  • 更新GPG密钥并重新配置Chrome仓库URL
  • 安装Chrome前手动执行apt-get update
  • 参考第三方教程调整配置

改用Chromium后的新问题

切换到Chromium配置后,构建成功但运行爬虫时出现WebDriver启动失败:

# Use a lightweight Python base image
FROM python:3.10-slim

# Avoid interactive prompts during package installations
ENV DEBIAN_FRONTEND=noninteractive

# Install system dependencies required by Chrome and ChromeDriver
RUN apt-get update && apt-get install -y \
    chromium-driver \
    chromium \
    fonts-liberation \
    libnss3 \
    libx11-6 \
    libatk-bridge2.0-0 \
    libatspi2.0-0 \
    libgtk-3-0 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxrandr2 \
    libgbm-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Set environment variables for Chromium and ChromeDriver paths
ENV CHROME_BIN=/usr/bin/chromium \
    CHROME_DRIVER_BIN=/usr/bin/chromedriver

# Set the working directory in the container
WORKDIR /app

# Copy the requirements.txt file to the working directory
COPY requirements.txt .

# Install Python dependencies from the requirements file
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code to the working directory
COPY . .

# Expose the port your application will run on
EXPOSE 8080

错误信息

File "/workspace/app/scraper.py", line 21, in 
driver = webdriver.Chrome(options=chrome_options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
super().__init__(
File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/chromium/webdriver.py", line 55, in __init__
self.service.start()
File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 105, in start
self.assert_process_still_running()
File "/workspace/.heroku/python/lib/python3.12/site-packages/selenium/webdriver/common/service.py", line 118, in assert_process_still_running
raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service /workspace/.cache/selenium/chromedriver/linux64/130.0.6723.69/chromedriver unexpectedly exited. Status code was: 127

可行配置方案

方案1:修复Google Chrome安装问题

问题根源在于Chrome源配置错误和依赖缺失,调整后的Dockerfile如下:

FROM python:3.10-slim

ENV DEBIAN_FRONTEND=noninteractive
WORKDIR /app

# 安装Chrome依赖与签名密钥
RUN apt-get update && apt-get install -y --no-install-recommends \
    wget gnupg2 ca-certificates \
    && wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-chrome-keyring.gpg \
    && echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome-keyring.gpg] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \
    && apt-get update \
    && apt-get install -y google-chrome-stable \
    && rm -rf /var/lib/apt/lists/*

# 安装与Chrome版本匹配的ChromeDriver
RUN CHROME_VERSION=$(google-chrome --version | grep -oP '\d+\.\d+\.\d+\.\d+') \
    && wget -O /tmp/chromedriver.zip "https://storage.googleapis.com/chromedriver/releases/$CHROME_VERSION/chromedriver_linux64.zip" \
    && unzip /tmp/chromedriver.zip -d /usr/local/bin/ \
    && chmod +x /usr/local/bin/chromedriver \
    && rm /tmp/chromedriver.zip

# 安装Python依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# 无头浏览器运行所需环境变量
ENV DISPLAY=:99
ENV CHROME_PATH=/usr/bin/google-chrome-stable

CMD ["python", "app/scraper.py"]

方案2:修复Chromium的Selenium配置问题

错误127是因为Selenium自动下载的ChromeDriver与系统Chromium版本不兼容,且缺少底层依赖,调整如下:

修改后的Dockerfile

FROM python:3.10-slim

ENV DEBIAN_FRONTEND=noninteractive
WORKDIR /app

# 安装完整依赖包
RUN apt-get update && apt-get install -y --no-install-recommends \
    chromium chromium-driver \
    fonts-liberation libnss3 libx11-6 libatk-bridge2.0-0 \
    libatspi2.0-0 libgtk-3-0 libxcomposite1 libxcursor1 \
    libxdamage1 libxrandr2 libgbm-dev libu2f-udev libvulkan1 \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 强制Selenium使用系统安装的chromedriver
ENV PATH="/usr/bin:$PATH"

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8080

爬虫代码中指定WebDriver路径

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

chrome_options = Options()
# 无头模式配置
chrome_options.add_argument('--headless=new')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
# 指定Chromium二进制文件路径
chrome_options.binary_location = '/usr/bin/chromium'

# 使用系统安装的chromedriver
driver = webdriver.Chrome(
    executable_path='/usr/bin/chromedriver',
    options=chrome_options
)

# 后续爬取逻辑...

内容的提问来源于stack exchange,提问作者flowerchild245

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 19:04:55