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.sh
ENTRYPOINT ["/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_playwright
with 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