node.js - puppeteer headless chrome 是否可以在 Azure 应用服务的 Windows 容器中工作?

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

更新

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

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

<小时/>

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

然而,它可以在 Azure 应用服务的 Linux 容器中完美运行。我是否需要做一些特别的事情才能让它在 Windows 容器中工作?还是由于sandboxing limitations而徒劳无功?在 Windows 应用服务(容器或非容器)中?不过,如果是这种情况,那么我会期望 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" ]

最佳答案

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

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

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

关于node.js - puppeteer headless chrome 是否可以在 Azure 应用服务的 Windows 容器中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57620361/

相关文章:

node.js - express 路线分离

Java 转义 JSON 字符串?

azure - 将现有 Visual Studio 项目添加到现有 Devops 项目

azure - Microsoft Azure - 通过我的 Web 应用程序使用自定义域获取 404 页面

node.js - 无法使用 Node.js 连接到 Apache ActiveMQ

JavaScript/Node.js : function returning another function based on parameters

azure - 是否可以使用现有存储帐户来创建 Databricks 工作区?

c# - 如何在 .cscfg 文件中设置 Windows azure - 辅助角色的数据库连接字符串..?

azure - 在自主位置之间路由流量的最佳 Azure 产品是什么

asp.net - SameSite None 未在 Azure Web App 中设置 cookie 属性