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

Docker容器中SeleniumBase启动Chrome遭遇HTTP连接超时问题求助

Docker容器中SeleniumBase启动Chrome遭遇HTTP连接超时问题求助

我写了一个基于SeleniumBase的Python脚本,本地运行完全符合预期,但部署到Docker容器后,一直报HTTP连接超时的错误,连main_task里的SB上下文逻辑都进不去,实在搞不定了,来求助大家!

错误信息

ERROR - An error occurred during the main task: HTTPConnectionPool(host='localhost', port=xxxxx): Read timed out. (read timeout=120)

更具体的错误日志:

HTTPConnectionPool(host='localhost', port=xxxxxx) Read timed out. (read timeout=120)

出问题的核心代码片段

以下是触发异常的关键代码,主要是SB实例初始化的部分:

import logging
import signal
import smtplib
import time
import base64
import pytesseract
import sys
import json
import os
from seleniumbase import SB
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image, ImageOps, ImageFilter
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import chromedriver_autoinstaller


chromedriver_autoinstaller.install()  # 检查当前ChromeDriver版本是否存在


def main_task(config):
    global sb_instance
    try: 
        with SB(uc=True, headless2=True, undetectable=True, use_auto_ext=False) as sb:
            # 核心业务逻辑,但根本执行不到这里
    except Exception as e:
        logging.error("An error occurred during the main task: %s", str(e))
        return -1

我的Dockerfile配置

FROM --platform=linux/amd64 python:3.9-slim

