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

如何编写可运行含Selenium的Python脚本的Dockerfile

核心问题说明
  • Docker容器默认运行在Linux环境下,Windows平台专属的chromedriver.exe无法在Linux环境中执行
  • 你找到的参考Dockerfile仅安装了Firefox对应的geckodriver驱动,未安装Firefox浏览器本体,且和现有代码调用Chrome的逻辑不匹配,无法直接使用
  • Selenium正常启动浏览器需要同时满足两个条件:系统内安装对应浏览器本体、与浏览器版本匹配的驱动文件加入系统可执行路径,二者缺一不可
方案1:保留Chrome浏览器,适配容器环境

该方案不需要调整你现有业务代码逻辑,仅需修改Dockerfile和Selenium启动参数即可。

可用Dockerfile配置

# 选用轻量Python基础镜像
FROM python:3.10-slim

# 安装Chrome浏览器及运行依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    wget \
    gnupg \
    ca-certificates \
    fonts-liberation \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libcups2 \
    libdbus-1-3 \
    libdrm2 \
    libgbm1 \
    libgtk-3-0 \
    libnspr4 \
    libnss3 \
    libu2f-udev \
    libvulkan1 \
    xdg-utils \
    && wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
    && dpkg -i google-chrome-stable_current_amd64.deb || apt-get install -fy \
    && rm google-chrome-stable_current_amd64.deb

# 自动安装和Chrome版本匹配的chromedriver,加入系统可执行路径
RUN CHROME_VERSION=$(google-chrome --version | grep -oP '\d+\.\d+\.\d+\.\d+') \
    && apt-get install -y --no-install-recommends unzip \
    && wget -q https://storage.googleapis.com/chrome-for-testing-public/$CHROME_VERSION/linux64/chromedriver-linux64.zip \
    && unzip chromedriver-linux64.zip \
    && chmod +x chromedriver-linux64/chromedriver \
    && mv chromedriver-linux64/chromedriver /usr/local/bin/ \
    && rm -rf chromedriver-linux64 chromedriver-linux64.zip \
    && apt-get purge -y --auto-remove unzip wget gnupg \
    && rm -rf /var/lib/apt/lists/*

# 安装Python依赖
RUN pip install --no-cache-dir selenium

# 将本地Python脚本复制到容器内,替换your_script.py为你实际的脚本文件名
COPY your_script.py .

# 容器启动命令,对应替换脚本文件名
CMD ["python", "your_script.py"]

Python代码调整

替换原有Selenium启动部分的代码,补充容器环境下Chrome必须的启动参数,不需要再指定本地exe驱动路径:

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
from selenium.webdriver.common.by import By

options = Options()
options.add_argument('--headless=new')
options.add_argument('--window-size=1920x935')
# 容器环境下Chrome必须加的两个启动参数
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# 驱动已在系统路径中,不需要手动指定executable_path
driver = webdriver.Chrome(options=options)

# 原有业务逻辑不需要修改
driver.get(f"https://www.wildberries.ru/catalog/{id}/feedbacks?imtId={imt_id}")
time.sleep(5)
big_stat = driver.find_element(by=By.CLASS_NAME, value="rating-product__numb")
方案2:改用Firefox浏览器,适配参考Dockerfile

如果你希望基于找到的geckodriver配置修改,可以把脚本改为适配Firefox,同时补全Dockerfile中缺失的Firefox安装步骤。

调整后的Dockerfile配置

FROM python:3.10-slim

# 安装Firefox浏览器、geckodriver驱动
RUN apt-get update -y && apt-get install -y --no-install-recommends \
    wget \
    firefox-esr \
    && wget -O /tmp/geckodriver.tar.gz https://github.com/mozilla/geckodriver/releases/download/v0.34.0/geckodriver-v0.34.0-linux64.tar.gz \
    && tar xf /tmp/geckodriver.tar.gz -C /usr/local/bin/ \
    && chmod +x /usr/local/bin/geckodriver \
    && rm -f /tmp/geckodriver.tar.gz \
    && rm -rf /var/lib/apt/lists/*

# 安装Python依赖
RUN pip install --no-cache-dir selenium

# 复制本地脚本到容器,替换your_script.py为实际脚本文件名
COPY your_script.py .

CMD ["python", "your_script.py"]

Python代码调整

将Selenium初始化部分改为Firefox版本:

from selenium.webdriver.firefox.options import Options
from selenium import webdriver
import time
from selenium.webdriver.common.by import By

options = Options()
options.add_argument('--headless')
options.add_argument('--width=1920')
options.add_argument('--height=935')

driver = webdriver.Firefox(options=options)

# 原有业务逻辑不需要修改
driver.get(f"https://www.wildberries.ru/catalog/{id}/feedbacks?imtId={imt_id}")
time.sleep(5)
big_stat = driver.find_element(by=By.CLASS_NAME, value="rating-product__numb")
部署注意事项
  • 本地的chromedriver.exe不需要上传到服务器/复制进镜像,该文件仅能在Windows环境运行
  • 不要在代码中硬编码Windows格式的驱动路径(比如带.exe后缀、Windows盘符路径),Linux环境不识别这类路径格式,只要驱动放在/usr/local/bin这类系统默认PATH路径下,Selenium会自动识别加载
  • Dockerfile需要和你的Python脚本放在同一目录下,参考目录结构:
    托管主机目录结构
  • 镜像构建命令:在Dockerfile所在目录执行docker build -t selenium-task .
  • 容器运行命令:docker run --rm selenium-task

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 11:24:22