Deploy a docker ASPNET MVC Core application backed by Kubernetes

Net core 2.0 is out and it looks very promising in terms of performance and cross platform support in the micro-services era. See this.

I left dotnet behind because of creepy IIS and the fact that you couldn’t code/run it on non-windows devices. Now it is possible to place ASPNET app behind a reverse proxy and develop and run .Net app and your beloved MAC/Ubuntu. It is the time to say goodbye to Windows-only legacy. This is awesome, isn’t it?

So I decided to give it a try finding out that how easy I can build and deploy a docker app to cloud.

First, create a simple ASPNET MVC Core app.

$ mkdir sample_aspnetmvc 
$ cd sample_aspnetmvc 
$ mkdir sample_aspnetmvc.app 
$ cd sample_aspnetmvc.app 
$ dotnet new mvc 
$ dotnet build 
$ dotnet run

Now If we run curl -I http://localhost:5000 we will get following result.

HTTP/1.1 200 OK 
Date: Wed, 23 Aug 2017 09:58:26 GMT 
Content-Type: text/html; charset=utf-8 
Server: Kestrel

Note: Kestrel is a cross-platform web server for ASP.NET Core based on libuv, a cross-platform asynchronous I/O library. See here and here

Now create a test project

$ cd sample_aspnetmvc 
$ mkdir sample_aspnetmvc.test 
$ cd sample_aspnetmvc.test 
$ dotnet new xunit 
$ dotnet add dotnet add reference 
../sample_aspnetmvc.app/sample_aspnetmvc.app.csproj 
$ dotnet test

The next step is to build a docker image that runs unit tests and the app.

As you might know unlike interpreted languages such as Python or Ruby, C# apps need to be compiled. Also the image should be optimized using aspnetcore-build and aspnetcore images in production. Following dockerfile do the trick.

Note: Create the Dockerfile in the root folder (sample_aspnetmvc)

FROM microsoft/aspnetcore-build AS build-env 

WORKDIR /solution

COPY sample_aspnetmvc.app/*.csproj ./sample_aspnetmvc.app/
RUN ls sample_aspnetmvc.app/ 
RUN dotnet restore ./sample_aspnetmvc.app/sample_aspnetmvc.app.csproj 

COPY sample_aspnetmvc.test/*.csproj ./sample_aspnetmvc.test/
RUN dotnet restore ./sample_aspnetmvc.test/sample_aspnetmvc.test.csproj 

COPY . .
RUN dotnet test ./sample_aspnetmvc.test/sample_aspnetmvc.test.csproj

RUN dotnet publish ./sample_aspnetmvc.app/sample_aspnetmvc.app.csproj --output /out/ --configuration Release

FROM microsoft/aspnetcore
WORKDIR /app 
COPY --from=build-env /out . 
ENTRYPOINT ["dotnet", "sample_aspnetmvc.app.dll"]

Now it is time to build the image and spin a container, it should go through all tests and if units test were successful the image will be built.

$ docker build . -t aspnetmvcapp

Then if you spin a container with this image, your app will be up and running. Hurray!

$ docker run -p 5000:80 aspnetmvcapp

Again we can test it using curl -I http://localhost:5000

Note: Alternatively you can use habitus. You can find a sample build.yml here

We need CI Pipeline

For sure building an image locally isn’t something that we want to do every time we change our code, that’s why we need continuous integration build pipeline to turn the code into container images in a reliable and traceable way. Cloud 66 Skycap is the one here. All we need is to push our code to a git repository and create an account on Cloud 66. Watch this video

Last step is publish the app.

Since we built our image using Skycap, now easily we can deploy our docker application using another product of Cloud 66 named Maestro. Container stacks v2 backed by Kubernetes. Watch this video

Conclusion

Building and deploying docker ASPNET MVC Core app is really easy and straight forward and it can be used in fast-paced development environments, however, in terms of performance, it can’t still compete with Go apps. See this!

Note: You can find full sample here.


Originally published at blog.cloud66.com on August 30, 2017.