How to Run Aspose.HTML in Docker – Aspose.HTML for .NET

Docker Container

Docker is a popular open-source platform that allows you to automate the deployment and management of applications within containers. Containers have many features and benefits, providing a lightweight and isolated environment for running applications and allowing them to work consistently across different operating systems and infrastructures.

The container is like a virtual machine. It can be installed, deleted, stopped, resumed, or connected to the terminal. So, the docker container is a lightweight, self-contained executable software package that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.

In this article, we’ll look at how to containerize a .NET application with docker and integrate Aspose.HTML functionality into your project. Here you will find step-by-step instructions on how to run Aspose.HTML in docker – how to create a console application and set up a Dockerfile.

System Requirements

Before you can run Aspose.HTML in docker, you must have docker installed on your system. To install Docker Desktop successfully, you should ensure that your system meets the minimum requirements for docker installation. For information on how to install docker on Windows, Mac, or Linux follow the links:

How to Create Console Application with Docker Support

Let’s consider the example: we create a simple Console Application ConsoleApp that converts an HTML document (document.html) and saves it in the TIFF format. This application you can build and run in docker. To create a console application project, follow a few steps:

  1. Open a Visual Studio (Visual Studio 2022 is used in the example), and on the “Create a new project” stage, select a Console App C#.

Text “Create a new Console App”

  1. Leave the directory and parameters for the console application project by default (as in the example), or specify your own:

Text “Set directory and parameters for the console application project”

  1. Сhoose the .NET 6.0 (Long-term support) from the Framework menu.

Text “Сhoose the .NET 6.0 (Long-term support) from the Framework menu”

  1. Add docker support to the project and select the target Linux OS.

Text “Add docker support to the project”

After adding docker support, a Dockerfile is created with default content.

How to Configure a Dockerfile

By properly configuring the Dockerfile, you can define the steps to build a docker image and create a containerized environment that includes all the necessary dependencies and configurations to run your application.

  1. Here is the default Dockerfile content:
 1#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
 2
 3FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
 4WORKDIR /app
 5
 6FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
 7WORKDIR /src
 8COPY ["ConsoleApp/ConsoleApp.csproj", "ConsoleApp/"]
 9RUN dotnet restore "ConsoleApp/ConsoleApp.csproj"
10COPY . .
11WORKDIR "/src/ConsoleApp"
12RUN dotnet build "ConsoleApp.csproj" -c Release -o /app/build
13
14FROM build AS publish
15RUN dotnet publish "ConsoleApp.csproj" -c Release -o /app/publish
16
17FROM base AS final
18WORKDIR /app
19COPY --from=publish /app/publish .
20ENTRYPOINT ["dotnet", "ConsoleApp.dll"]
  1. You need to edit the Dockerfile and install libgdiplus, the required libraries and fonts.
 1#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
 2
 3FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
 4WORKDIR /app
 5RUN apt-get update \
 6    && apt-get install -y --no-install-recommends libgdiplus libc6-dev \
 7    && apt-get clean \
 8    && rm -rf /var/lib/apt/lists/*
 9RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
10RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig
11
12FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
13WORKDIR /src
14COPY ["ConsoleApp/ConsoleApp.csproj", "ConsoleApp/"]
15RUN dotnet restore "ConsoleApp/ConsoleApp.csproj"
16COPY . .
17WORKDIR "/src/ConsoleApp"
18RUN dotnet build "ConsoleApp.csproj" -c Release -o /app/build
19
20FROM build AS publish
21RUN dotnet publish "ConsoleApp.csproj" -c Release -o /app/publish /p:UseAppHost=false
22
23FROM base AS final
24WORKDIR /app
25COPY --from=publish /app/publish .
26ENTRYPOINT ["dotnet", "ConsoleApp.dll"]

A Dockerfile is a text file that contains a set of instructions used to build a docker image. It can be divided into four distinct blocks: docker base image, docker build image, docker publish image, and docker final image blocks. Let’s go through the steps in a Dockerfile and their meanings:

  1. The docker base image defines the base image and installs the necessary dependencies needed to handle images and fonts in the container.
  1. The docker build image contains the instructions used for building .NET applications.
  1. The docker publish image declares the application for deployment.
  1. The docker final image runs the application when the docker container starts.

These four blocks in the Dockerfile work together to build a multi-stage docker image. The initial base and build stages are used to install dependencies, restore packages, and build the application. The next publish and final blocks are used for publishing the application for deployment and running when the container starts.

Example of Console Application

In the example, we suggest using a simple program that converts an HTML document to a TIFF image format:

 1using Aspose.Html;
 2using Aspose.Html.Rendering.Image;
 3
 4namespace ConsoleApp
 5{
 6    internal class Program
 7    {
 8        static void Main(string[] args)
 9        {
10            using var doc = new HTMLDocument("document.html");
11            using var dev = new ImageDevice(new ImageRenderingOptions(ImageFormat.Tiff), "result.tiff");
12            doc.RenderTo(dev);
13        }
14    }
15}

Configure Console Application and Run Aspose.HTML in Docker

  1. To run Aspose.HTML in docker, install the latest Aspose.HTML version and System.Drawing.Common 6.0 from NuGet.

Text “Install the latest Aspose.HTML version from NuGet”

Text “Install the System.Drawing.Common 6.0 from NuGet”

  1. Edit the ConsoleApp.csproj project – add the option InvariantGlobalization = false:

The following file is a .NET project file that defines the project settings, dependencies, and additional files needed to create an executable application. <InvariantGlobalization> is an additional element that disables invariant globalization behavior in the project. When set to false, the project will use the default globalization behavior.

 1<Project Sdk="Microsoft.NET.Sdk">
 2
 3  <PropertyGroup>
 4    <OutputType>Exe</OutputType>
 5    <TargetFramework>net6.0</TargetFramework>
 6    <ImplicitUsings>enable</ImplicitUsings>
 7    <Nullable>enable</Nullable>
 8    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
 9  </PropertyGroup>
10
11  <ItemGroup>
12    <PackageReference Include="Aspose.HTML" Version="23.5.0" />
13    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
14    <PackageReference Include="System.Drawing.Common" Version="6.0.0" />
15  </ItemGroup>
16
17  <ItemGroup>
18    <None Update="document.html">
19      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
20    </None>
21  </ItemGroup>
22
23  <ItemGroup>
24    <RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
25  </ItemGroup>
26  
27  <PropertyGroup>
28	  <InvariantGlobalization>false</InvariantGlobalization>
29  </PropertyGroup>
30
31</Project>
  1. Set the “Сopy to Output Directory” property for the converted file:

Text “Set the “Сopy to Output Directory” property for the converted file”

  1. When all required dependencies and properties are added, run the HTML to TIFF conversion for Linux in a docker container by clicking the green button.

Text “Run docker”

Help & Support

If you need help or have questions about running Aspose.HTML in docker, please contact Free Support Forum or Free Consulting. We will be happy to help!

See Also

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.