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

Docker容器中Chrome启动崩溃问题(AWS ECS部署场景)

Chrome在AWS ECS Docker环境中启动崩溃问题排查

问题背景

通过Copilot CLI在AWS ECS部署Python脚本,配套Dockerfile及ChromeDriver配置后,运行时出现/usr/bin/google-chrome进程停止,ChromeDriver判定Chrome崩溃的错误。

相关配置代码

Dockerfile

# pull official base image
FROM python:3.10-slim as build
RUN apt-get update
RUN  python3.10 -m pip install --upgrade pip
RUN apt-get install -y --no-install-recommends \
    build-essential gcc
# required for psycopg2
RUN apt-get install -y libpq-dev 
RUN apt-get install -y gnupg
RUN apt-get install -y wget
RUN apt-get install -y curl
RUN apt-get install -yqq unzip
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -

# RUN apt-get install -y wget xvfb unzip
# Set up the Chrome PPA -> (not sure if needed)
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list

# Update the package list
RUN apt-get update -y

# Set up Chromedriver Environment variables and install chrome
ENV CHROMEDRIVER_VERSION 114.0.5735.90
ENV CHROME_VERSION 114.0.5735.90-1
RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
    && apt install -y /tmp/chrome.deb \
    && rm /tmp/chrome.deb

ENV CHROMEDRIVER_DIR /usr/local/bin/chromedriver
RUN mkdir $CHROMEDRIVER_DIR

# Download and install Chromedriver
RUN wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
RUN unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR

# Put Chromedriver into the PATH
ENV PATH $CHROMEDRIVER_DIR:$PATH

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

# env variables for aws
ENV AWS_ACCESS_KEY_ID=*-*-*-*
ENV AWS_SECRET_ACCESS_KEY=*-*-*-*
ENV AWS_DEFAULT_REGION=*-*-*-*

# install selenium
RUN pip install selenium==4.11.2

RUN mkdir /app
WORKDIR /app
COPY . .
COPY ./requirements.txt /requirements.txt 
RUN  python3.10 -m pip install -r requirements.txt

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

Python ChromeDriver配置

def set_web_driver_settings() -> webdriver.Chrome:
    options = webdriver.ChromeOptions()

    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-gpu")
    # options.add_argument("--start-maximized") # for the populating twitter and discord script. Activate it before the execute that script.
    options.add_argument("--window-size=1920,1080")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--log-level=3")
    options.add_argument(
        "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
    )

    driver = webdriver.Chrome(
        executable_path="usr/local/bin/chromedriver",
        options=options,
    )  # docker

    return driver

报错信息

{"error": "Message: unknown error: Chrome failed to start: crashed.\n  (chrome not reachable)\n  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)\nStacktrace:\n#0 0x555555c2d4e3 <unknown>\n#1 0x55555595cc76 <unknown>\n#2 0x555555985d78 <unknown>\n#3 0x555555982029 <unknown>\n#4 0x5555559c0ccc <unknown>\n#5 0x5555559c047f <unknown>\n#6 0x5555559b7de3 <unknown>\n#7 0x55555598d2dd <unknown>\n#8 0x55555598e34e <unknown>\n#9 0x555555bed3e4 <unknown>\n#10 0x555555bf13d7 <unknown>\n#11 0x555555bfbb20 <unknown>\n#12 0x555555bf2023 <unknown>\n#13 0x555555bc01aa <unknown>\n#14 0x555555c166b8 <unknown>\n#15 0x555555c16847 <unknown>\n#16 0x555555c26243 <unknown>\n#17 0x2aaaab7ac134 <unknown>\n", "event": "scraper failed", "level": "error", "timestamp": "2024-04-08 04:47.50"}

排查与修复方案

1. 修正ChromeDriver路径错误

Python配置中executable_path参数缺少开头的斜杠,导致按相对路径查找ChromeDriver,无法定位到正确文件。修改为绝对路径:

driver = webdriver.Chrome(
    executable_path="/usr/local/bin/chromedriver",
    options=options,
)

2. 统一Chrome版本与User-Agent

Docker中安装的Chrome版本为114.0.5735.90,但User-Agent指定的是118.0.0.0,版本不匹配会引发兼容性问题。更新User-Agent为对应版本:

options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.90 Safari/537.36"
)

3. 补充Slim镜像缺失的系统依赖

Python:3.10-slim轻量镜像缺少Chrome运行必需的系统库,在Dockerfile的Chrome安装步骤前添加依赖安装命令:

RUN apt-get install -y --no-install-recommends \
    libglib2.0-0 \
    libnss3 \
    libgconf-2-4 \
    libfontconfig1 \
    libxrender1 \
    libxext6

4. 启用Xvfb虚拟显示

虽然设置了DISPLAY=:99,但Slim镜像未安装Xvfb,Chrome在headless模式下可能因缺少显示环境崩溃。修改Dockerfile:

  • 取消注释RUN apt-get install -y wget xvfb unzip
  • 将CMD替换为启动Xvfb后再运行脚本:
CMD ["sh", "-c", "Xvfb :99 -screen 0 1920x1080x24 > /dev/null 2>&1 && python start_worker.py"]

5. 调整ECS任务资源配置

AWS ECS任务CPU/内存分配过低会导致Chrome启动时资源不足崩溃,建议为任务分配至少1vCPU和2GB内存,保证Chrome有足够运行资源。


内容的提问来源于stack exchange,提问作者Beşir Kassab

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 04:04:53