How to run Document. Net in Docker


Document .Net can be used inside docker containers that are running .NET Docker images. To use Docker, you must first install Docker Desktop.
Also, note that Visual Studio 2019, .NET Core 3.0 SDK is used in the example, provided below.

Docker Desktop is an easy-to-install application for your Mac or Windows environment that enables you to build and share containerized applications and microservices. Docker Desktop includes Docker Engine, Docker CLI client, Docker ComposeNotaryKubernetes, and Credential Helper.

Microservices, in conjunction with containerization make it possible to easily combine technologies. Docker allows you to easily integrate Document .Net functionality into your application, regardless of what technology is in your development stack.

You can create a Docker container using many of our Code examples.
Notice: Since Windows and Linux have different relative paths to the file, you will need to make changes to the source and the resulting file.

1. Create a new project Console App (.NET Core)

How to create a simple .NET Core application you may read here.
Let us say, you have just created a .NET Core project, that will convert "PDF to DOCX" and name it: "DockerDemo".

In my case, the project is here:

Document .Net can help your application to convert a document from a one format to another.
You'll need only to Load() a document and Save() to a desired format:


            DocumentCore dc = DocumentCore.Load("...");
            dc.Save("....");

Our code sample to load Book.pdf and convert it in Book.docx


					using System;
using SautinSoft.Document;

namespace SautinSoft.Document.Docker.Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load Book.pdf 
            DocumentCore dc = DocumentCore.Load("/TestOut/Book.pdf", new PdfLoadOptions());
            // Save Book.docx
            dc.Save("/TestOut/Book.docx", new DocxSaveOptions());
            // Displaying a message about successful conversion
            Console.WriteLine("Converted - [OK]");
        }
    }
}
					

Note that the “TestOut” folder is specified as an output folder for saving output documents. When running the application in Docker, a folder on the host machine will be mounted to this folder in the container.
This will enable you to easily view the output generated by Document .Net in the Docker container.

In my case, Book.pdf placed in "D:\Download".

2. Configuring a DockerFile

The next step is to create and configure the DockerFile.

1. Create a new file with name "DockerFile" and place it next to the project file of your application (csproj or vbproj). Keep this file name without extension (the default).
2. In the DockerFile, specify:


FROM mcr.microsoft.com/dotnet/runtime:3.1 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/nightly/sdk:3.1 AS build
WORKDIR /data-src
COPY ["DockerDemo.csproj", ""]
RUN dotnet restore "./DockerDemo.csproj"
COPY . .
WORKDIR "/data-src/."
RUN dotnet build "DockerDemo.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "DockerDemo.csproj" -c Release -o /app/publish

FROM base AS final

# Update package sources to include supplemental packages (contrib archive area).
RUN sed -i 's/main/main contrib/g' /etc/apt/sources.list

# Downloads the package lists from the repositories.
RUN apt-get update

# Install System.Drawing.Common dependency.
RUN apt-get install -y libgdiplus

# Install Microsoft TrueType core fonts.
RUN apt-get install -y ttf-mscorefonts-installer

# Or install Liberation TrueType fonts.
# RUN apt-get install -y fonts-liberation

# Or some other font package...

WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet",  "DockerDemo.dll"]

When creating PDF or Word files with images, System.Drawing.Common is used to process image data, which requires a libgdiplus package on a Linux container. When creating/reading PDF files, the font files need to be present in the container. The official Linux images for .NET won't have any fonts installed. So, you'll need to copy the necessary font files to the Linux container, or install a font package like ttf-mscorefonts-installer.
The above DockerFile contains instructions for installation: libgdiplus, ttf-mscorefonts-installer.

You may save the DockerFile to the project folder.

Ok. We created a simple project and prepared DockerFile. Now we need to launch it using Docker.

3. Building and Running the Application in Docker

Now the application can be built and run in Docker.

Notice, check that Docker is started! If the current status is "Stop", "Failed", "Pause", please restart Docker.


Open your favorite command prompt (cmd.exe, powershell, etc), change directory to the folder with the application (folder where the project file and the DockerFile are placed) and run the following command:

Type the following command to create the docker image


            docker build -t dockerdemo -f DockerFile .

The first time this command is executed it may take longer, since Docker needs to download the required images.
Once the previous command is completed, run the following command:


            docker run --mount type=bind,source="YourPath",target=/TestOut --rm dockerdemo from Docker

In my case, "YourPath" - "D:\Download". Pay attention to the mount argument, because, as mentioned earlier, a folder on the host machine is mounted into the container’s folder, to easily see the results of the application execution.
Paths in Linux are case sensitive. Full path in my case: "D:\Download\TestOut".

Notice: If you experience startup and imaging errors, please check the following Docker settings:

 

After executing the command, you will receive the result as a converted document.



Download

Running .NET Core application in docker is a breeze. Most importantly it integrates seamlessly.

Thank you.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.