Friday, December 24, 2021

.NET Full Framework app deployment to docker container

+



Today I am going to share how you can deploy a .NET application (web application) in docker. The benefit of using containerization of application is build once and deploy any for any environment. As we know most of the modern application are configuration file driven where as base code remain almost across the environment(DEV, UAT and PROD). So while deploying the code container if you can replace the config file then you are good to deploy in any environment by build once. Usually code are build in dev environment and build artifact got promoted to higher environment. What docker do, it virtually segregate at Operating system level and allow you to multiple container(think as a small virtual machine) and allow to deploy code. Any point of the application life cycle you can create and destroy container. In this article mostly I will be showing how to get a base windows images from cloud docker repository and deploy to AWS ECS.


What you need

  1. Windows 10 machine with Hyper V enabled.

  2. Visual Studio installed in your machine --> Just to create any web application.

  3. Docker demon installed in you local machine-> Just to push the build artifact to docker and run.
    You can use Docker desktop to run docker container.

Following are the URL to download the software

https://visualstudio.microsoft.com/downloads/
https://docs.docker.com/desktop/windows/install/

How do you Containerize a .NET application?


Step-1 : Create a .NET Full Framework application and publish it.
Step-2 : You need have base image in which you are going to deploy your code. The base image contains all the necessary run time and required base dlls to run your application. Base image can be pulled from public repository or private repository. Mostly Organization keep the docker base images in their ow private repository.
Step-3 : Create docker file which tells the docker demon how to deploy your code in to the base images.
Step-4 : run the docker container.

Docker file Sample

##### Sample Docker file for dotnet core app-linux container deployment #########


FROM <URL-TO-YOUR-DOCKER-BASE-IMAGE-REPOSITORY>
WORKDIR App
COPY <SOURCE-PATH-TO-LOCAL-MACHINE> <DESTINATIN-PATH-IN-DOCKER>
COPY ["appsettings.json","."]
ENV ASPNETCORE_URLS http://*:8090
EXPOSE 8090
ENV ASPNETCORE_ENVIRONMENT "Development"
ENTRYPOINT ["dotnet" ,"dotnet-core-dll-name.dll"]

########### Sample Docker file for .NET Full Framework app- windows container #####

FROM <URL-TO-YOUR-DOCKER-BASE-IMAGE-REPOSITORY>
RUN MKDIR <website-dir-name-in-container>
COPY ["source-directory-path","<C:\website-dir-name-in-container>"]
COPY ["Web.config","<C:\website-dir-name-in-container>"]
RUN COPY-ITEM <C:\\website-dir-name-in-container> -Destination c:\\inetpub\wwwroot\\<website-dir-name-in-container> -Recurse
RUN New-WebSite - TestWebsite -Port 80 -PhysicalPath c:\\inetpub\wwwroot\\<website-dir-name-in-container>
RUN New-WebBinding -Name "TestBinding" -IP "*"; -Post 443 Protocol https;$domaname='*.cert-domain-name';$sitename='TestWebSite';$dnsname=$domainname;$newCert=New-SelfSignedCertificate -DnsName $dnsname - CertStoreLocation cert:\LocalMachine\My;$binding=Get-WebBinding -Name $sitename -Protocol "https";$binding.AddSSlCertificate($newCert.GetCertHashString(),My);
EXPOSE 80
EXPOSE 443

Few Basic Docker command to push the application to container and run it,

1. docker pull <url-to-repository>:<docker-image-name> , to pull base docker image from repository.


2. docker build -t <name-of-modified-image> -f <path-to-docker-file> to build the container image using above base image.


3. docker run --name <container-name> -d-rm -it -p 8000:80 <name-of-modified-image> to run the container from image


How docker container push work with Jenkins?

1. Docker cli has to be configured at the windows or Linux node based upon your preferred build node.
2. From pipeline script or groovy script run the above command as bat or sh.

No comments:

Post a Comment