kate
kate

Reputation: 179

Cannot launch Playwright in Ubuntu Docker image with .NET

I've created service with .Net 6.0 using Playwright v1.19.1 and built an image on Ubuntu 20.04 as below:

In the code I set up to launch the Chromium browser

 await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
                {
                    Headless = false,
                    Channel = "chrome",
                    Args = new [] { "--disable-dev-shm-usage"}
                });

Below is Docker file:

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build

COPY . /app
WORKDIR /app

ENV PLAYWRIGHT_BROWSERS_PATH=/app/playwright

RUN dotnet restore "src/Service/Service.csproj"
RUN dotnet build "src/Service/Service.csproj" -c Release
RUN pwsh src/Service/bin/Release/net6.0/playwright.ps1 install chrome

RUN dotnet publish "src/Service/Service.csproj" -c Release -o /app/output

# =======================================================================================================
# Runtime
# =======================================================================================================
FROM mcr.microsoft.com/dotnet/runtime:6.0-focal AS runtime
EXPOSE 80
WORKDIR /home/site/wwwroot
COPY --from=build /app/output .
COPY --from=build /app/playwright .playwright/ms-playwright

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
    PLAYWRIGHT_BROWSERS_PATH=/home/site/wwwroot/.playwright/ms-playwright

The image was built successful but when I start the image got below error. Do you know how to solve this issue? Thank you

Microsoft.Playwright.PlaywrightException: Browser closed.

==================== Browser output: ====================

<launching> /opt/google/chrome/chrome --disable-background-networking 
--enable-features=NetworkService,NetworkServiceInProcess 
--disable-background-timer-throttling --disable-backgrounding-occluded-windows 
--disable-breakpad --disable-client-side-phishing-detection 
--disable-component-extensions-with-background-pages 
--disable-default-apps --disable-dev-shm-usage --disable-extensions 
--disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,
DestroyProfileOnBrowserClose,MediaRouter,AcceptCHFrame,AutoExpandDetailsElement 
--allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection 
--disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding 
--disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run 
--enable-automation --password-store=basic --use-mock-keychain
 --no-service-autorun--export-tagged-pdf --no-sandbox --disable-dev-shm-usage 
--user-data-dir=/tmp/playwright_chromiumdev_profile-HPOMeC
 --remote-debugging-pipe --no-startup-window
<launched> pid=85
[pid=85][err] [85:85:0330/032427.114118:ERROR:ozone_platform_x11.cc(247)] Missing X server or $DISPLAY
[pid=85][err] [85:85:0330/032427.114193:ERROR:env.cc(225)] The platform failed to initialize.  Exiting.

Upvotes: 2

Views: 1543

Answers (1)

Eliran Alon
Eliran Alon

Reputation: 189

You need inside your container to set up a DISPLAY env parameter so you will need to do in the container:

export DISPLAY='{IP}:0'

also start your browser with 'Headless = true', also add --disable-gpu flag

if you want to run browser with GUI, remove the gpu flag and start your browser with 'Headless = false'

Upvotes: 2

Related Questions