1

I have created a Dockerfile for Azure Function that build and installs playwright into container. Install all the dependency related to Playwright. I tried with Playwright official image still it was giving same error. So I have installed Playwright CLI globally to install only chromium dependency

Below is a dockerfile

# Base image for Azure Functions .NET Isolated
FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 AS base
WORKDIR /home/site/wwwroot
EXPOSE 8080

RUN apt-get update -q && \
    apt-get install -y -qq --no-install-recommends \
        xvfb \
        libxcomposite1 \
        libxdamage1 \
        libatk1.0-0 \
        libasound2 \
        libdbus-1-3 \
        libnspr4 \
        libgbm1 \
        libatk-bridge2.0-0 \
        libcups2 \
        libxkbcommon0 \
        libatspi2.0-0 \
        libnss3 \
        libpango-1.0-0 \
        libcairo2 \
        libxrandr2

# Build stage for the service project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["FunctionApps/CheckImage/CheckImage.csproj", "FunctionApps/CheckImage/"]
COPY ["Libraries/HealthCheckHelper/HealthCheckHelper.csproj", "Libraries/HealthCheckHelper/"]
COPY ["Libraries/BlobHelper/BlobHelper.csproj", "Libraries/BlobHelper/"]

RUN dotnet restore "./FunctionApps/CheckImage/CheckImage.csproj"
COPY . .
WORKDIR "/src/FunctionApps/CheckImage"

RUN dotnet build "./CheckImage.csproj" -c $BUILD_CONFIGURATION -o /app/build

RUN dotnet tool install --global Microsoft.Playwright.CLI
ENV PATH="${PATH}:/root/.dotnet/tools"
RUN playwright install chromium

# Publish stage for the service project
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./CheckImage.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Final stage - Using Playwright .NET image as the base for compatibility
FROM mcr.microsoft.com/playwright/dotnet:v1.51.0-noble AS final
WORKDIR /home/site/wwwroot

# Copy published application files
COPY --from=publish /app/publish .