RUN apt-get update && apt-get install -y \
    wget \
    gnupg \
    tesseract-ocr \
    libglib2.0-0 \
    libnss3 \
    libgconf-2-4 \
    libxss1 \
    fonts-liberation \
    libappindicator3-1 \
    xdg-utils \
    unzip \
    net-tools \
    iputils-ping \
    dbus \
    && rm -rf /var/lib/apt/lists/*

RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /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

ENV DISPLAY=:99
ENV PATH="/usr/local/lib/python3.9/site-packages/seleniumbase/drivers/:${PATH}"

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

容器启动命令

docker run -d -p 8080:8080 -it --name aperol-spritz --cpus="8" --shm-size=4g -v $PWD:/app:rw -w /app $BASE_IMAGE bash -c "pip install -r /app/requirements.txt && python /app/sel.py"

Docker容器状态(docker inspect输出)

{
    "Id": "314969c0f7695f87e7af8a22bad419d60b7221ad36783b27ae8b4a1d60922904",
    "Created": "2024-12-06T04:29:34.960458338Z",
    "Path": "bash",
    "Args": [
        "-c",
        "pip install -r /app/requirements.txt && python /app/sel.py"
    ],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 25079,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2024-12-06T04:29:35.039250047Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "sha256:410d4b429be3a8044d27d231f92089700c1da0cf124cfcb41e42c8f153043fde",
    "ResolvConfPath": "/var/lib/docker/containers/314969c0f7695f87e7af8a22bad419d60b7221ad36783b27ae8b4a1d60922904/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/314969c0f7695f87e7af8a22bad419d60b7221ad36783b27ae8b4a1d60922904/hostname",
    "HostsPath": "/var/lib/docker/containers/314969c0f7695f87e7af8a22bad419d60b7221ad36783b27ae8b4a1d60922904/hosts",
    "LogPath": "/var/lib/docker/containers/314969c0f7695f87e7af8a22bad419d60b7221ad36783b27ae8b4a1d60922904/314969c0f7695f87e7af8a22bad419d60b7221ad36783b27ae8b4a1d60922904-json.log",
    "Name": "/aperol-spritz",
    "RestartCount": 0,
    "Driver": "overlay2",
    "Platform": "linux",
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "",
    "ExecIDs": [
        "7407cea0c7ac7d2295fcf51f99ee9992834b141c56ad74e01c45d2ee0295642d"
    ],
    "HostConfig": {
        "Binds": [
            "/Users/frozen/Documents/ClionProjects/sel:/app:rw"
        ],
        "ContainerIDFile": "",
        "LogConfig": {
            "Type": "json-file",
            "Config": {}
        },
        "NetworkMode": "bridge",
        "PortBindings": {
            "8080/tcp": [
                {
                    "HostIp": "",
                    "HostPort": "8080"
                }
            ]
        },
        "RestartPolicy": {
            "Name": "no",
            "MaximumRetryCount": 0
        },
        "AutoRemove": false,
        "VolumeDriver": "",
        "VolumesFrom": null,
        "ConsoleSize": [
            41,
            187
        ],
        "CapAdd": null,
        "CapDrop": null,
        "CgroupnsMode": "private",
        "Dns": [],
        "DnsOptions": [],
        "DnsSearch": [],
        "ExtraHosts": null,
        "GroupAdd": null,
        "IpcMode": "private",
        "Cgroup": "",
        "Links": null,
        "OomScoreAdj": 0,
        "PidMode": "",
        "Privileged": false,
        "PublishAllPorts": false,
        "ReadonlyRootfs": false,
        "SecurityOpt": null,
        "UTSMode": "",
        "UsernsMode": "",
        "ShmSize": 4294967296,
        "Runtime": "runc",
        "Isolation": "",
        "CpuShares": 0,
        "Memory": 0,
        "NanoCpus": 8000000000,
        "CgroupParent": "",
        "BlkioWeight": 0,
        "BlkioWeightDevice": [],
        "BlkioDeviceReadBps": [],
        "BlkioDeviceWriteBps": [],
        "BlkioDeviceReadIOps": [],
        "BlkioDeviceWriteIOps": [],
        "CpuPeriod": 0,
        "CpuQuota": 0,
        "CpuRealtimePeriod": 0,
        "CpuRealtimeRuntime": 0,
        "CpusetCpus": "",
        "CpusetMems": "",
        "Devices": [],
        "DeviceCgroupRules": null,
        "DeviceRequests": null,
        "MemoryReservation": 0,
        "MemorySwap": 0,
        "MemorySwappiness": null,
        "OomKillDisable": null,
        "PidsLimit": null,
        "Ulimits": [],
        "CpuCount": 0,
        "CpuPercent": 0,
        "IOMaximumIOps": 0,
        "IOMaximumBandwidth": 0,
        "MaskedPaths": [
            "/proc/asound",
            "/proc/acpi",
            "/proc/kcore",
            "/proc/keys",
            "/proc/latency_stats",
            "/proc/timer_list",
            "/proc/timer_stats",
            "/proc/sched_debug",
            "/proc/scsi",
            "/sys/firmware",
            "/sys/devices/virtual/powercap"
        ],
        "ReadonlyPaths": [
            "/proc/bus",
            "/proc/fs",
            "/proc/irq",
            "/proc/sys",
            "/proc/sysrq-trigger"
        ]
    },
    "GraphDriver": {
        "Data": {
            "LowerDir": "/var/lib/docker/overlay2/6b6ebea6bd46f0ba9dd7c7a81b427188e7dc380a2d0aa06ae8807873999639e5-init/diff:/var/lib/docker/overlay2/mlje1ari6gak9pna09zfrih25/diff:/var/lib/docker/overlay2/vk4w9hg69j4s3nusauk7ns4ld/diff:/var/lib/docker/overlay2/f737d0bea1171662c6997bfc3df7cf033162f21c937babffc4261523b328f191/diff:/var/lib/docker/overlay2/14eff14f8b6ee79e6ce8828fc642ef447784d607749d784a0dd9fe44ffa6cbc7/diff:/var/lib/docker/overlay2/b2fed1798c5a3965b6b7de7e907474a9fa6c1f207b8ea1212e2b629cdf2f1699/diff:/var/lib/docker/overlay2/5d4f24be554957235b3ec5272c7d0d3e7f1648a8780dfb1ada1f8ddf6a3c8048/diff",
            "MergedDir": "/var/lib/docker/overlay2/6b6ebea6bd46f0ba9dd7c7a81b427188e7dc380a2d0aa06ae8807873999639e5/merged",
            "UpperDir": "/var/lib/docker/overlay2/6b6ebea6bd46f0ba9dd7c7a81b427188e7dc380a2d0aa06ae8807873999639e5/diff",
            "WorkDir": "/var/lib/docker/overlay2/6b6ebea6bd46f0ba9dd7c7a81b427188e7dc380a2d0aa06ae8807873999639e5/work"
        },
        "Name": "overlay2"
    },
    "Mounts": [
        {
            "Type": "bind",
            "Source": "/Users/frozen/Documents/ClionProjects/sel",
            "Destination": "/app",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        }
    ]
}

我已经尝试过的操作:

  • 给容器分配了4G的shm-size
  • 启用了headless2无头模式
  • 安装了Chrome及所有依赖的系统库
  • 本地验证过ChromeDriver与Chrome版本完全匹配

有没有大佬遇到过类似的问题?或者能给我一些排查方向?跪谢!

备注:内容来源于stack exchange,提问作者Frozen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 16:10:27