Puppeteer 项目 Docker 部署方案

1、JS代码修改

// const puppeteer = require('puppeteer');
const puppeteer = require('puppeteer-core');



// const browser = await puppeteer.launch({
//   headless: false
// });
const browser = await puppeteer.connect({ browserWSEndpoint: 'ws://browser-server:3000' });// browser-server为无头浏览器服务地址

2、将无头浏览器使用docker运行,这里使用browserless/chrome的docker镜像;示例使用docker-compose编排服务,docker-compose.yml文件如下:

version: "2.2"
networks:
  puppeteer_net:
    driver: bridge
services:
  browser-server:
    image: browserless/chrome:latest
    #image: browserless/chrome:tzshanghai
    ports:
      - "3000:3000"
    environment:
      - TZ=Asia/Shanghai
      - MAX_CONCURRENT_SESSIONS=10
      - CONNECTION_TIMEOUT=10000000
    networks:
      puppeteer_net:
        aliases:
          - browser-server

3、时区问题处理。使用browserless/chrome:latest系统时间会较北京时间少8小时;我们在官方镜像的基础上重构建镜像处理即可,Dockerfile如下:

FROM browserless/chrome:latest

MAINTAINER [email protected]

# browserless/chrome镜像默认进入的是blessuser,没有权限安装
USER root

ENV TZ=Asia/Shanghai
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
RUN apt-get update
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get install tzdata
RUN apt-get clean
RUN apt-get autoclean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# 返回到blessuser
USER blessuser

在Dockerfile所在目录执行镜像构建命令

sudo docker build -t browserless/chrome:tzshanghai .

修改docker-compose.yml中image为browserless/chrome:tzshanghai即可;

4、browserless/chrome的相关配置请参考官方文档。

 

你可能感兴趣的:(笔记,puppeteer,docker,browserless)