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

无法在Docker中运行Selenium,请求协助排查并修复问题

Docker中运行Selenium的问题排查与修复

问题场景

本地Windows 10环境下,通过docker-compose部署两个服务:

  • selenium:基于官方selenium/standalone-chrome镜像,提供远程浏览器服务
  • webapp:基于python:3.8-slim构建,运行Python脚本连接远程Selenium服务

相关配置如下:

初始Dockerfile

FROM python:3.8-slim

# Install the Selenium Python bindings
RUN pip install selenium

# Download the ChromeDriver executable and add it to the PATH
RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip \
    && unzip /tmp/chromedriver.zip -d /usr/local/bin \
    && rm /tmp/chromedriver.zip
ENV PATH="/usr/local/bin:${PATH}"

docker-compose.yml

version: '3'
services:
  selenium:
    image: selenium/standalone-chrome
    ports:
      - "4444:4444"
      - "5900:5900"
  webapp:
    build: .
    volumes:
      - .:/app
    command: python3 test.py
    depends_on:
      - selenium

test.py脚本

from selenium import webdriver

driver = webdriver.Remote(
    command_executor='http://selenium:4444/wd/hub',
    desired_capabilities={'browserName': 'chrome'}
)

driver.get('https://www.google.com')
driver.save_screenshot('search_results.png')
driver.quit()

报错信息

执行docker-compose up后,首先触发报错:

ERROR [3/3] RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip     0.8s 
------
 > [3/3] RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip     && unzip /tmp/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip:
#0 0.736 /bin/sh: 1: wget: not found
------
failed to solve: executor failed running [/bin/sh -c wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip     && unzip /tmp/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip]: exit code: 127 

添加wget安装步骤后,又出现新报错:

#0 2.034 2022-12-17 08:19:54 (6.27 MB/s) - ‘/tmp/chromedriver.zip’ saved [5404417/5404417]
#0 2.034
#0 2.037 /bin/sh: 1: unzip: not found
------
failed to solve: executor failed running [/bin/sh -c wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip     && unzip /tmp/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip]: exit code: 127 

问题根源

  1. 精简镜像缺少基础工具:python:3.8-slim是Debian的轻量镜像,默认未预装wget、unzip这类工具,导致执行相关命令时触发"not found"错误。
  2. 冗余操作无意义:由于test.py通过webdriver.Remote连接独立的selenium服务容器,webapp容器完全不需要安装chromedriver——浏览器驱动已由selenium/standalone-chrome镜像提供,本地下载属于多余操作。

修复方案

直接简化Dockerfile,仅保留必要的Selenium依赖安装:

修正后的Dockerfile

FROM python:3.8-slim

# 仅安装Selenium Python绑定
RUN pip install selenium

如果后续确实需要在容器内使用wget、unzip(本场景非必需),可一次性安装工具并清理apt缓存以减小镜像体积:

FROM python:3.8-slim

RUN apt update && apt install -y wget unzip \
    && rm -rf /var/lib/apt/lists/*

RUN pip install selenium

# 若需下载文件可保留以下步骤(本场景无需)
# RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip \
#     && unzip /tmp/chromedriver.zip -d /usr/local/bin \
#     && rm /tmp/chromedriver.zip
# ENV PATH="/usr/local/bin:${PATH}"

额外优化建议

  • 服务就绪检测:depends_on仅保证容器启动顺序,不保证服务完全就绪,可给selenium服务添加健康检查避免连接失败:
    修改docker-compose.yml:
    version: '3'
    services:
      selenium:
        image: selenium/standalone-chrome
        ports:
          - "4444:4444"
          - "5900:5900"
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:4444/wd/hub/status"]
          interval: 10s
          timeout: 5s
          retries: 5
      webapp:
        build: .
        volumes:
          - .:/app
        command: python3 test.py
        depends_on:
          selenium:
            condition: service_healthy
    
  • 适配新版Selenium:若使用Selenium 4.x+版本,desired_capabilities已被弃用,建议改用options:
    修改test.py:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument("--headless=new")
    driver = webdriver.Remote(
        command_executor='http://selenium:4444/wd/hub',
        options=chrome_options
    )
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 10:15:14