Data Management Global

 View Only

Creating Linux based Docker applications using Db2 Data Providers for .NET Core and .NET 5.0

By Hosathota Vishwanatha posted Fri July 24, 2020 03:59 AM

  

Microsoft supports developing applications which runs on Docker starting from Visual Studio 2017. Docker for Windows is available for install at

https://docs.docker.com/docker-for-windows/install/

 

This article explains how to create a simple Console application using IBM Data Server Driver for .NET Core or .NET 5.0 and run the application on Docker for Linux. For applications using Db2 .NET Core with Docker Windows Server Core container, please refer to here.

Open Visual Studio (VS 2019 or VS 2019 preview) and create new  Console application from Start-> New -> Project -> Installed -> Visual C# -> .NET Core -> Console App(.NET Core). For .NET 5, choose .net5.0 as target framework. Name the project as DockerTest.

 

NOTE : Remember this path since we need to open a Power Shell command prompt for compiling and running the application from the same path.

 

Once the project is created, right click on the project Dependencies and add Db2 .NET Core 3.1 or Db2 .NET 5.0 provider package.

 

Next, add Docker support by right clicking on the project and doing a Add-> New->Docker Support

 

When prompted, select Linux as the required Operating System.

 

Note : On  some docker environments, one of the dependency file libxml2.so.2 may be missing. In that case, the docker file should have commands to install the libxml2.so.2 as shown in below example.


Next, modify the content of the DockerFile in the DockerTest as follows:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#For .NET 5.0
FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
##For .NET Core 3.1
#FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base

WORKDIR /app

#incase of any library loading issues, enabled the next line to collect more information
#ENV LD_DEBUG=bindings:symbols

#Make sure the path is set to location where clidriver is present. For .NET Core, the path will vary
ENV LD_LIBRARY_PATH="/app/clidriver/lib"
#ENV LD_LIBRARY_PATH="/app/bin/x64/Debug/net5.0/clidriver/lib"

#install the dependency library libxml2
RUN apt-get -y update && apt-get install -y libxml2
#On some systems as found out by Jason McCombie, additional dependency installation may be needed as described in below links
#https://www.ibm.com/docs/en/db2/11.5?topic=running-restrictions-linking-libdb2so
#https://www.ibm.com/docs/en/opw/8.0.0?topic=solutions-libdb2so-cannot-be-loaded
apt-get install -y pam x86_64
apt-get install -y pam.i686
#On Red Hat
#yum install -y pam x86_64
#yum install -y pam.i686

#For .NET Core 3.1 it will be FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src

COPY . ./
RUN dotnet restore "./DockerTest.csproj"

RUN dotnet build "DockerTest.csproj" -c Release -o /app/build
#RUN dotnet build "DockerTest.csproj" -c Debug -o /app/build

FROM build AS publish
RUN dotnet publish "DockerTest.csproj" -c Release -o /app/publish
#RUN dotnet publish "DockerTest.csproj" -c Debug -o /app/publish

##For applications targeting IBM database servers i.e. Db2 for z/OS or Db2 for i, a Db2 Connect license is needed.
##There are two licensing options available:
##1. Server based license -
##The easiest and recommended way is to use the server based licensing of Db2 Connect where the
## license is installed in the Db2 for z/OS or Db2 for i database servers that allows unlimited number of clients/applications/users to connect.

##2. Client/ user-based license -
##User-based licensing of Db2 Connect requires the license file to be copied explicitly to clidriver/license folder
##NuGet cache is one of the locations to keep the license file.
##Application output directory is another location but each time the application is built, the file may have to copied explicitly.
##Adding the license file as part of the project and copying it to <output directory>/clidriver/license/ as part of build is also an option.
##Note: db2consv_ee.lic is the license file for the user based Db2 Connect Enterprise Edition.
COPY ./db2consv_ee.lic /app/publish/clidriver/license/db2consv_ee.lic

FROM base AS final
WORKDIR /app

COPY --from=publish /app/publish .

Env PATH=$PATH:"/app/bin/x64/Release/net5.0/clidriver/bin:/app/bin/x64/Release/net5.0/clidriver/lib"
#Env PATH=$PATH:"/app/bin/x64/Debug/net5.0/clidriver/bin:/app/bin/x64/Debug/net5.0/clidriver/lib"

ENTRYPOINT ["dotnet", "DockerTest.dll"]

Open a Power Shell command prompt with Administrative Privileges by doing a right click and using Run As Administrator option.

Browse to the folder where the docker file of the DockerTest is present and execute the following command to build\

      docker build -t DockerTest .

Notice the dot at the end of above command.

 

If successfully built, execute the following command to run the DockerTest

       docker run DockerTest

 

We should see a "Hello World!" as the output of this sample.


We can now try adding some IBM Data Provider specific code to test connectivity to a database.

 //using IBM.Data.DB2.Core; //For .NET Core
using IBM.Data.Db2; //For .NET 5

....

 try

      {

            Console.WriteLine("Opening the connection");

            DB2Connection connection = new DB2Connection("server=<myserver>:<portnumber>;uid=<dbuserid>;pwd=<mypwd>;database=sample");

            connection.Open();

            Console.WriteLine("Connection opened");                          

        }

        catch(Exception dbException)

        {

             Console.WriteLine("Exception: "+ dbException.Message);

         }

 

Repeat the build and run command from Shell script as explained earlier and test the outcome.

​​​​​​​​​​​​​
Last updated on 1st of August 2022.​​​​
#DataManagementGlobal
#DataServerDrivers
5 comments
125 views

Permalink

Comments

Mon August 28, 2023 12:55 PM

If I have a trial license db2consv_t.lic, can I also test, deploying to DockerFile with Linux or Azure AKS?

Thu June 10, 2021 09:24 AM

last updated on 10th June 2021 to include changes needed to work with Release mode of the application.

Sat November 21, 2020 07:12 AM

Hi Gargi,
 the packages are self contained and separate installation of GSKit is not needed. Are you facing any issue with SSL?

Thanks
Vishwa

Thu November 12, 2020 02:17 PM

Will this work with SSL where we need to supply arm files. Do we need to install GS KIT ?

Wed July 29, 2020 09:34 AM

The link to the windows container blog is broken.