【问题标题】:Does puppeteer headless chrome work in a Windows container in Azure App Service?puppeteer headless chrome 是否可以在 Azure 应用服务的 Windows 容器中工作?
【发布时间】:2019-08-28 08:29:08
【问题描述】:

更新

正如@Joaquín 所说,基本映像缺少依赖项。经过大量研究和反复试验,我决定使用重型 servercore 图像以及 Chrome 所需的必要 fonts。您可以在 my github 上找到我的测试应用程序和 Dockerfile。

我能够让它作为 Windows 容器在 Azure 应用服务中运行,但该映像比我的 Linux 版本大很多倍,启动时间也慢得多。我对 Docker 容器还是很陌生,所以到目前为止还很有趣。


在 Azure 应用服务的 Windows 容器中运行我的 puppeteer 应用程序最终会得到 UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!

然而,它在 Azure 应用服务的 Linux 容器中完美运行。我必须做一些特别的事情才能让它在 Windows 容器中工作吗?还是由于 Windows 应用服务(容器与否)中的 sandboxing limitations 而徒劳?不过,如果是这种情况,那么我预计会出现 spawn UNKNOWN 错误...

我尝试过的一些事情包括使用以下 puppeteer.launch() 选项:

  1. ignoreDefaultArgs: ['--disable-extensions'](每troubleshooting

  2. executablePath: '<path_to_chromium>'

这是错误和堆栈跟踪

(node:1272) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!


TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

    at onClose (C:\app\node_modules\puppeteer\lib\Launcher.js:349:14)
    at Interface.helper.addEventListener (C:\app\node_modules\puppeteer\lib\Launcher.js:338:50)
    at Interface.emit (events.js:203:15)
    at Interface.close (readline.js:397:8)
    at Socket.onend (readline.js:173:10)
    at Socket.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1129:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:1272) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1272) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Dockerfile

ARG core=mcr.microsoft.com/windows/servercore:ltsc2019
ARG target=mcr.microsoft.com/windows/nanoserver:1809
FROM $core as download

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

ENV NODE_VERSION 10.16.0

RUN Invoke-WebRequest $('https://nodejs.org/dist/v{0}/node-v{0}-win-x64.zip' -f $env:NODE_VERSION) -OutFile 'node.zip' -UseBasicParsing ; \
    Expand-Archive node.zip -DestinationPath C:\ ; \
    Rename-Item -Path $('C:\node-v{0}-win-x64' -f $env:NODE_VERSION) -NewName 'C:\nodejs'

FROM $target

COPY --from=download /nodejs/ /nodejs/

USER Administrator
RUN setx /M PATH "%PATH%;C:\nodejs"

RUN mkdir "C:\app"
WORKDIR "C:\app"

COPY . .

RUN npm install

EXPOSE 8000
CMD [ "node.exe", "server.js" ]

【问题讨论】:

  • 您好,您的应用程序和容器是否已在本地启动并运行?我尝试运行您的容器,但 NPM 安装失败,因为它找不到 package.json 并且最后一行没有执行,因为容器中没有 server.js。我在 c:\app 文件夹中也没有看到任何项目

标签: node.js azure azure-web-app-service puppeteer windows-container


【解决方案1】:

当涉及到 Windows 容器时,应用服务沙箱没有限制,因为 Hyper-V 被用作应用服务上容器的沙箱。

无法在容器内启动 chrome 的唯一原因是 Windows 容器的基本映像缺少 chrome 所需的一些依赖项。

如果您共享 dockerfile,我想重现该问题。

【讨论】:

  • 我用 dockerfile 更新了 OP。感谢您的帮助,我真的很感激。
  • 你说得对,我必须使用完整的 servercore 映像才能使其正常工作。我用我的发现更新了 OP 的顶部。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
相关资源
最近更新 更多