在Zyte平台自定义部署Docker镜像时遭遇Playwright执行文件问题
问题背景
在Zyte(原Scrapinghub)平台创建自定义Docker镜像,目标搭建包含Scrapy、Playwright、Twisted及必要依赖的Python环境,但运行时提示Playwright浏览器可执行文件不存在。
当前配置文件
Dockerfile
FROM python:3.11.6-slim WORKDIR /app COPY . /app RUN apt-get update \ && pip install --no-cache-dir --upgrade pip \ && pip install --no-cache-dir -r requirements.txt \ && playwright install --with-deps chromium \ && mv /root/.cache/ms-playwright /ms-playwright \ && mv /ms-playwright/chromium-* /ms-playwright/chromium \ && chmod -Rf 777 /ms-playwright ENV SCRAPY_SETTINGS_MODULE dummy.settings RUN python setup.py install
requirements.txt
scrapy==2.11.0 playwright==1.39.0 Twisted==22.10.0 scrapinghub-entrypoint-scrapy==0.17.1
错误信息
playwright._impl._api_types.Error: Executable doesn't exist at /root/.cache/ms-playwright/chromium-1084/chrome-linux/chrome
╔════════════════════════════════════════════════════════════╗
║ Looks like Playwright was just installed or updated. ║
║ Please run the following command to download new browsers: ║
║ playwright install ║
║ ║
║ <3 Playwright Team ║
╚════════════════════════════════════════════════════════════╝
解决方案
问题根源:手动移动浏览器目录导致Playwright默认路径失效,同时slim镜像缺少Chromium运行依赖,且Zyte平台运行容器时非root用户可能无法访问原路径。
修正后的Dockerfile
FROM python:3.11.6-slim WORKDIR /app COPY . /app # 指定Playwright浏览器安装到工作目录下,避免root权限及路径问题 ENV PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright RUN apt-get update && apt-get install -y --no-install-recommends \ # 安装Chromium运行必需的系统依赖 libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 \ libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 libasound2 \ libpango-1.0-0 libcairo2 libatspi2.0-0 libxshmfence1 \ && rm -rf /var/lib/apt/lists/* \ && pip install --no-cache-dir --upgrade pip \ && pip install --no-cache-dir -r requirements.txt \ && playwright install --with-deps chromium \ # 确保所有用户可访问浏览器文件 && chmod -R 777 $PLAYWRIGHT_BROWSERS_PATH ENV SCRAPY_SETTINGS_MODULE dummy.settings RUN python setup.py install
关键修改说明
- 固定浏览器路径:通过
PLAYWRIGHT_BROWSERS_PATH环境变量,强制Playwright将浏览器安装到工作目录下,避免后续运行时路径查找失败。 - 补充系统依赖:python:slim镜像精简了大量库,Chromium运行需要上述依赖,必须添加否则浏览器无法启动。
- 取消手动移动目录:删除原Dockerfile中移动浏览器目录的命令,保留Playwright默认的目录结构,避免路径匹配错误。
- 权限设置:给浏览器目录赋予全权限,确保Zyte平台运行用户可访问执行文件。
内容的提问来源于stack exchange,提问作者Kristiqn Tashev

