4th April 2023

Dockerizing .NET Console App

Build React Application

Deploying .NET Console Apps can often be complex and time-consuming, involving manual configuration and setup on various environments. Docker, a popular containerization platform, provides a solution by encapsulating the entire application stack into a single, portable container that can run consistently across different environments. In this blog post, we will delve into how to containerize a .NET Console App, simplifying the deployment process, improving application portability, and reducing dependency issues.

Creating the Console App

First let’s create a console application in visual studio By following the below steps

Select “Create a new project” in the home menu,

To get the list of docker images on your system

Choose “Console App” and click “Next”,

To get the list of docker images on your system

Choose a name for your project, solution and also choose location directory and click “Next”

To get the list of docker images on your system

Go with default chosen “Framework” and click “Create” which will create your consoleApplication.

To get the list of docker images on your system

To test everything is working fine click on the run button in visual studio, If you see “Hello, World!” printed to the console as follows, then you are good to go.

To get the list of docker images on your system

Generating the build files

First you need to generate the build file for the app by following the instructions as follows, Right click on the Application Folder and click “publish”

To get the list of docker images on your system

Choose folder and click next,

To get the list of docker images on your system

Again choose “Folder” again and click “Next”

To get the list of docker images on your system

Go with the default file location and click “Finish”

To get the list of docker images on your system

Now click “Publish”, which will generate build files along with the “.dll” file which we will use to run our app inside the docker container.

To get the list of docker images on your system

Dockerizing the app

To get the list of docker images on your system
                                    
                                        
  FROM mcr.microsoft.com/dotnet/runtime:6.0
  COPY bin/Release/net6.0/publish .
  ENTRYPOINT ["dotnet", "ConsoleApp1.dll"]
    
                                    
                                

Let’s see what each of the above docker commands does in our dockerfile,

FROM - it builds the docker image from the base image “mcr.microsoft.com/dotnet/runtime:6.0” which is available on docker hub

COPY - copies the build files that we generated to the docker container

ENTRYPOINT- specifies the command that we want to run when the container gets created

Now Create a file called “Dockerfile” with no extensions in the root where the “program.cs” file is located and copy and paste the below code. 

Now build the docker image for the app by running the below command in the command line where the project folder is located.

                                    
                                        
    docker build -t dotnet-docker 
    
                                    
                                

Then run the image using the image name as follows,

                                    
                                        
    docker run dotnet-docker
    
                                    
                                

You can see that the docker image has run successfully.

To get the list of docker images on your system

Conclusion

We have successfully containerized the dot net console application and by encapsulating the entire application stack in a single container, you can eliminate dependency issues, streamline deployment, and enhance scalability and reliability.

About Us

  • VS Online Services : Custom Software Development

VS Online Services has been providing custom software development for clients across the globe for many years - especially custom ERP, custom CRM, Innovative Real Estate Solution, Trading Solution, Integration Projects, Business Analytics and our own hyperlocal e-commerce platform vBuy.in and vsEcom.

We have worked with multiple customers to offer customized solutions for both technical and no technical companies. We work closely with the stake holders and provide the best possible result with 100% successful completion To learn more about VS Online Services Custom Software Development Solutions or our product vsEcom please visit our SaaS page, Web App Page, Mobile App Page to know about the solution provided. Have an idea or requirement for digital transformation? please write to us at siva@vsonlineservices.com

Let's develop your ideas into reality