How to run Playwright in headed mode under dotnet docker container?

433 Views Asked by At

I'm trying to run a headed Firefox using Playwright in a docker container.

Headless works, but headed not.

Here is my Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

RUN apt-get update && apt-get install -y --no-install-recommends \
    xvfb\
    fonts-liberation\
    libasound2\
    libatk-bridge2.0-0\
    libatk1.0-0\
    libatspi2.0-0\
    libcairo2\
    libcups2\
    libdbus-glib-1-2\
    libdbus-1-3\
    libdrm2\
    libgbm1\
    libglib2.0-0\
    libgtk-3-0\
    libnspr4\
    libnss3\
    libpango-1.0-0\
    libx11-6\
    libxcb1\
    libxcomposite1\
    libxdamage1\
    libxext6\
    libxfixes3\
    libxrandr2\
    libxtst6\
    tzdata 


FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Project.csproj", "src/"]
RUN dotnet restore "src/Project.csproj"
COPY . .
WORKDIR "/src/src"
RUN dotnet build "Project.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Project.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

##ENV DISPLAY=:99

# Start Xvfb, set DISPLAY variable, and run the application
##ENTRYPOINT ["/usr/bin/xvfb-run -a $@", "dotnet", "Project.dll"]
#ENTRYPOINT ["/bin/sh", "-c", "/usr/bin/xvfb-run -a $@", ""]

##CMD ["xvfb-run", "dotnet", "Project.dll"]
##CMD Xvfb :99 -ac & export DISPLAY=:99 && dotnet Project.dll
ENTRYPOINT ["dotnet", "Project.dll"]

in the comments I'm showing many tries that didn't works. the image will compile to a Ubuntu 22 image.

the C# code does not have anything special.

    using (var pw = await Playwright.CreateAsync())
    {
        var proxyHandler = new ProxyHandler();

        await using var browser = await pw.Firefox.LaunchAsync(new BrowserTypeLaunchOptions()
        {
            Headless = false
        });

        var context = browser.NewContextAsync(new BrowserNewContextOptions() 
        { 
            IgnoreHTTPSErrors = true
        });

        var page = await browser.NewPageAsync();

        await page.GotoAsync("http://page");
    }

This is the error I receive when trying to run headed:

2023-09-21 21:33:31       An unhandled exception has occurred while executing the request.
2023-09-21 21:33:31       Microsoft.Playwright.PlaywrightException: 
2023-09-21 21:33:31       ╔════════════════════════════════════════════════════════════════════════════════════════════════╗
2023-09-21 21:33:31       ║ Looks like you launched a headed browser without having a XServer running.                     ║
2023-09-21 21:33:31       ║ Set either 'headless: true' or use 'xvfb-run <your-playwright-app>' before running Playwright. ║
2023-09-21 21:33:31       ║                                                                                                ║
2023-09-21 21:33:31       ║ <3 Playwright Team                                                                             ║
2023-09-21 21:33:31       ╚════════════════════════════════════════════════════════════════════════════════════════════════╝
2023-09-21 21:33:31       =========================== logs ===========================
2023-09-21 21:33:31       <launching> /ms-playwright/firefox-1422/firefox/firefox -no-remote -wait-for-browser -foreground -profile /tmp/playwright_firefoxdev_profile-PVqbMc -juggler-pipe -silent
2023-09-21 21:33:31       <launched> pid=954
2023-09-21 21:33:31       [pid=954][err] Error: no DISPLAY environment variable specified
2023-09-21 21:33:31       [pid=954] <process did exit: exitCode=1, signal=null>

How can I solve this?

0

There are 0 best solutions below