In Focus

Build And Deploy ASP.NET Core WebAPI Using Docker

In this blog, we will create a sample ASP.NET MVC Core Web application and will deploy it on Azure Linux VM using Docker.

Hello everyone.

In this blog, we will create a sample ASP.NET MVC Core Web application and deploy it on Azure Linux VM using Docker.

  1. Create a new Blank Solution.

    Build And Deploy ASP.NET WebAPI Using Docker
  1. Add new project by right-clicking on Solution file. Select ASP.NET Core Web Application.

    Build And Deploy ASP.NET WebAPI Using Docker

    Make sure you have dotnet core 2.1 SDK installed.

    Build And Deploy ASP.NET WebAPI Using Docker
  1. Add a new API Controller called HomeController. We will now add new HTTP methods in this controller.

    Build And Deploy ASP.NET WebAPI Using Docker
  1. Run the application. Navigate to https://localhost:44300/api/home/getallnames. (Localhost port number can be different). You will get an array of names as a result.

  2. Once your API is working fine, push your code to any Git repository. I have used VSTS using Git.

    Build And Deploy ASP.NET WebAPI Using Docker
  1. Now our API is ready to be deployed. Let's navigate to azure.com. (You must have an Azure subscription to perform the below activities).
  1. Go to Virtual Machine.

    1. Click on Add button.
    2. Select Red Hat Enterprise Linux 7.3 

      Build And Deploy ASP.NET WebAPI Using Docker

      Build And Deploy ASP.NET WebAPI Using Docker
  1. Proceed to Next and Select your desired configurations for your VM.
  1. Once your VM creation is completed, navigate to your VM in the Azure portal.

  2. To connect to Linux VM we will need Putty. Download it from https://www.putty.org/.

  3. If you are getting a connectivity error, make sure you have added the inbound rules for that port.

    Build And Deploy ASP.NET WebAPI Using Docker
  1. Install Git to clone your repository using the following command: "sudo yum install git".
  1. Install SCL tool: "sudo yum install scl-utils".
  1. Install Dotnet SDK to compile your source code: "sudo install rh-dotnet21 -y".
  1. Enable rh-dotnet21 so that we can run dotnet from bash . "scl enable rh-dotnet21 bash".
  1. Check if dotnet is installed properly or not. Run the command "dotnet --version". If dotnet is successfully installed you will get the version number.
  1. Make a folder called services and clone your repository inside it. ( command : "mkdir Services" )

    Build And Deploy ASP.NET WebAPI Using Docker
  1. Once your repository is cloned, build your project using the command: "dotnet build". Make sure you are in the solution folder.

    In my case, the project name is DockerDemo.

    Build And Deploy ASP.NET WebAPI Using Docker

    Build And Deploy ASP.NET WebAPI Using Docker
  1. In this path, create a Dockerfile using the command: "vi Dockerfile".

    Copy and paste the below code in this Dockerfile.
    1. FROM microsoft / dotnet: 2.1 - sdk  
    2. WORKDIR / app  
    3. # Copy csproj and restore as distinct layers  
    4. COPY.. / RUN dotnet restore  
    5. # Copy everything  else and build  
    6. COPY.. /   
    7. #WORKDIR / app / DockerDemo  
    8. RUN dotnet publish - c Release - o out  
    9. WORKDIR / app / ToDoWebApi / out  
    10. ENTRYPOINT["dotnet""ToDoWebApi.dll"]  
  1. Next, we need to execute following commands to setup our Docker.
Installing Docker using command  yum install docker
Start Docker service so we can use it  service docker start
    • Once we have installed docker, let's use Docker to build our project.

      Command: "docker build -t dockerdemo."

      If your project is successfully built, docker will create an image for it.
  1. Now, let's run our image using the command: docker run -d -p 8080:80 --name myapp dockerdemo.

    Here we are saying docker that run our dockerdemo image at port 8080 with container name myapp.

    Build And Deploy ASP.NET WebAPI Using Docker
  1. If you want to see all the running containers, "docker ps",

    "docker ps -a" will show you all the container which ran earlier.
  1. So now your WebApi is UP and Running. Let check whether APIs are working as expected or not. Type "curl <port>/api/values". In my case, "curl 0.0.0.0:8080/api/values"

    Build And Deploy ASP.NET WebAPI Using Docker

This is too much manual work, in the next blog, we will try to automate things.

Feel free to share your feedback.

Thank you for reading.