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(...

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see...

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provi...