Containerise dot-net mvc app

In this Article , I will show you how to containerise Microsoft MVC application using docker and kubernetes.

Prerequisite:

1- Install docker for windows or mac.
2- Verify your .net and docker version. If you haven’t installed docker you can download from here.
3- Pull .net images from docker registry.

Step-1 Verify docker version using below cmd.

docker version

Step-2 Verify dotnet version using below cmd.

dotnet --info

Step-3 If dotnet is not install you can search from docker registry by cmd or you can also check from dockerhub portal.

docker search microsoft

From docker hub portal image search : After click on the image you can find more details when it was created . Is it verified image or custom image and how image was built etc. You will also get cmd to install image in details.

Note – Use sdk or runtime dependecy images during the build time but while to production you don’t need all build tools

Step-2 Create dotnet mvc project by below cmd.

dotnet new sln --name dotnetdemo
dotnet new mvc --output dotnetdemodocker

Step-3 Build dotnet project using below cmd

dotnet build

Step-4 run app using below cmd.

dotnet run

Step-5 Let’s containerise app using multi-stage docker file.

Step-6 To specify dependency in containerise app , We need create Nuget file manually and copy below code .

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>

Step-7 Create .dockerignore file . It removes unwanted folders to push to container.

Step-8 Now let’s build and run image locally using below cmd

docker build --tag asifwaquar/dotnetdemodocker:v1 .
docker run --name v1 -p 80:80 asifwaquar/dotnetdemodocker:v1

In above build command we have tagged image with name asifwaquar/dotnetdemodocker and version v1 . In run command we going to run v1 version image on port 80:80 . If we have different environment we can tag those images with different version and run on different port.

Step-8 Let’s push this image to docker hub repository. First we need to login to docker hub account and push our docker image to the repository by using below commands.

docker login
docker push asifwaquar/dotnetdemodocker:v2

Note – If your local image name is different from docker hub remote repo name than you might get access denied error as given below in this case you need to tag your local docker image to remote docker image.

Step-9 Tagging local docker image to remote docker hub repo by using below command.

docker tag local-image:tagname new-repo:tagname
docker push new-repo:tagname

In my case my local image was asifwaquar/dotnetdemodocker:v2 and remote docker hub repo was asif1202 so tag remote repo name as asif1202/dotnetdemodocker:v2 then push remote repo image name to docker hub.

docker tag asifwaquar/dotnetdemodocker:v2 asif1202/dotnetdemodocker:v2
docker push asif1202/dotnetdemodocker:v2

Leave a Reply

Your email address will not be published. Required fields are marked *