Skip to main content

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

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

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 -
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 -
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 - 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)

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.


Popular posts from this blog

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

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions. Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood. using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope. In the fol

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder. using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token. The following code show to you how you could write the encode and decode methods.