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

AWS Lambda中Ubuntu Docker镜像Chrome创建mimeapps.list崩溃问题

问题:AWS Lambda中自定义Ubuntu镜像启动Chrome崩溃

背景

基于Ubuntu构建Docker镜像用于AWS Lambda,已配置lambda-runtime-interface,安装google-chrome-stable配合最新版Puppeteer实现网页爬取。镜像在ECS容器环境运行正常,但Lambda收到请求时,Puppeteer启动Chrome会崩溃。

错误堆栈

Error: Failed to launch the browser process!
mkdir: cannot create directory '/home/sbx_user1051': Read-only file system
touch: cannot touch '/home/sbx_user1051/.local/share/applications/mimeapps.list': No such file or directory
/usr/bin/google-chrome: line 45: /dev/fd/63: No such file or directory
/usr/bin/google-chrome: line 46: /dev/fd/63: No such file or directory
chrome_crashpad_handler: --database is required
Try 'chrome_crashpad_handler --help' for more information.
[35:35:0828/093835.966383:ERROR:socket.cc(120)] recvmsg: Connection reset by peer (104)

当前Puppeteer启动代码

const browser = await puppeteer.launch({ headless: "new", executablePath: '/usr/bin/google-chrome', ignoreDefaultArgs: ['--disable-extensions'], args: ['--no-sandbox', '--disable-setuid-sandbox','--no-default-browser-check'], timeout: 0,  protocolTimeout: 24000000 });

Dockerfile内容

FROM ubuntu:latest
RUN apt-get update
RUN apt-get install curl wget -y

#Installing NodeJS 16
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install nodejs

# Installing dependencies for Google-chrome
RUN apt install -y libxss1 libappindicator1 libindicator7 libasound2 libgconf-2-4 libnspr4 libnss3 libx11-xcb1 libxtst6 xauth xvfb

RUN apt install -y libatk-bridge2.0-0 fonts-liberation libatspi2.0-0 libgbm1 libgtk-4-1 libu2f-udev libvulkan1 libxkbcommon0 xdg-utils

# Installing Google-chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb
RUN apt-get install -f
RUN google-chrome --version

# Installing dependencies for Lambda runtime interface
RUN apt-get install -y \
g++ \
make \
cmake \
unzip \
libtool \
libcurl4-openssl-dev

# Installing lambda runtime interface
RUN apt-get install cmake -y
RUN apt-get install autoconf -y
RUN npm install aws-lambda-ric

# Copy function code
COPY / ${LAMBDA_TASK_ROOT}

RUN npm install

ENV NPM_CONFIG_CACHE=/tmp/.npm
ENV CHROME_USER_DATA_DIR=/tmp/chrome

ENTRYPOINT ["/usr/bin/npx", "aws-lambda-ric"]
CMD ["index.handler"]

解决方案

1. 调整Puppeteer启动参数

修改executablePath为Chrome实际二进制文件,添加参数强制Chrome将所有可写目录指向/tmp,同时禁用/dev/shm使用(Lambda环境中该空间受限):

const browser = await puppeteer.launch({
  headless: "new",
  executablePath: '/usr/bin/google-chrome-stable',
  ignoreDefaultArgs: ['--disable-extensions'],
  args: [
    '--no-sandbox',
    '--disable-setuid-sandbox',
    '--no-default-browser-check',
    '--user-data-dir=/tmp/chrome-user-data',
    '--disk-cache-dir=/tmp/chrome-cache',
    '--crash-dumps-dir=/tmp/chrome-crash-dumps',
    '--disable-dev-shm-usage'
  ],
  timeout: 0,
  protocolTimeout: 24000000
});

2. 在Dockerfile中设置HOME环境变量

Lambda默认的/home/sbx_user1051是只读目录,将HOME指向/tmp让Chrome默认使用可写目录:

# 在现有ENV后添加
ENV HOME=/tmp

3. 预创建Chrome所需目录(可选)

在Dockerfile中添加命令,提前创建并授权/tmp下的Chrome相关目录,避免启动时创建失败:

# 在安装Chrome后添加
RUN mkdir -p /tmp/chrome-user-data /tmp/chrome-cache /tmp/chrome-crash-dumps && chmod -R 777 /tmp/chrome-*

4. 修改后的完整Dockerfile片段

FROM ubuntu:latest
RUN apt-get update
RUN apt-get install curl wget -y

#Installing NodeJS 16
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install nodejs

# Installing dependencies for Google-chrome
RUN apt install -y libxss1 libappindicator1 libindicator7 libasound2 libgconf-2-4 libnspr4 libnss3 libx11-xcb1 libxtst6 xauth xvfb

RUN apt install -y libatk-bridge2.0-0 fonts-liberation libatspi2.0-0 libgbm1 libgtk-4-1 libu2f-udev libvulkan1 libxkbcommon0 xdg-utils

# Installing Google-chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb
RUN apt-get install -f
RUN google-chrome --version

# 预创建Chrome可写目录并授权
RUN mkdir -p /tmp/chrome-user-data /tmp/chrome-cache /tmp/chrome-crash-dumps && chmod -R 777 /tmp/chrome-*

# Installing dependencies for Lambda runtime interface
RUN apt-get install -y \
g++ \
make \
cmake \
unzip \
libtool \
libcurl4-openssl-dev

# Installing lambda runtime interface
RUN apt-get install cmake -y
RUN apt-get install autoconf -y
RUN npm install aws-lambda-ric

# Copy function code
COPY / ${LAMBDA_TASK_ROOT}

RUN npm install

ENV NPM_CONFIG_CACHE=/tmp/.npm
ENV CHROME_USER_DATA_DIR=/tmp/chrome
ENV HOME=/tmp

ENTRYPOINT ["/usr/bin/npx", "aws-lambda-ric"]
CMD ["index.handler"]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 02:59:51