Visual Studio 2019 offers some great help to use Docker with ASP.Net applications on the .Net full framework. The support for Docker improved over the last versions of Visual Studio and is now on a level where you nearly can’t get it wrong. If all is working, you can start your application inside a Docker container with the click of a button:
Prerequisites: You need to install Docker Desktop and set the format of your containers to Windows. You can run .Net core applications on Linux, but the full framework needs Windows.
Select support for Docker with your new applications
The dialog for creating new web applications offers you an option to add support for Docker:
When selected, it creates a Dockerfile and sets everything up to run your application inside a container.
Add Docker support for existing applications
If you already have an application, you can add support for Docker using the Add context menu in the Solution Explorer (the same place you use to add a new class):
This will make the same changes to your project as the new project dialog does.
The Dockerfile
The automatically created Dockerfile starts with the aspnet image from the Microsoft Container Registry and copies your application into the folder /inetpub/wwwroot:
1 2 3 4 5 6 7 8 9 |
#Depending on the operating system of the host machines(s) that #will build or run the containers, the image specified in the #FROM statement may need to be changed. #For more information, please see https://aka.ms/containercompat FROM mcr.microsoft.com/dotnet/framework/aspnet:4.7.2-windowsservercore-ltsc2019 ARG source WORKDIR /inetpub/wwwroot COPY ${source:-obj/Docker/publish} . |
This will run your application inside IIS. In the background everything is setup to support debugging from Visual Studio, therefore you will not notice any difference in your development experience.
The first start will take its time, then Docker will most likely need to download the full 5.7 GB for the .Net 4.7.2 image. This is done once per machine and the subsequent starts will be much faster.
Managing your container
You find your application container in Docker as you do with any other container:
You even can connect to the container using its randomly generated name (musing_wu in my example) and run PowerShell inside:
1 |
docker exec -it musing_wu powershell |
Conclusion
With this level of integration of Docker into Visual Studio 2019 you can make your first steps without much effort. As soon as this works, you can deep dive into Docker and start optimizing the deployment or add other parts of your environment into a container (for example the database).
I was surprised how simple everything is and hope you can use this as a starting point for your own adventures with ASP.NET and Docker.