简体   繁体   English

如何让 Puppeteer-Sharp 在运行 Docker (.NET Core 6) 的 AWS Elastic Beanstalk 上工作?

[英]How to get Puppeteer-Sharp working on an AWS Elastic Beanstalk running Docker (.NET Core 6)?

I'm looking for an up-to-date example of how to get PuppeteerSharp running on an AWS Elastic Beanstalk instance running Docker (.NET Core 6).我正在寻找如何让 PuppeteerSharp 在运行 Docker (.NET Core 6) 的 AWS Elastic Beanstalk 实例上运行的最新示例。 There are quite a few articles out there which are either outdated, poorly documented, or both.那里有相当多的文章要么过时,要么文档不足,要么两者兼而有之。 I tried installing Chrome dependencies in my Dockerfile, however, I'm not able to get it running.我尝试在我的 Dockerfile 中安装 Chrome 依赖项,但是,我无法运行它。

Does anyone have a working example using AWS + .NET Core 6 + Docker + Puppeteer?有没有人有使用 AWS + .NET Core 6 + Docker + Puppeteer 的工作示例?

This is my current Dockerfile:这是我现在的 Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY . ./
ARG VERSION_SUFFIX=dev
ENV VERSION_SUFFIX=v$VERSION_SUFFIX
RUN dotnet restore Book/*.csproj
RUN dotnet publish Book/*.csproj -c Release -o out /p:VersionSuffix=$VERSION_SUFFIX

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0

WORKDIR /app
COPY --from=build-env /app/out .
EXPOSE 80
EXPOSE 443
ENTRYPOINT ["dotnet", "Book.dll"]

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

Code:代码:

using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true, Args = new[] {"--no-sandbox"}
});
await using var page = await browser.NewPageAsync();

Exception:例外:

An unhandled exception was thrown by the application.","Exception":"PuppeteerSharp.ProcessException: Failed to launch browser! /app/.local-chromium/Linux-884014/chrome-linux/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory     at PuppeteerSharp.States.ChromiumStartingState.StartCoreAsync(LauncherBase p) in C:\\projects\\puppeteer-sharp\\lib\\PuppeteerSharp\\States\\ChromiumStartingState.cs:line 83    at PuppeteerSharp.States.ChromiumStartingState.StartCoreAsync(LauncherBase p) in C:\\projects\\puppeteer-sharp\\lib\\PuppeteerSharp\\States\\ChromiumStartingState.cs:line 89    at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options) in C:\\projects\\puppeteer-sharp\\lib\\PuppeteerSharp\\Launcher.cs:line 68    at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options) in C:\\projects\\puppeteer-sharp\\lib\\PuppeteerSharp\\Launcher.cs:line 91 

Related issues:相关问题:

https://github.com/hardkoded/puppeteer-sharp/issues/1180 https://github.com/hardkoded/puppeteer-sharp/issues/262 https://github.com/hardkoded/puppeteer-sharp/issues/1180 https://github.com/hardkoded/puppeteer-sharp/issues/262

For anybody still encountering this issue, the answer by jamie-tillman on this github issue solved it for me:对于仍然遇到此问题的任何人, jamie-tillman在这个 github 问题上的回答为我解决了这个问题:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
#####################
#PUPPETEER RECIPE based on https://github.com/hardkoded/puppeteer-sharp/issues/1180#issuecomment-1015532968
#####################
RUN apt-get update && apt-get -f install && apt-get -y install wget gnupg2 apt-utils
RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list
RUN apt-get update \
&& apt-get install -y google-chrome-stable --no-install-recommends --allow-downgrades fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf
######################
#END PUPPETEER RECIPE
######################
ENV PUPPETEER_EXECUTABLE_PATH "/usr/bin/google-chrome-stable"
WORKDIR /app
EXPOSE 80
EXPOSE 443

The path to the executable is automatically detected by Puppeteer. Puppeteer 会自动检测到可执行文件的路径。 However, I still had to disable sandboxmode as seen in the snippet below:但是,我仍然必须禁用沙盒模式,如下面的代码片段所示:

var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true,
    Args = new [] {
      "--disable-gpu",
      "--disable-dev-shm-usage",
      "--disable-setuid-sandbox",
      "--no-sandbox"}
});

More useful information is found in the issue thread so I recommend skimming through it if you still have trouble.更多有用的信息可以在问题线程中找到,所以如果您仍然遇到问题,我建议您浏览一下。

I worked it out myself.我自己解决了。 See this GitHub issue for details.有关详细信息,请参阅此 GitHub 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM