Skip to main content

Run native .NET application in Docker (.NET Framework 4.6.2)

Scope
The main scope of this post is to see how we can run a legacy application written in .NET Framework in Docker.

Context
First of all, let’s define what is a legacy application in our context. By a legacy application we understand an application that runs .NET Framework 3.5 or higher in a production environment where we don’t have any more the people or documentation that would help us to understand what is happening behind the scene.
In this scenarios, you might want to migrate the current solution from a standard environment to Docker. There are many advantages for such a migration, like:

  • Continuous Deployment
  • Testing
  • Isolation
  • Security at container level
  • Versioning Control
  • Environment Standardization

Until now, we didn’t had the possibility to run a .NET application in Docker. With .NET Core, there was support for .NET Core in Docker, but migration from a full .NET framework to .NET Core can be costly and even impossible. Not only because of lack of features, but also because once you modify an existing application phases like V&V (Verification and Validation) can be time consuming.

Docker support on Windows Server 2016
Now, with Windows Server 2016 and native support for containers, things started to change. On Windows Server 2016, we can have a console application that runs native in Docker.
The most important thing at this level is that we don’t need to modify our existing code to run in Docker. Based on what kind of resources we use we might need to map different folders, endpoints or install on the Docker image some applications and services. This are the same steps that we do on a classical system – but in this case we need to configure Docker and not a machine.

Initial Use Case
Let’s take as example a consoler application that is written in .NET 4.6.1. We need to be able to take this application and run in in Docker, without changing a line of code in our console application.

In the above example we have Legacy.Code assembly that runs a custom code in StockCheck class. Legacy.Runner is our console application that calls StockCheck. Nothing special or exotic.

Docker Setup
To be able to do this migration we will need Windows Server 2016. For PoC and playground, I recommend to create an instance of Windows Server 2016 on Azure. You will be able to find an image with Containers.
Once you have a machine with Windows Server 2016, you will need to install and setup Docker. There is a great article that describes all this steps that needs to be done on MSDN - https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/quick_start_windows_server.
You should continue only when you manage to run with success the Microsoft/sample-dotnet image, presented in the above link.

Docker Image for .NET Framework
In Docker Hub, we can find numerous images created by Microsoft, even for .NET Framework (4.6.2, 3.5, ASP.NET and so on). The one that we need for this demo is.NET Framework 4.6.2 - https://hub.docker.com/r/microsoft/dotnet-framework/.
To pull the right image from Docker we need to run the following command:
docker pull microsoft/dotnet-framework
Now, we have the image on our system and we can play with it. If we want we can run we can do this from Power Shell using the following command:
docker run microsoft/dotnet-framework
As we can see in below picture, we run the image. Nothing happen because in this moment we don’t have nothings deployed.

Create a custom image with our code
On GitHub, I shared the base project, that you can use to run this demo - https://github.com/vunvulear/Stuff/tree/master/Azure/RunLegacyDotNetFrameworkUsingDocker. Once you downloaded it, you will need to build the project. To be able to build it with success you will need also Docker Tools for Visual Studio.
At solution folder, you can find Dockerfile.txt. This contains the commands and actions that needs to be executed by Docker to build the image. The first line specifies what image shall be used to create a new image (FROM). The second like specifies to Docker to copy the output of the build to root folder of the image (COPY). The last line specifies the entry point of our image.
Do be able to work you will need to run the below image from the same location where the .sln project is. Otherwise, you need to modify the COPY action to use a valid path from where our application is copied.
docker build -f Dockerfile.txt -t legacy .
Because we added .txt prefix to Dockerfile, we need to specify the input instruction file. ‘-t’ adds a custom tag to our new image. This tag will be used by us later on to run this image. The above commands creates a new image, adding our code to it.

Run our custom image
Now, we have our image created and we are ready to run our new image for the first time.
docker run legacy
Congratulation, you have a legacy application that runs in Docker, using .NET Framework 4.6.2

Useful commands
Inspect image:
docker inspect legacy

Remove docker image legacy:
docker rmi legacy --force

Remove docker container legacy
docker rm legacy

Remove all containers
docker rm $(docker ps -a -q)

Conclusion
For developers it is the perfect moment when we need to look at containers and Dockers. Even if we work with systems that are production and use .NET Framework <=4.6.2, we can still use Docker with success. Based on our needs we might be able to migrate them to containers pretty easily.

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(...

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...