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

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

我正在寻找有关如何在 AWS Elastic Beanstalk 实例 运行 Docker (.NET Core 6) 上获取 PuppeteerSharp 运行 的最新示例。那里有相当多的文章要么过时,要么文档不足,要么两者兼而有之。我尝试在我的 Docker 文件中安装 Chrome 依赖项,但是,我无法获取它 运行.

有人有使用 AWS + .NET Core 6 + Docker + Puppeteer 的工作示例吗?

这是我当前的Docker文件:

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

代码:

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();

异常:

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 

相关问题:

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

我自己解决了。有关详细信息,请参阅 this GitHub issue

对于仍然遇到此问题的任何人,answer by 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

可执行文件的路径由 Puppeteer 自动检测。但是,我仍然必须禁用沙盒模式,如下面的代码片段所示:

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

在问题线程中可以找到更多有用的信息,所以如果您仍然遇到问题,我建议您浏览一下。