我正在寻找一个最新的例子,如何让PuppeteerSharp运行在一个AWS弹性豆柄实例运行码头(.NET核心6)。有相当多的文章要么已经过时,要么记录不清,或者两者兼而有之。我尝试在我的Dockerfile中安装Chrome依赖项,但是我无法运行它。
是否有人使用AWS + .NET Core 6+ Docker + Puppeteer的工作示例?
这是我当前的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代码:
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
发布于 2022-01-25 09:17:35
我自己解决的。详情请参见这个GitHub问题。
发布于 2022-04-26 14:01:24
对于仍然遇到这个问题的人,回答 by 杰米-蒂尔曼在这个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"}
});更多有用的信息在发布线程中找到,所以如果您仍然有困难,我建议浏览它。
https://stackoverflow.com/questions/70752901
复制相似问题