Getting Started with Containers for ASP.NET Developers on Windows

worksonmymachineContainers give you a way to run you application in a controlled environment, isolated from other applications running on the machine and from the underlying infrastructure.

It means that when you go to deploy, all the dependencies are published together. So you can finally say, “It worked on my machine” and mean it. All the dependencies with the same versions in your container will be there when you deploy to the cloud.

Microsoft restricts which image you can use with each container type, based on the host OS, as outlined in the following table.

Host OS Windows Server Container Hyper-V Container
Windows Server Full UI Server Core image Nano Server image
Windows Server Core Server Core image Nano Server image
Windows Server Nano Nano Server image Nano Server image
Windows 10 N/A Nano Server image

Note: In this article, you will learn how to deploy on Windows 10, so you will deploy on Windows 10. However, You do not need to choose the container type (Windows Server or Hyper-V) until you’re ready to implement the actual container. The container type has no bearing on how you will ultimately assemble your images.

Set up Containers on Your Developer Comptuer

Here are the steps to get started running ASP.NET Core and ASP.NET applications on Windows containers.

1. Install Prerequisites

You will need to set up your dev computer with the following:

  • Windows 10 Anniversary Update or higher.
  • Visual Studio 2017.
  • Hyper-V and Containers enabled (can be manually enabled from Windows Features).
    containerfeatures
  • Virtualization must be enabled on the machine (Docker will not start if virtualization is not enabled).
    containervirtualization
    This can be verified by checking the ‘Performance” tab on Task Manager.

NOTE:
Use a physical machine, not a VM, for this tutorial. Docker for Windows does not support nested virtualization.

2. Install Docker

The Docker for Windows install package includes everything you need to run Docker on a Windows system.

After installing Docker, logging out of Windows and re-login is required. Docker may prompt for that. After logging in again, Docker starts automatically.

3. Switch Docker to Use Windows Containers

By default, Docker is set to use Linux containers. Type into a command prompt:

docker version

dockerversionlinux

Right-click on the docker tray icon and click Switch to Windows Containers.

containersswitch

Type into a command prompt:

docker version

You will see Windows running as the host.

dockerversionwindows

4. Set up ASP.NET or ASP.NET Core application

You can run ASP.NET or ASP.NET Core applications in containers. You start from one of two kinds of container images available for Windows: Nano Server and Server Core containers. ASP.NET Core apps are lightweight enough that they can run in Nano Server containers. ASP.NET apps need more capabilities and require Server Core containers.

To start, create an ASP.NET or ASP.NET Core Web application, or use an existing one. (Hopefully, you already know how to create an app in Visual Studio 2017. If not, see Create a web app with ASP.NET Core using Visual Studio.)

ASP.NET Core applications developed in Visual Studio can have Docker support automatically added using Visual Studio Tools for Docker.

Docker support with Windows Nano Server can be added at project creation time by checking the “Enable Docker Support” checkbox and selecting Windows in the OS dropdown, or it can be added later on by right-clicking on the project in Solution Explorer, then Add -> Docker Support.

This particular tutorial assumes that “Docker Support” was checked when the project was created in Visual Studio. (If you want to build the files by hand, see Getting Started on Windows Containers.)

dockervisualstudio

Once the project is generate, right-click the docker-compose project, click Build.

Click Docker in the tool tray or Press F5 to Start Debuggingdockerstart

How to add Docker support to an existing project

If you are starting from an existing project, you can add Docker Support by right-clicking on your project, click Add, click Docker Support.

dockeradd

You can then select your Target OS. Your Target OS must match Windows for Docker running Windows or Linux for Docker running Linux.

5. Inspect dockerfile, docker-compose folder

The Web Application template will include the files you need to get started. They include:

  • Dockerfile in the project file.
  • docker-compose folder.

Dockerfile

A Dockerfile is the magic of Docker. It tells how to build the image and put your application into that image.

dockerfile

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Each Dockerfile is a script, composed of various commands (instructions) and arguments listed successively to automatically perform actions on a base image in order to create (or form) a new one. They are used for organizing things and greatly help with deployments by simplifying the process start-to-finish.

FROM directive is probably the most crucial amongst all others for Dockerfiles. It defines the base image to use to start the build process.

WORKDIR sets where the command defined with CMD is to be executed.

EXPOSE associates a specified port to enable networking between the running process inside the container and the outside world (i.e. the host).

ENTRYPOINT sets the concrete default application that is used every time a container is created using the image.

docker-compose folder

A docker-compose.yml file is a YAML file that defines how Docker containers should behave in production.

dockercompose

The docker-compose file comes in several versions. Amd the YAML file is prescribed

And the file format is incredibly flexible. Foe example, you can use Docker Compose to define you deployment in Azure Service Fabric.

Docker Compose tool

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

See Get started with Docker Compose.

6. See your application in your container

You can inspect your application running in the containter. Type docker images into a command prompt.

dockerimages

Note in the images above the differences in size for the ASP.NET vs ASP.NET Core containers: the image size for the ASP.NET container is 11.6GB, and the image size for the ASP.NET Core container is about ten times smaller.

When you run your application from Visual Studio, you’ll be able to debug as usual, but instead of running in localhost, you will see the IP address.

References

CloudDays™ Posts on Containers