/ DEVOPS, SYSTEMENGINEER

Deploy Dotnet MVC web

Setting environment

1. Install Dotnet core SDK

https://dotnet.microsoft.com/download/linux-package-manager/sdk-current
Find OS Type and version matches your environment.
In my case, using ubuntu 18.x version


wget -q https://packages.microsoft.com/config/ubuntu/19.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

sudo apt-get update
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-3.0 -y

2. Pull dotnet core image


docker search dotnet
    ## result
    ## microsoft/dotnet                      Official images for .NET Core and ASP.NET Co…   1446          [OK]
    ## ....
docker pull microsoft/dotnet:latest
    ## result
    ## 9a0b0ce99936: Pull complete
    ## db3b6004c61a: Pull complete
    ## f8f075920295: Pull complete
    ## 6ef14aff1139: Extracting [==================================================>]  50.07MB/50.07MB
    ## c05081985e91: Download complete
    ## 6c5e96b85e8c: Download complete
    ## d39c626fbbd1: Download complete

Make Dotnet Project

1. create project


## make solution file
mkdir test
cd test
dotnet new sln

## make mvc project
mkdir test
cd test
dotnet new mvc

2. Local Test

1) Change Listening port [1]

This is just for local test purpose. It is useless setting After deploying as docker container, because docker automatically configure port fowarding setting.


cd ~/test/test
vi Program.cs


public class Program
  {
      public static void Main(string[] args)
      {
          CreateHostBuilder(args).Build().Run();
      }

      public static IHostBuilder CreateHostBuilder(string[] args) =>
          Host.CreateDefaultBuilder(args)
              .ConfigureWebHostDefaults(webBuilder =>
              {
                      webBuilder.UseUrls("http://*:80;http://*:5000;")
                      .UseStartup\<Startup\>();
              });
  }

 

Change the port from 80, 5000 to any number you want

2) Run dotnet project and check connection


cd ~/test/test
dotnet run

If you create project on azure or cloud plateform, You should add firewall rule allowing your selected port.

Type http://(public ip):(your port) on your browser You can see a dotnet default web page. Press ctl+c on your server machine to exit the web service.

Deploy to Docker [2]

3. Make Dockerfile

type “ vi ~/test/Dockerfile “


FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app

## copy csproj and restore as distinct layers
#  COPY *.sln .
COPY *.csproj ./test/
RUN dotnet restore

## copy everything else and build app
COPY . ./test/
WORKDIR /app/test
RUN dotnet publish -c Release -o out


FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY --from=build /app/test/out ./
ENTRYPOINT ["dotnet", "test.dll"]

If you want to use another project name, replace “test” to “Your Project Name”. [3]

4. Run in a Linux container

Go to directory that has Dockerfile, Build and Deploy.


cd ~/test/
## Build dotnet core web by Dockerfile
docker build -t test .

## Run on background
## Port fowarding : Local 5000 port -> container 80 port
## Replace 5000 to another port you want 
docker run -it --rm -p 5000:80 --name test_docker_aspnet test -d

You can connect web page by Public IP address on 5000 port [4]


  1. reference: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-3.0#server-urls ↩︎
  2. reference: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.0 ↩︎
  3. reference: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.0#the-dockerfile-1 ↩︎
  4. reference: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.0#run-in-a-linux-container ↩︎