Data Management Global

 View Only

Steps to deploy Db2 .Net Core Applications on Docker Windows Server Core Container

By PrashanthRao Karnam posted Mon March 01, 2021 11:57 PM

  

Steps to deploy Db2 .Net Core Applications on Docker Windows Server Core Container

Microsoft Visual Studio 2017 supports writing applications which can be deployed on Docker.

Docker for Windows is available for install at

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

By default, docker daemon will be pointing to linux containers. Make sure to switch to windows containers by right clicking on Docker icon from the Windows System Tray as shown in below image:

 

To know more about how to write a sample Db2 .Net Core MVC application, please refer  here

Windows Docker Containers are supported only with the IBM Db2 .Net Core Provider package version 3.1.0.100 and can be downloaded from here

Once you have application ready, follow below steps to deploy your application to Docker Windows Server Core Containers:

 

1) Right Click on the project to add the docker support as shown in the below screen shots :

 

 

 

2) A Dockerfile will be created. Now replace the contents of the file with the following.


# escape=`

FROM mcr.microsoft.com/windows/servercore:1809 AS build

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Retrieve .NET Core SDK

ENV DOTNET_SDK_VERSION 2.1.503

RUN Invoke-WebRequest -OutFile dotnet.zip https://dotnetcli.blob.core.windows.net/dotnet/Sdk/$Env:DOTNET_SDK_VERSION/dotnet-sdk-$Env:DOTNET_SDK_VERSION-win-x64.zip; `

    $dotnet_sha512 = '29e44a4d6bd81ace5f7f5b5be946e7fc81325f4563d375d6809150bfc0552c70e07467770c8c6b44127b5b1c01d93ca14e1c98ba527313dc093db8942358760c'; `

    if ((Get-FileHash dotnet.zip -Algorithm sha512).Hash -ne $dotnet_sha512) { `

        Write-Host 'CHECKSUM VERIFICATION FAILED!'; `

        exit 1; `

    }; `

    `

    Expand-Archive dotnet.zip -DestinationPath dotnet; `

    Remove-Item -Force dotnet.zip

RUN setx /M PATH $($Env:PATH + ';C:\dotnet')

WORKDIR /src

COPY ["testdb2aspcoreapp/testdb2aspcoreapp.csproj", "testdb2aspcoreapp/"]

RUN dotnet restore "testdb2aspcoreapp/testdb2aspcoreapp.csproj"

# copy and build everything else

COPY . ./

WORKDIR "/src/testdb2aspcoreapp"

RUN dotnet build testdb2aspcoreapp.csproj -c Release -o /app

FROM build AS publish

RUN dotnet publish -c Release -o /app

# Final Application Deployment image

FROM mcr.microsoft.com/windows/servercore:1809 as final

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

COPY --from=build ["C:/dotnet", "C:/Program Files/dotnet/"]

# In order to set system PATH, ContainerAdministrator must be used

USER ContainerAdministrator

RUN setx /M PATH $($Env:PATH + ';C:\Program Files\dotnet')

USER ContainerUser

WORKDIR /app

COPY --from=publish /app .

# Configure web servers to bind to port 80 when present

ENV ASPNETCORE_URLS=http://+:80 `

    # Enable detection of running in a container

    DOTNET_RUNNING_IN_CONTAINER=true `

    # Enable correct mode for dotnet watch (only mode supported in a container)

    DOTNET_USE_POLLING_FILE_WATCHER=true `

    # Skip extraction of XML docs - generally not useful within an image/container - helps performance

    NUGET_XMLDOC_MODE=skip `

   #set the db2 language which is needed for db2 .net core applications to work on Windows Server Core Docker Containers

    DB2LANG=en_US

#Expose the ports which are mapped between host and container

EXPOSE 80

EXPOSE 63580

EXPOSE 6050

# Trigger first run experience by running arbitrary cmd to populate local package cache

ENTRYPOINT ["dotnet.exe", "testdb2aspcoreapp.dll"]
 
3) Make sure to change the project name and solution name as per your project and solution name accordingly in the DockerFile

4) Now select “Docker” from the deployment target options, the docker commands will be run from the dockerfile and application will be hosted on a docker Windows Server Core container:

To know how to deploy Db2 .Net Core applications on Docker Linux Containers, please refer here


#DataManagementGlobal
#DataServerDrivers
3 comments
33 views

Permalink

Comments

Mon November 27, 2023 05:39 PM

Hi Vishwa,
 
Is there an updated article for this?
 
My .NET 6 app is unable to locate DLL 'db2app64.dll' or one of its dependencies in the Docker Windows container. See the stack trace below. The same issue doesn't occur when running the published build in a native Windows environment. It'll be great if there's an example of how this can work in .NET 6/7. Thanks!
 
System.DllNotFoundException: Unable to load DLL 'db2app64.dll' or one of its 
2023-11-27 15:30:00 dependencies: The specified module could not be found. (0x8007007E)
2023-11-27 15:30:00   at short IBM.Data.Db2.UnsafeNativeMethods.DB232.SQLFreeConnAttribsADONET(ref  
2023-11-27 15:30:00      DB2SQLGetConnAttribsADONETParams pParam)                                   
2023-11-27 15:30:00   at string IBM.Data.Db2.DB2ConnPool.ReplaceConnectionStringParms(DB2Connection 
2023-11-27 15:30:00      connection, string szValue, ref DB2ConnSettings pSettings, ref             
2023-11-27 15:30:00      DB2ConnSettingsInternal pSettingsInternal, bool bAttach, bool              
2023-11-27 15:30:00      pushDownStrAppended)                                                       
2023-11-27 15:30:00   at void IBM.Data.Db2.DB2Connection.set_ConnectionString(string value)         
2023-11-27 15:30:00   at IBM.Data.Db2.DB2Connection..ctor(string connectionString)                  
2023-11-27 15:30:00   at DbConnection IBM.EntityFrameworkCore.Storage.Internal.Db2SqlConnection.    
2023-11-27 15:30:00      CreateDbConnection()                                                       
2023-11-27 15:30:00   at DbConnection Microsoft.EntityFrameworkCore.Storage.RelationalConnection.   
2023-11-27 15:30:00      get_DbConnection()                                                         
2023-11-27 15:30:00   at DbCommand IBM.EntityFrameworkCore.Storage.Db2RelationalCommand.            
2023-11-27 15:30:00      CreateDbCommand(RelationalCommandParameterObject parameterObject, Guid     
2023-11-27 15:30:00      commandId, DbCommandMethod commandMethod)                                  
2023-11-27 15:30:00   at async Task<RelationalDataReader>                                           
2023-11-27 15:30:00      Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(
2023-11-27 15:30:00      RelationalCommandParameterObject parameterObject, CancellationToken        
2023-11-27 15:30:00      cancellationToken)                                                         
2023-11-27 15:30:00   at async Task<bool>                                                           
2023-11-27 15:30:00      Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Asy
2023-11-27 15:30:00      ncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator,             
2023-11-27 15:30:00      CancellationToken cancellationToken)                                       
2023-11-27 15:30:00   at async ValueTask<bool>                                                      
2023-11-27 15:30:00      Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Asy
2023-11-27 15:30:00      ncEnumerator.MoveNextAsync()                                               
2023-11-27 15:30:00   at async Task<List<TSource>>                                                  
2023-11-27 15:30:00      Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.          
2023-11-27 15:30:00      ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken         
2023-11-27 15:30:00      cancellationToken)                                                         
2023-11-27 15:30:00   at async Task<List<TSource>>                                                  
2023-11-27 15:30:00      Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.          
2023-11-27 15:30:00      ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken         
2023-11-27 15:30:00      cancellationToken)
 
Thanks,
Joseph

Tue August 09, 2022 01:01 AM

Hi David,
 yes this is outdated in certain aspects and was written when it was needed to group all relevant information into one place. We are working on an update and will publish the updated document soon.

Thanks
Vishwa

Thu August 04, 2022 03:49 PM

It would appear that this post is out of date. The docker file targets .NET 2 which is no longer supported by Microsoft as of April 2022. The article doesn't seem to suggest any alternatives. Any update would be great. Thank you!