Set up LEADTOOLS Document Service in a Docker Container

This tutorial shows how to set up the LEADTOOLS Document Service in a Docker container.

Overview  
Summary This tutorial covers how to set up the LEADTOOLS Document Service using Docker.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (49 MB)
Platform JavaScript Application using Docker
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

You will need:

Note

Docker has recently changed their licensing policy to: "Docker Desktop remains free for small businesses (fewer than 250 employees and less than $10 million in annual revenue), personal use, education, and non-commercial open source projects."

Setting the License File

The first step is to set up the license file. Open the ServiceHelper.cs file located in ...\DocumentServiceDotNet\src\Tools\Helpers\. Go to line 368 and find the SetLicense function. Replace the SetLicense function with the following code.

C#
public static void SetLicense() 
{ 
    var licString = "[License]\n" + "License = <doc><ver>2.0</ver>`PASTE YOUR LICENSE CONTENTS HERE`</doc>"; 
    var developerKey  = "PASTE YOUR DEVELOPER KEY HERE"; 
    byte[] licBytes = System.Text.Encoding.UTF8.GetBytes(licString); 
    RasterSupport.SetLicense(licBytes, developerKey); 
    IsLicenseChecked = !RasterSupport.KernelExpired; 
} 

The only portion of the LEADTOOLS.lic file that is required for this tutorial is the last line. Copy the entire line and paste it where indicated in the above code.

The LEADTOOLS developer key is in the LEADTOOLS.lic.key file. Copy and paste the entire contents of this file into the developerKey variable as indicated in the above code.

Creating the Docker File

Open the DocumentServicesDotNet folder. Create a new file named Dockerfile. This file will contain all the instructions for Docker to build the Document Service. 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 the files in the local directory to the container's app directory and restores and publishes the DocumentService.csproj found in the core folder into the app/out directory on the container.

Add the following code to the Dockerfile:

### Building the project (hub.docker.com/_/microsoft-dotnet-sdk) 
 
FROM mcr.microsoft.com/dotnet/core/sdk:2.1 AS build-env 
WORKDIR /app 
 
# Copy everything and build 
COPY . ./ 
RUN dotnet restore core/DocumentService.csproj \ 
    && dotnet publish -c Release -o ../out core/DocumentService.csproj 

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-aspnet). 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-aspnet) 
 
FROM mcr.microsoft.com/dotnet/core/aspnet:2.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/v21/main/api/getting-started-with-the-leadtools-linux-libraries-and-demo-projects.html) 
 
    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) 
 
    && echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections \ 
    && apt install -y fontconfig ttf-mscorefonts-installer \ 
 
# Install wkhtmltopdf (http://wkhtmltopdf.org) 
    && wget -q https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.stretch_amd64.deb \ 
    && apt install -y --allow-unauthenticated \ 
    ./wkhtmltox_0.12.6-1.stretch_amd64.deb \ 
    && ldconfig \ 
 
# Clean up APT cache 
 
    && rm -rf /var/lib/apt/lists/* 

Final Setup

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

Set the containers current directory to app and remove the temp directory we made above, as it is no longer needed. Next, it will copy 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, ENTRYPOINT ["dotnet", "DocumentService.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", "DocumentService.dll"] 

Create a Docker Ignore File

Next create a file called .dockerignore. This file is used by Docker to ignore specified documents. This will reduce the amount of time Docker takes to build the project since it will not have to migrate the entire project into the container when it is being built. Copy and paste the following into the .dockerignore file.

core\bin\ 
core\obj\ 
core\.vs\ 
.\vs\ 
.\vscode\ 

Building the Docker Container

Next open the Terminal and migrate to the DocumentServiceDotNet folder. Use the command below:

docker build -t documentservice .

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

Running the Docker Container

Once the Docker image can be run, use the following command:

docker run -p 30000:80 documentservice

The -p flag binds the container port 30000:80 to localhost:30000. Port 30000 is used because the Document Viewer assumes that this port will be available to service its requests.

Go to the browser and navigate to http://localhost:30000. The following page should be displayed.

Document Service Landing Page

Wrap-up

This tutorial showed how to set up the LEADTOOLS Document Service in a Docker Container and run on local host.

See Also

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


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