Containerize LEADTOOLS Applications with Docker - C# .NET Core

This tutorial shows how to set up a .NET Core Console application using the LEADTOOLS SDK in a Docker container.

Overview  
Summary This tutorial covers how to Containerize a .NET Core Console Application using LEADTOOLS with Docker.
Completion Time 30 minutes
Platform .NET Core Console Application using Docker
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Before working on the Containerize .NET Core Applications with Docker - C# .NET Core tutorial, ensure that you install the LEADTOOLS SDK as well as Docker for Desktop. If you do not have both downloaded, use the below links to access the respective installers.

Prepare Project for use with Docker

Locate the .NET Core Console project folder.

Once you have the .NET Core project you wish to use, rename the project's folder structure to <PROJECT_NAME>\App\<PROJECT_NAME.csproj>. Ensure that you move all the files and folders needed for the project into the App directory where the .csproj is located.

Next in the App folder create a new folder called data, which will contain any external resources needed by the application (i.e. images loaded by the application). Edit your code where it is loading files from local paths and modify it to look into the data folder like so, /app/data/<FILE_NAME>, where <FILE_NAME> is the name of the file you are loading.

If you are setting your LEADTOOLS license from, create a new folder called license inside the App\data directory and place your Leadtools.lic and Leadtools.lic.key files there. Edit your code to have the application look to the following file paths to load your license files:

Creating the Docker File

Navigate to the <PROJECT_NAME> folder that contains the App directory. Create a new file named Dockerfile. This file will contain all the instructions for Docker to build the container. The Dockerfile instructs Docker to install the required dependencies within the container.

Setup Project Files

This section of the Dockerfile will set up the project files to be ran in the container. First, it installs the SDK required by the application (change to match accordingly, information found at hub.docker.com/_microsoft-dotnet-sdk).

Next, it sets the container's current directory to /app and copies all .csproj files in the local App directory to the container's app directory and restores the project.

It copies all the necessary files in the local App and App/data directories to the container's app and /app/out/data directories. Finally, it publishes a release version of the project to the container's app/out directory.

Add the following code to the Dockerfile:

### Building the project (hub.docker.com/_/microsoft-dotnet-sdk) 
 
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env 
WORKDIR /app 
 
# Copy CSPROJ and restore 
 
