Playwright in Docker not working: Microsoft.Playwright.PlaywrightException: Executable doesn't exist

6k Views Asked by At

I have a .net dashboard application where a user can export the current page as pdf via playwright. This works locally, however, when I run the application in Docker I get this error:


    Microsoft.Playwright.PlaywrightException: Executable doesn't exist at /root/.cache/ms-playwright/chromium-1005/chrome-linux/chrome
     ╔════════════════════════════════════════════════════════════╗
     ║ Looks like Playwright was just installed or updated.       ║
     ║ Please run the following command to download new browsers: ║
     ║                                                            ║
     ║     pwsh bin\Debug\netX\playwright.ps1 install             ║
     ║                                                            ║
     ║ <3 Playwright Team                                         ║
     ╚════════════════════════════════════════════════════════════╝

Dockerfile:

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

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal  AS build
WORKDIR /src
COPY ["Dashboard/Dashboard.csproj", "Dashboard/"]
RUN dotnet restore "Dashboard/Dashboard.csproj"
COPY . .
WORKDIR "/src/Dashboard"
RUN dotnet add package Microsoft.Playwright
RUN dotnet build "Dashboard.csproj" -c Release -o /app/build
RUN dotnet tool update --global PowerShell
RUN pwsh /app/build/playwright.ps1 install --with-deps
FROM build AS publish
RUN dotnet publish "Dashboard.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Dashboard.dll"]

3

There are 3 best solutions below

0
On

For anyone else, can find in document

After build success, have playwright.ps1 file created, just run it with powershell:

# dir: /your_source_folder
powershell.exe bin/Debug/netX/playwright.ps1 install
# or pwsh bin/Debug/netX/playwright.ps1 install

Or you can install special browser, like chrome, firefox:

powershell.exe bin/Debug/netX/playwright.ps1 install firefox
powershell.exe bin/Debug/netX/playwright.ps1 install chrome

Cache folders:

Windows: %USERPROFILE%\AppData\Local\ms-playwright
MacOS: ~/Library/Caches/ms-playwright
Linux: ~/.cache/ms-playwright

And add to "Post-build event" into csproj:

powershell.exe -Command $(TargetDir)playwright.ps1 install firefox;
0
On

Add the blow line after COPY --from=publish /app/publish .

RUN chown -R pwuser:pwuser /app
0
On

The error came from me not correctly understanding Docker stages. Here a fixed Dockerfile, where I installed Playwright in the base stage:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
RUN apt-get update
RUN apt-get -y install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_16.x  | bash -
RUN apt-get -y install nodejs
RUN npx playwright install
RUN apt-get update && apt-get install -y --no-install-recommends \
    fonts-liberation\
    libasound2\
    libatk-bridge2.0-0\
    libatk1.0-0\
    libatspi2.0-0\
    libcairo2\
    libcups2\
    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
WORKDIR /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Dashboard.dll"]