.NET 4.x app running in a container using Docker
So this blog post comes from doing some work recently to containerize a .Net 4.8 project that needs to run on a Windows container.
I wrote about Windows containers already so this is going to be some other tips to help someone who may have run into the same issues I did.
Lets jump right in, the Dockerfile looks like this:-
So we are using the mcr.microsoft.com/dotnet/framework/sdk:4.8 docker image for building the code, this image has a number of useful tools as part of the image.
Then we copy some files over and restore nuget packages to a packages folder.
We then use msbuild to build the project to an output folder.
On line 12, I am then using another image mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019 for setting up iis as well as copying some files from the build layer into the image which will run the code within IIS.
The contents of startiis.ps1 are as follows:-
So this creeates a new folder for the site, enables IIS remote management which means we can connect to the running container using IIS, and then it creates a new-app pool and website and then finally removes the default iis website.
Note: Now I had some issues trying to web.config transforms and ended up ditching that and ended up creating a config for each environment and switching to the one I need using an environment variable. I also tried Config Builders but I really dont like them as they dont update the actual file they change the values on the fly which isnt not fun when trying to debug anything.
So in the DockerFile I have an entrypoint like so:-
ENTRYPOINT powershell.exe c:/Initialisecontainer.ps1; C:/ServiceMonitor.exe w3svc
This will start the container and inside Initialisecontainer.ps1 you can copy over the web.config with the web.test.config for example.
Tip: to ensure the container keeps IIS running I add the C:/ServiceMonitor.exe w3svc and this works. I tried numerous ways to get the docker container to stay running and went down the rabbit hole here but the following does work.
So now when I have built the container I can run it with this command:-
docker run -d -e ASPNET_Environment=Test <name of my container>
There is many ways to do this and I opted for simplicity over anythingelse.
The main thing is, the setting up of IIS which is pretty easy and then removing the default site which if you deploy to causes IIS to stop and therfore kill your container.
Please reach out if there is a better way, or if you have questions.
Don’t forget to subscribe to my YouTube Channel.
[…] .NET 4.x app running in a container using Docker (Gregor Suttie) […]
[…] .NET 4.x app running in a container using Docker – Gregor Suttie […]
[…] .NET 4.x app running in a container using Docker […]