【问题标题】:Docker + puppeteer + nodejs + chromeDocker + puppeteer + nodejs + chrome
【发布时间】:2021-04-13 04:24:31
【问题描述】:

我的代码有错误,期待您的帮助! 我正在编写一个测试 puppeteer,它会自动进入 nodejs 上的站点(代码有效),但是当我保存它并在 Docker 中运行它时,它根本不起作用(chrome 无法启动)。 任何帮助都会让我开心! 我们希望得到您的帮助!!

代码js

const puppeteer = require('puppeteer-core');
const download = require('image-downloader');
const fs = require('fs');
const express = require("express");



// buildPaths.js
const { buildPathHtml, buildPathPdf } = require('./buildPaths');
(async() => {
    const browser = await puppeteer.launch({        
    executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
    headless: false,
    slowMo:300,
    args: [
        //'--auto-open-devtools-for-tabs',
        '--disable-dev-shm-usage',
        '--no-sandbox',
        '--disable-setuid-sandbox'
    ]
    });    
    console.log('Browser openned');
    const page = await browser.newPage();
    // dịnh dạng khung cửa sổ hiển thị
   // page.setViewport({ width: 1280, height: 926 });
   try{
  const pageURL =  await page.goto('http://kenh14.vn/', {waitUntil: 'networkidle2', timeout: 0});
    console.log('Page loaded');
    console.log(`opened the page: ${pageURL}`);
   }catch(error){
       console.log(`failed to open the page: ${pageURL} with the error: ${error}`);
   }
   

码头文件

FROM node:latest
RUN  apt-get update \
     && apt-get install -y wget gnupg ca-certificates \
     && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
     && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
     && apt-get update \
     # We install Chrome to get all the OS level dependencies, but Chrome itself
     # is not actually used as it's packaged in the node puppeteer library.
     # Alternatively, we could could include the entire dep list ourselves
     # (https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix)
     # but that seems too easy to get out of date.
     && apt-get install -y google-chrome-stable \
     && rm -rf /var/lib/apt/lists/* \
     && wget --quiet https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -O /usr/sbin/wait-for-it.sh \
     && chmod +x /usr/sbin/wait-for-it.sh    
RUN mkdir -p /app/src    
WORKDIR /app/src    
# COPY package.json .  
COPY  package.json package-lock.json ./
RUN npm install
COPY . . 
#EXPOSE 4001
CMD ["node","puppeteer-one-page.js"]

【问题讨论】:

  • 尝试在没有executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',headless: false, 的情况下运行
  • 遇到的任何错误请补充
  • 感谢您的帮助,我仍然收到类似这样的错误“终端将被任务重用,按任意键关闭它”并且 docker 会自动关闭

标签: javascript node.js docker google-chrome puppeteer


【解决方案1】:

如果您不使用卷绑定,则无法在容器内部访问主机文件。

尝试通过

绑定chrome文件夹

docker run -v C:\\Program Files (x86)\\Google\\Chrome\\Application:/opt/chrome .

所以将-v C:\\Program Files (x86)\\Google\\Chrome\\Application:/opt/chrome 添加到您的 docker run 中。(类似地,您可以使用 docker-compose volume 选项)

然后使用“/opt/chrome/chrome.exe”作为executablePath

参考

【讨论】:

  • 感谢您的帮助,我仍然收到类似这样的错误“终端将被任务重用,按任意键关闭它”并且 docker 会自动关闭
猜你喜欢
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-07
相关资源
最近更新 更多