ASP.NET Core 2.0 in Docker

Authored by

TL;DR In this tutorial, you will learn how to create a Docker image with an ASP.NET Core 2.0 application and how to run your application on any Docker host.

Disclaimer

I was looking for a blog post how to create a Docker image with an ASP.NET Core 2.0 application. It was hard to find a blog post that covers the baby steps how to do it. Fortunately, I find a video of Mark Rendle doing a lab of ASP.NET Core 2.0 in Docker where he explains exactly what I was looking for! This blog post is based on Mark Rendle's video but the demo source is my own. This is absolutely my personal experience about what I learn from the lab video.

Prerequisites
Docker Hub

Docker Hub is a cloud-based registry service which allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker Cloud so you can deploy images to your hosts.

It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.

.NET Core 2.0 in Docker

First, we need to go to Docker Hub in order to grab Docker images. Let's start pulling a dotnet 2.0 SDK from Docker Hub using the following Docker CLI :

docker pull microsoft/dotnet:2.0.0-sdk

docker run -ti microsoft/dotnet:2.0.0-sdk

ASP.NET Core 2.0 in Docker

Two Docker images are required to create a Docker image for an ASP.NET Core 2.0 application : microsoft/aspnetcore-build and microsoft/aspnetcore.

docker pull microsoft/aspnetcore:2.0.0

docker pull microsoft/aspnetcore-build:2.0.0

docker images shows all the images on your disk.

dotnet new web -o src/DockerDemo

We use the dotnet CLI to run the DockerDemo :

This is the following result when we open a web browser :

Razor Pages

ASP.NET Core 2.0 version comes with a great new feature : Razor Pages. Razor Pages is less ceremony than MVC and a part of MVC as well. That's why we need to configure MVC middleware in the pipeline. The following line 10 adds the MVC middleware to make it available. Line 15 configures the MVC middleware in the pipeline.

using Microsoft.AspNetCore.Builder;  
using Microsoft.Extensions.DependencyInjection;

namespace DockerDemo  
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();           
        }
    }
}

The creation of Pages folder is a convention to respect in order to configure the routing to Razor Pages. I am using Visual Studio Code to create index.cshtml inside Pages folder. The directive @page is required to define a Razor Pages. Note here @DateTime.Now! which ends with "!" on purpose to show how smart is Razor engine! It detects that DateTime.Now is C# and "!" is a string!

Dockerfile

A Dockerfile is like a recipe for Docker image. ~ Mark Rendle

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. Using docker build users can create an automated build that executes several command-line instructions in succession.

Use multi-stage builds

Multi-stage builds are a new feature requiring Docker 17.05 or higher on the daemon and client. Multistage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain.

FROM microsoft/aspnetcore-build:2.0.0 AS build

WORKDIR /code

COPY . .

RUN dotnet restore

RUN dotnet publish --output /output --configuration Release

FROM microsoft/aspnetcore:2.0.0

COPY --from=build /output /app

WORKDIR /app

ENTRYPOINT [ "dotnet", "DockerDemo.dll" ]
docker build
docker build -t local/demo .

Let's remove the unused data using the following command :

docker system prune

This command will remove all stopped containes, all networks not used by at least one container, all dangling images and all build cache. Before deleting such containes, networks, images or cache, you need to consent it.

Mark used -ti option which means terminal interactive. He also mapped the hosting machine port 5080 with port 80 in the docker container:

docker run -ti -p 5080:80 local/demo

Let's open a browser, type localhost:5080 and voilà!

Summary

In this blog post, we learnt the following points :

  • docker pull to get Images from hub.docker.com

  • docker run to start a container

  • microsoft/aspnetcore and microsoft/aspnetcore-build

  • Dockerfile to define your own image

  • Multi-stage build – two FROM lines

  • docker build to build your own image

Hydrate yourself and happy coding!

Authored by

Maher Jendoubi

Speaker, Microsoft MVP, Blogger, Pluralsight Paris Study Group Leader.

Subscribe to Hi! I'm Maher.

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.