Deploy a docker ASPNET MVC Core application backed by Kubernetes
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
Now create a test project
$ cd sample_aspnetmvc
$ mkdir sample_aspnetmvc.test
$ cd sample_aspnetmvc.test
$ dotnet new xunit
$ dotnet add dotnet add reference
$ 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
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
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
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.
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.