skip to Main Content

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). 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.

Does anyone have a working example using AWS + .NET Core 6 + Docker + Puppeteer?

This is my current 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

2

Answers


  1. Chosen as BEST ANSWER

    I worked it out myself. See this GitHub issue for details.


  2. For anybody still encountering this issue, the answer by jamie-tillman on this github issue solved it for me:

    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. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search