COPY ./App/*.csproj ./ 
RUN dotnet restore 
 
# Copy everything else and build 
 
COPY ./App ./ 
COPY ./App/data ./out/data 
RUN dotnet publish -c Release -o ./out/ 

Setup Runtime Environment

This next section of the Dockerfile will set up the runtime environment in the container.

First, it installs the runtime required by the application (change to match accordingly, information found at hub.docker.com/_microsoft-dotnet-sdk). It then sets the containers current directory to temp and adds the debian package source to the sources.list file to be able to utilize and pull required dependencies via apt-get. Next, it sets a container environment DEBIAN_FRONTEND to non-interactive to prevent any confirmations or optional inputs to be requested by installing dependencies. Finally, it updates the apt-get and installs the apt-utils and wget to further facilitate installing of the other dependencies.

Add the following code to the Dockerfile:

### Building the runtime image (hub.docker.com/_microsoft-dotnet-runtime) 
 
FROM mcr.microsoft.com/dotnet/core/runtime:3.1 
WORKDIR /temp 
 
# Add package source 
 
RUN echo "deb http://ftp.us.debian.org/debian stretch main contrib" >> /etc/apt/sources.list 
 
# Generate the APT cache 
 
ENV DEBIAN_FRONTEND noninteractive 
RUN apt-get update \ 
    && apt-get install -y apt-utils wget 

Setup LEADTOOLS Dependencies

This section installs the dependencies needed by LEADTOOLS in order to run in the container.

The Microsoft fonts section is only needed for applications with a GUI. The wkhtmltopdf section is needed to process HTML and provide dependencies in license processing. The final line removes any apt lists we generated as they are no longer needed.

Add the following code to the Dockerfile:

# Install the LEAD dependencies (https://www.leadtools.com/help/sdk/v22/main/api/getting-started-with-the-leadtools-linux-libraries-and-demo-projects.html) 
 
RUN apt-get install -y \ 
    uuid-dev uuid-runtime gcc g++ libc-dev-bin \ 
    linux-libc-dev libx11-6 libx11-dev libxt6 \ 
    libxt-dev sqlite3 libsqlite3-dev libfreetype6 
 
# Install Microsoft fonts (http://askubuntu.com/a/25614) 
 
#RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections 
#RUN apt-get install -y \ 
#    fontconfig ttf-mscorefonts-installer 
 
# Install wkhtmltopdf (http://wkhtmltopdf.org) 
RUN wget -q https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb 
RUN apt install -y --allow-unauthenticated \ 
   ./wkhtmltox_0.12.6-1.buster_amd64.deb 
RUN ldconfig 
 
# Clean up APT cache 
 
RUN rm -rf /var/lib/apt/lists/* 

Final Setup

In this final section we do final clean up, final setup, and specify how to initialize the app.

Set the containers current directory to app and remove the temp directory we made above, as it is no longer needed. If your application is a web-based application, uncomment EXPOSE 80 so your application will listen on Port 80. Change the port number as needed. Paste the contents of the /app/out directory in the container that was stored in the buid-env section to the app folder. Lastly, we set the command, dotnet <PROJECT_NAME>.dll, to be ran when we start the container which triggers the application to run.

Add the following code to the Dockerfile:

# Delete the temp files 
WORKDIR /app 
RUN rm -rf /temp 
 
#EXPOSE 80 
 
# Copy and deploy application 
 
COPY --from=build-env /app/out ./ 
ENTRYPOINT [ "dotnet", "<PROJECT_NAME>.dll" ] 

Note

Edit the ENTRYPOINT [ "dotnet", "<PROJECT_NAME>.dll" ] line to where <PROJECT_NAME> is the name of your project dll file.

Create a Docker Ignore File

Create another file in the same directory as the Dockerfile named .dockerignore. This file is used by Docker to ignore specified files and directories. This will reduce the amount of time Docker takes to build the project since it will not have to migrate the entire directory to the container when it is being built.

Add the following into the .dockerignore file:

bin\ 
obj\ 

Run the Project

Build the Docker Container

Open the Command Prompt/Terminal or Windows PowerShell and navigate into your project directory that contains the Dockerfile, using the command below:

cd '<PathToProject>' 

Build and name the Docker container <PROJECT_NAME> all lowercase and one word, using the script below:

docker build -t <PROJECT_NAME> . 

It may take a few minutes for Docker to complete. Once completed, the image can then be run.

Run the Docker Container

Once the Docker image is built, use the following command to run the container:

docker run <PROJECT_NAME> 

Note

You need to wrap any Console.Read lines in your code in a while loop with a break on captured input. Additionally, the Docker application needs to run in interactive mode in order to capture input. See the example below.

Code to capture Enter key to exit:

C#
while (true) 
{ 
    Console.WriteLine("Press (x) to exit..."); 
    var confirmation = Console.ReadLine().Substring(0, 1); 
    if (confirmation.Equals("x", StringComparison.OrdinalIgnoreCase)) 
    { 
        break; 
    } 
} 
Console.WriteLine("Application exited."); 

Docker Run Commands

If your application needs user input, you need to run the command below to allow interaction.

docker run -i <PROJECT_NAME> 

If your application saves a file, you can direct it to a local directory via the command below. This will copy anything in the container's /app/data/output folder to the local C:/Temp directory.

docker run -v /c/temp:/app/data/output <PROJECT_NAME> 

Wrap-up

This tutorial showed how to containerize a .NET Core Console application in a Docker Container.

See Also

Help Version 22.0.2021.10.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.