Switching between Linux and Windows OS to run your Docker Container

Getting Started with Docker and Microservices in Visual Studio 2017: Part 1

Before you do anything else be sure to install Docker on your local machine. As I am running a Windows machine I installed Docker for Windows. You will also need to enable Hyper-V Virtual Machine manager on your machine – Docker or Visual Studio will probably prompt you to do this anyway.

Once you create a new Application in Visual Studio 2017 (or open existing) all you need to do to add docker support is to:

  • Right click on the project you wish to add to docker
  • Scroll down and click “Add” -> “Docker Support”

Switching between Linux and Windows (Nanoserver) for an Existing .NET Docker project:

When you first set up Docker you will be prompted to choose which of the two OS images you want to install and build your Docker containers (containing your apps) to, Linux or Windows. If you wish to use Linux ensure you go into Hyper-V first and turn on your Linux image that has been added to it by Docker – For me it is called MobyLinuxVM.

If you choose Windows then it will download and use the Windows Nano-server image instead to run your container in. This image however, does not run within Hyper-V – you will not see it listed there – as the Docker for Windows application is able to run that image on your machine itself, assuming it’s a Windows machine. I’m not actually 100% sure how the Docker application is able to host and run an entire OS (even if it is nano) to be honest and I’m still looking up info on it.

Regardless, if your .NET web app is currently running on Windows Nano and you wish to switch to Linux or vice versa you can do this by following this 2 step process

1) in your Dockerfile – change any reference to the nano server, e.g.

FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base

becomes

FROM microsoft/aspnetcore:2.0 AS base

2) Secondly, you must also:

  • Right click on your “docker-compose” project and choose “Properties”
  • Change Target OS from Windows to Linux – you may need to manually type it in

and that’s it, try to debug your docker-compose container in Visual Studio now. It should start up in the browser just like before.