# Install Playwright dependencies and Chromium
RUN apt-get update && apt-get install -y --no-install-recommends \
    && npx playwright install chromium \
    && npx playwright install-deps chromium \
    && rm -rf /var/lib/apt/lists/*

# Environment variables for Azure Functions
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
    PLAYWRIGHT_BROWSERS_PATH=/home/site/wwwroot/.playwright

While running in local it is giving me

2025-04-25 17:58:39 [2025-04-25T12:28:39.800Z] Driver not found: /home/site/wwwroot/bin/.playwright/node/linux-x64/node
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z] Result: Driver not found: /home/site/wwwroot/bin/.playwright/node/linux-x64/node
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z] Exception: Microsoft.Playwright.PlaywrightException: Driver not found: /home/site/wwwroot/bin/.playwright/node/linux-x64/node
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at Microsoft.Playwright.Helpers.Driver.GetExecutablePath() in /_/src/Playwright/Helpers/Driver.cs:line 96
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at Microsoft.Playwright.Transport.StdIOTransport.GetProcess(String driverArgs) in /_/src/Playwright/Transport/StdIOTransport.cs:line 116
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at Microsoft.Playwright.Transport.StdIOTransport..ctor() in /_/src/Playwright/Transport/StdIOTransport.cs:line 46
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at Microsoft.Playwright.Playwright.CreateAsync() in /_/src/Playwright/Playwright.cs:line 43
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at CheckImage.ScanImage.CheckImage(HttpRequest req, String scanid) in D:\Projects\Dev-CheckImage\ProjectX\FunctionApps\CheckImage\CheckImage.cs:line 32
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z] Stack:    at Microsoft.Playwright.Helpers.Driver.GetExecutablePath() in /_/src/Playwright/Helpers/Driver.cs:line 96
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at Microsoft.Playwright.Transport.StdIOTransport.GetProcess(String driverArgs) in /_/src/Playwright/Transport/StdIOTransport.cs:line 116
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at Microsoft.Playwright.Transport.StdIOTransport..ctor() in /_/src/Playwright/Transport/StdIOTransport.cs:line 46
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at Microsoft.Playwright.Playwright.CreateAsync() in /_/src/Playwright/Playwright.cs:line 43
2025-04-25 17:58:39 [2025-04-25T12:28:39.801Z]    at CheckImage.ScanImage.CheckImage(HttpRequest req, String scanid) in D:\Projects\Dev-CheckImage\ProjectX\FunctionApps\CheckImage\CheckImage.cs:line 32.

It seems it is not taking path of bin/Debug/net8.0. How do I make it such that it finds driver path.

0

1 Answer 1

3

Driver not found: /home/site/wwwroot/bin/.playwright/node/linux-x64/node

To resolve above issue :

  • I have added the below lines to .csproj file.

.csproj :

<PropertyGroup>
  <PlaywrightPlatform>all</PlaywrightPlatform>
</PropertyGroup>
  • And I added the below lines to my code to install Playwright.
private static void PerformInitialPlaywrightInstall()
{
    try
    {
        var exitCode = Microsoft.Playwright.Program.Main(new[] { "install", "--with-deps", "chromium" });
        if (exitCode != 0)
        {
            throw new Exception($"Playwright installation failed with exit code {exitCode}");
        }
    }
    catch (Exception ex)
    {
        throw new Exception($"Error during Playwright installation: {ex.Message}");
    }
}

Function1.cs :

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Playwright;

namespace FunctionApp55.Functions
{
    public class Function1
    {
        private readonly ILogger _logger;
        public Function1(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<Function1>();
        }

        [Function("Function1")]
        public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req)
        {
            _logger.LogInformation("Playwright .NET function triggered.");
            var response = req.CreateResponse();

            try
            {
                PerformInitialPlaywrightInstall();
                using var playwright = await Playwright.CreateAsync();
                await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
                {
                    Headless = true,
                    Args = new[] { "--no-sandbox", "--disable-setuid-sandbox", "--disable-gpu" }
                });
                var page = await browser.NewPageAsync();
                await page.GotoAsync("https://example.com", new PageGotoOptions { Timeout = 30000 });
                var screenshot = await page.ScreenshotAsync(new PageScreenshotOptions { FullPage = true });
                response.Headers.Add("Content-Type", "image/png");
                await response.Body.WriteAsync(screenshot, 0, screenshot.Length);

                return response;
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error taking screenshot: {ex.Message}");
                response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                await response.WriteStringAsync("Failed to take screenshot.");
                return response;
            }
        }
        private static void PerformInitialPlaywrightInstall()
        {
            try
            {
                var exitCode = Microsoft.Playwright.Program.Main(new[] { "install", "--with-deps", "chromium" });
                if (exitCode != 0)
                {
                    throw new Exception($"Playwright installation failed with exit code {exitCode}");
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Error during Playwright installation: {ex.Message}");
            }
        }
    }
}

Dockerfile :

FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 AS base
WORKDIR /home/site/wwwroot
EXPOSE 8080

RUN apt-get update -q && \
    apt-get install -y -qq --no-install-recommends \
        xvfb \
        libxcomposite1 \
        libxdamage1 \
        libatk1.0-0 \
        libasound2 \
        libdbus-1-3 \
        libnspr4 \
        libgbm1 \
        libatk-bridge2.0-0 \
        libcups2 \
        libxkbcommon0 \
        libatspi2.0-0 \
        libnss3 \
        libpango-1.0-0 \
        libcairo2 \
        libxrandr2

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["FunctionApp55.csproj", "./"]
RUN dotnet restore "./FunctionApp55.csproj"

COPY . .
WORKDIR "/src"

RUN dotnet build "./FunctionApp55.csproj" -c $BUILD_CONFIGURATION -o /app/build

RUN dotnet tool install --global Microsoft.Playwright.CLI
ENV PATH="${PATH}:/root/.dotnet/tools"

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

FROM mcr.microsoft.com/playwright/dotnet:v1.51.0-noble AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .

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

logs :

enter image description here enter image description here

Output :

enter image description here

Reference : 1. Getting started - Library | Playwright .NET 2. [BUG] Driver not found: /app/bin/.playwright/node/linux-x64/playwright.sh · Issue #2619 · microsoft/playwright-dotnet

Sign up to request clarification or add additional context in comments.

6 Comments

Hey Thank You @DasariKamali. I've used given Dockerfile but while running this dockerfile it is not starting function. Just showing file system of container.
Have you tried using the above function code to install the Playwrite?
Yes, see i.imgur.com/5A3QTCh.png this for better idea.
Try to run the image using docker run -p 8080:80 <imageName> or using the Container run button in your function's project.
Tried both way it starts and automatically closes. Error is something Function:Worker:HostEndpoint not found. I don't how to resolve in docker. In local, it is easy to add localsettings.json file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.