skip to Main Content

New and learning Dockerfile; need help to edit the following Dockerfile to containerize a C# Azure Function App with Playwright.

Here’s part of the function code:


    using System.Net;
    using Func_MyFunction.Interfaces;
    using Func_MyFunction.Models;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Azure.Functions.Worker.Http;
    using Microsoft.Extensions.Logging;
    using Microsoft.Playwright;


        [Function("HttpExample")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to Azure Functions!");

            return response;
        }

The other endpoints that will checks the UI of an URL and sends an email if it’s not visible.

This is the Dockerfile (with line numbers)

 1. FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated6.0 AS base
 2. WORKDIR /home/site/wwwroot
 3. EXPOSE 80
 4. 
 5. FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
 6. ARG BUILD_CONFIGURATION=Release
 7. WORKDIR /src
 8. COPY . .
 9. RUN ls -la
10. RUN dotnet restore "./Func_MyFunction.csproj"
11. RUN dotnet add package Microsoft.Playwright
12. RUN dotnet build "./Func_MyFunction.csproj" -c $BUILD_CONFIGURATION -o /app/build
13. RUN playwright install
14. 
15. FROM build AS publish
16. ARG BUILD_CONFIGURATION=Release
17. 
18. RUN dotnet publish "./Func_MyFunction.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
19. 
20. FROM base AS final
21. WORKDIR /home/site/wwwroot
22. COPY --from=publish /app/publish .
23. ENV AzureWebJobsScriptRoot=/home/site/wwwroot 
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

This is the error:

#14 [build 8/8] RUN playwright install
#14 0.415 /bin/sh: 1: playwright: not found
#14 ERROR: process "/bin/sh -c playwright install" did not complete successfully: exit code: 127
------
 > [build 8/8] RUN playwright install:
0.415 /bin/sh: 1: playwright: not found
------
Dockerfile:16
--------------------
  14 |     RUN dotnet add package Microsoft.Playwright
  15 |     RUN dotnet build "./Func_MyFunction.csproj" -c $BUILD_CONFIGURATION -o /app/build
  16 | >>> RUN playwright install
  17 |     
  18 |     FROM build AS publish
--------------------
ERROR: failed to solve: process "/bin/sh -c playwright install" did not complete successfully: exit code: 127

2

Answers


  1. Chosen as BEST ANSWER

    There were a couple of errors I had. One was this:

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

    The solution can be found here.

    In the .csproj file add:

    <PlaywrightPlatform>all</PlaywrightPlatform>
    

    The other error was:

    Microsoft.Playwright.PlaywrightException: 'Executable doesn't exist at /root/.cache/ms-playwright/chromium-xxxx/....'
    

    The root cause was that I was installing playwright in the wrong order of the Dockerfile. It should have been in the 'base' and not 'build'. Also, needed to install playwright dependencies. Here's a working Dockerfile

    FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated6.0 AS base
    # Install Playwright
    RUN apt-get update
    RUN apt-get -y install curl gnupg
    RUN curl -sL https://deb.nodesource.com/setup_18.x  | bash -
    RUN apt-get -y install nodejs
    RUN npx playwright install
    
    # Install Playwright depedencies
    RUN apt-get update && apt-get install -y --no-install-recommends 
         libglib2.0-0
         libnss3                 
         libnspr4                
         libdbus-1-3             
         libatk1.0-0             
         libatk-bridge2.0-0      
         libcups2                
         libdrm2                 
         libatspi2.0-0           
         libxcomposite1          
         libxdamage1             
         libxext6                
         libxfixes3              
         libxrandr2              
         libgbm1                 
         libxkbcommon0           
         libpango-1.0-0          
         libcairo2               
         libasound2  
    
    WORKDIR /home/site/wwwroot
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    ARG BUILD_CONFIGURATION=Release
    WORKDIR /src
    COPY ["Func_MyFunction/Func_MyFunction.csproj", "Func_MyFunction/"]
    RUN dotnet restore "./Func_MyFunction/Func_MyFunction.csproj"
    COPY . .
    RUN ls -la
    WORKDIR "/src/Func_MyFunction"
    RUN dotnet build "./Func_MyFunction.csproj" -c $BUILD_CONFIGURATION -o /app/build
    
    FROM build AS publish
    ARG BUILD_CONFIGURATION=Release
    RUN dotnet publish "./Func_MyFunction.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
    
    FROM base AS final
    WORKDIR /home/site/wwwroot
    COPY --from=publish /app/publish .
    RUN ls -la
    ENV AzureWebJobsScriptRoot=/home/site/wwwroot 
        AzureFunctionsJobHost__Logging__Console__IsEnabled=true
    

  2. I am able to install playwright using the below docker file.

    FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated6.0 AS base
    WORKDIR /home/site/wwwroot
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    ARG BUILD_CONFIGURATION=Release
    WORKDIR /src
    COPY ["FunctionApp63/FunctionApp63.csproj", "FunctionApp63/"]
    RUN dotnet restore "./FunctionApp63/FunctionApp63.csproj"
    COPY . .
    WORKDIR "/src/FunctionApp63"
    RUN dotnet build "./FunctionApp63.csproj" -c $BUILD_CONFIGURATION -o /app/build
    
    # Install Playwright
    RUN apt-get update && 
        apt-get install -y curl && 
        curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && 
        apt-get install -y nodejs
    RUN npm install -g playwright
    RUN playwright install
    
    FROM build AS publish
    ARG BUILD_CONFIGURATION=Release
    RUN dotnet publish "./FunctionApp63.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
    
    FROM base AS final
    WORKDIR /home/site/wwwroot
    COPY --from=publish /app/publish .
    ENV AzureWebJobsScriptRoot=/home/site/wwwroot 
        AzureFunctionsJobHost__Logging__Console__IsEnabled=true
    

    Upon building the docker image, I am getting below output and playwright got installed successfully to the src folder.

    enter image description here

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