GenerateCode

Deploying C# Applications with Puppeteer in Docker on Azure Container Apps

Posted on 03/09/2025 16:00

Category: C#

In this article, we'll examine how to deploy a C# application using Puppeteer for web scraping or automated testing within a Docker container on Azure Container Apps. We will also address common issues encountered, especially regarding Chrome installation.

Step 1: Create Dockerfile

Begin with a Dockerfile for your .NET 9 ASP.NET application. The following is an example that installs Chrome and its dependencies:

# Use .NET 9.0 ASP.NET base image
FROM mcr.microsoft.com/dotnet/aspnet:9.0

# Set the working directory to /app
WORKDIR /app

# Expose ports for the application
EXPOSE 8080
EXPOSE 8081

# Install dependencies and Chrome
RUN apt-get update -y && \
    apt-get install -yqq \
    unzip \
    wget \
    curl \
    libgdiplus \
    libc6-dev \
    libx11-dev && \
    wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
    apt-get install -y ./google-chrome-stable_current_amd64.deb && \
    # Get the latest ChromeDriver version number
    CHROME_DRIVER_VERSION=$(curl -sS https://chromedriver.storage.googleapis.com/LATEST_RELEASE) && \
    wget -q -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/${CHROME_DRIVER_VERSION}/chromedriver_linux64.zip && \
    unzip -q /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ && \
    rm -f /tmp/chromedriver.zip google-chrome-stable_current_amd64.deb && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set the environment path
ENV PATH="/usr/local/bin/chromedriver:${PATH}"

# Entry point for the .NET application
ENTRYPOINT ["dotnet", "PdfApi.dll"]

Step 2: Implementing Puppeteer in C#

In your C# code, ensure that you can correctly detect Chrome's installation and handle errors if it’s unavailable. Here’s how you can utilize Puppeteer within an ASP.NET controller:

[HttpGet(Name = "GetWeatherForecast")]
public async Task<ActionResult> Get()
{
    byte[] screenshot;
    string chromePath = "/usr/bin/google-chrome-stable";

    bool exists = System.IO.File.Exists(chromePath);
    if (exists)
    {
        _logger.LogInformation("file exists");
    }
    else
    {
        _logger.LogInformation("file does not exist");
    }

    try
    {
        using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
        {
            Headless = true,
            ExecutablePath = Environment.GetEnvironmentVariable("PUPPETEER_EXECUTABLE_PATH") ?? chromePath,
            Args = new[] { "--no-sandbox", "--disable-setuid-sandbox" }
        });

        using var page = await browser.NewPageAsync();
        await page.GoToAsync("https://example.com");
        screenshot = await page.ScreenshotDataAsync();
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message + ex.StackTrace);
    }

    return File(screenshot, "image/png");
}

Summary

This article covered how to build a Docker container with a .NET 9 ASP.NET application that uses Puppeteer for headless browsing. We addressed a common deployment issue where the Chrome executable could not be found, leading to a failure in launching the browser. By ensuring that Chrome is installed correctly and checking its existence before usage, you can prevent runtime errors in Azure Container Apps.

Related Posts

How to Log Raw SQL Queries with Dapper in C#

Posted on 05/13/2025 09:00

This article explains how to effectively log raw SQL queries and their parameters in C# when using Dapper, focusing on resolving issues with DynamicParameters not capturing values.

How to Fix 'FileNotFoundException' in C# Worker Services

Posted on 05/13/2025 06:30

This article helps fix 'FileNotFoundException' in C# Worker Services by addressing common causes like missing assemblies and providing a step-by-step guide for resolution.

How to Manage Empty Headers and Footers in Word with C#

Posted on 05/13/2025 04:00

In this article, learn how to manage headers and footers in Word using C#. Discover methods to check their existence, manipulate them safely, and remove empty ones effectively.

Comments

Add a Comment

OSZAR »