How to Use Docker & Visual Studio for Your ASP.NET Applications on the .Net Framework

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:

Visual Studio run button is changed to Docker

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:

You can select Docker support with new projects

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

For existing apps, use Add / Docker support from the context menu of the Solution Explorer

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:

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:

run docker container ls to see your container

You even can connect to the container using its randomly generated name (musing_wu in my example) and run PowerShell inside:

 

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.

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.