This tutorial shows how to set up a Docker container in a .NET 6 Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to Containerize a .NET 6 Console Application using LEADTOOLS with Docker. |
Completion Time | 30 minutes |
Platform | .NET 6 Console Application using Docker |
IDE | Visual Studio 2022 |
Development License | Download LEADTOOLS |
Before working on the Containerize .NET 6 Applications with Docker - C# .NET 6 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.
Locate the .NET 6 Console project you wish to work with.
Many of these projects can be found in the functionality file folders in:
C:\LEADTOOLS23\Examples\<FEATURE>\DotNet
Once you have the .NET 6 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. Ensure the entire project is self contained outside of web resources.
If you are setting your LEADTOOLS license, 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:
/App/data/license/LEADTOOLS.LIC
/App/data/license/LEADTOOLS.LIC.key
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.
This section of the Dockerfile
will set up the project files to be run 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
FROM mcr.microsoft.com/dotnet/sdk:6.0
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/
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). 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 debian:bookworm
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y apt-utils wget
This section installs the dependencies needed by LEADTOOLS in order to run in the container.
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/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
RUN apt-get update && apt-get install -y wkhtmltopdf
RUN rm -rf /var/lib/apt/lists*
For the final step, execute the application. Add the following code to the Dockerfile
:
ENTRYPOINT [ "dotnet", "./out/<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 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\
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.
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
:
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.");
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>
This tutorial showed how to containerize a .NET 6 Console application in a Docker Container.