欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > docker打包使用有头模式playwright

docker打包使用有头模式playwright

2025/5/9 14:10:32 来源:https://blog.csdn.net/Awesome_py/article/details/146903082  浏览:    关键词:docker打包使用有头模式playwright

1.打包镜像

创建Dockerfile文件如下

# playywright 官方镜像
FROM mcr.microsoft.com/playwright:v1.37.0-jammy# 设置非交互式环境变量和时区
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Etc/UTC# 安装 Python 3.9 和 pip(修复时区阻塞问题)
RUN apt-get update && \apt-get install -y software-properties-common tzdata && \ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \dpkg-reconfigure --frontend noninteractive tzdata && \add-apt-repository -y ppa:deadsnakes/ppa && \apt-get update && \apt-get install -y python3.9 python3.9-distutils && \curl -sS https://bootstrap.pypa.io/get-pip.py | python3.9# 安装 Playwright Python 库和浏览器驱动
RUN python3.9 -m pip install playwright && \python3.9 -m playwright install# 安装 Xvfb 和窗口管理器(支持有头模式)
RUN apt-get install -y xvfb fluxbox && \rm -rf /var/lib/apt/lists/*# 设置环境变量
ENV DISPLAY=:99
ENV PYTHONPATH=/usr/bin/python3.9# 启动脚本
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.shENTRYPOINT ["/entrypoint.sh"]

打包后镜像如下

FROM registry.cn-hangzhou.aliyuncs.com/spider_tie/playwright-python

2.启动容器测试

docker run -it --rm \-v $(pwd)/test.py:/app/test.py \registry.cn-hangzhou.aliyuncs.com/spider_tie/playwright-python \python3.9 /app/test.py

test.py内容如下

from playwright.sync_api import sync_playwrightwith sync_playwright() as p:# 启动有头浏览器browser = p.chromium.launch(headless=False)page = browser.new_page()page.goto("https://example.com")print(page.title())browser.close()

测试结果

[root@centos-1 playwright_compile]# docker run -it --rm   -v $(pwd)/test.py:/app/test.py   playwright-python   python3.9 /app/test.py
Example Domain

可以看到正常打印网页中的title

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词