Docker + puppeteer + nodejs + chrome

zzwlnbp8  于 2023-08-01  发布在  Go
关注(0)|答案(1)|浏览(129)

我的代码出错了,期待您的帮助!
我正在写一个测试puppeteer,它可以自动进入nodejs上的站点(代码可以工作),但是当我保存它并在Docker中运行它时,它根本不工作(chrome无法 Boot )。
任何帮助都让我快乐!
我们希望得到您的帮助!!
代码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 opened');
    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"]

fhity93d

fhity93d1#

如果不使用卷绑定,则无法访问容器内部的主机文件。
尝试通过绑定chrome文件夹
第一个月
因此,将-v C:\\Program Files (x86)\\Google\\Chrome\\Application:/opt/chrome添加到您docker运行中。(类似地,您可以使用docker-compose volume选项)
然后使用“/opt/chrome/chrome.exe”为executablePath
参考文献

相关问题