Create Docker Image for Simple Web Application and Hosting using Visual Studio .NET Core

Introduction

 
Creating a simple docker image for .NET Core web application and running it into the local machine as well as in the Azure container service. 
 
Before going through this article, please go through the other articles regarding docker and related areas for basic concepts. 
 
Prelinguistic
 
You need to install the docker for windows and .Net Core SDK in your machine:
  • https://docs.docker.com/docker-for-windows/install/
  • https://dotnet.microsoft.com/download/dotnet-core/2.2 
Step 1
 
Open Visual Studio 2019 and create a new project. 
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Step 2
 
Select ASP.Net Core Web Application --> Click Next --> Provide project name. 
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
Step 3
 
Click Create and then select .Net Core as a framework and select .Net Core 2.2 . Next, Select Web Application in the left side panel. In the right-side panel, select Enable docker support and make sure the windows option is selected. This docker image is windows-specific. 
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
Note
 
If you are missed to select docker for windows support while creating an application, don't worry, you have an alternate option. Right-click on the project in solution explorer and select Add --> there you can see docker support.
 
Step 4
 
Once the sample project is created, you can visit DockerFile in the solution explorer of the created project. There you can find a few commands, which states that what are the steps to be followed to create an image of this application. You can also see .dockerignore file in project solution explorer. It is acting like a git ignore file, ignoring a few files while creating a docker image.
 
For Example:
  1. # Get base sdk from microsoft  
  2. #Copy the CSPROJ and any dependencies via nuget  
  3.   
  4. //FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-nanoserver-1809 AS base  
  5. //WORKDIR /app  
  6. //EXPOSE 80 ---> For Port mapping with container.  
  7.  
  8. #Copy our project files and build our release  
  9.   
  10. //FROM mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-1809 AS build  
  11. //WORKDIR /src  
  12. //COPY ["dockerdemowebapp/dockerdemowebapp.csproj", "dockerdemowebapp/"]  
  13. //RUN dotnet restore "dockerdemowebapp/dockerdemowebapp.csproj"  
  14. //COPY . .  
  15. //WORKDIR "/src/dockerdemowebapp"  
  16. //RUN dotnet build "dockerdemowebapp.csproj" -c Release -o /app  
  17.   
  18. //FROM build AS publish  
  19. //RUN dotnet publish "dockerdemowebapp.csproj" -c Release -o /app  
  20.   
  21. //FROM base AS final  
  22. //WORKDIR /app  
  23. //COPY --from=publish /app .  
  24. //ENTRYPOINT ["dotnet", "dockerdemowebapp.dll"  
Step 5
 
Finally, our application contains the following files.
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Step 6
 
Let's start with creating a docker image, so to do that open command prompt on project file location. Which means the below folder location:
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Step 7
 
In opened command prompt enter the following command to create an image. docker build -t dockerhubid/projectname: latest .
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Command: docker build -t dockerhubid/projectname: latest.
 
So in our case command is like, docker build -t dockerhubid/dockerdemowebapp .
 
-t --> means tagging this image with the following name.
dockerhubid --> https://hub.docker.com/ // Here you can create docker hubid.
dockerhubid/dockerdemowebapp --> You can give any name in that place but when you try to host this docker container into Azure or any other cloud service means you should move this docker container image into docker hub and then only you can deploy.
 
By projectname, it means the full name that we have given.
  
Finally we have . (dot) symbol which is important with space. We have to follow the docker principle.
 
Note
In the command part, you can see the latest keyword. It is not mandatory, if not provided, it will take the latest version.
 
Step 8
 
When you hit enter, it will try to read the docker file and execute the commands which we have written. Let see!!
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Step 9
 
OOPS, something went wrong, it is because the docker file is not in the correct place. Open the file explorer and navigate into the project location. Move the dockerfile one step backward, which means move the docker file into solution file location and open the command prompt on that location. Like in the below image. 
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Step 10
 
Now enter the build command and wait for executing the docker file commands (which means dependencies and all other related). Once the command is successful, you can see the success window.
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Step 11
 
Once the docker image has been created successfully, you can enter the below command to see the images.
 
command: docker images
 
There you can see your recently created images.
 
Step 12
 
So, we have created the docker image. Now we need to run this image locally. To do that we need a container to run this image. This process can be achieved by running the below command.
 
command: docker run -p 9090:80 dockerhubid/projectname
-p --> It means port mapping.
9090 --> It is our random port number to run our application (We can give our own).
80 --> This one is we exposed 80 port while creating an image. So that port number we have to map with container that we created now.
 
So let see by running the command in our case.
 
 Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Step 13
 
Success, now our application is running as a docker image in this URL.
 
http://localhost:9090/
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Step 14
 
That's it !!! We have created a docker image and run it in our local machine.
 
Now we need to deploy it into Azure. To do that, we need a docker image in a distributed environment. This was we can easily deploy docker image from there into azure container services.
 
So we can move our docker image into dockerhub repository. To do that execute the following command.
 
command: docker push dockerhubid/projectname
 
When you run this command in our command prompt, it looks like below:
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Once you've pushed the docker image into the docker hub, you can visit the docker hub login and see your repository as shown below.
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Note
You can make this repository public or private. If it is private, then you have to provide the username and credentials while fetching this image from azure service. So for now, we will make it as public. (Click the repository, go to settings and make it public)
 
Step 15
 
To deploy into Azure service:
  1. Need an azure portal subscription.
  2. Needs to create an azure container instance.
Step 16
  1. Open https://portal.azure.com in a new tab and enter your credentials.
  2. Then navigate to add a resource section --> type 'container instance'.
  3. Create a new container instance resource like below:
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
Note
Image names should be the same as we gave while creating the docker image.
 
Imagename: dockerhubid/projectname. Based on this docker hub id, it will fetch and get the image and deploy.
 
Step 17
 
In the Azure container service networking tab, you need to provide some DNS name to run our application.
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
That's it, now you can review + create. It will deploy our application in azure container service.
 
So, once deployment success, then go to the resource and pickup the DNS name and tab in browser tab.
 
Create Docker Image For Simple Web Application And Hosting Using Visual Studio .NET Core
 
That is it, our application is running in azure service. I hope this was helpful for you. Enjoy ;).
 
A few commands in Docker
 
docker version - Version
docker info - Provides information about installed docker
docker ps - List of containers
docker images - List of images
docker start containerid - Starts the given container
docker stop containerid - Stop the given container
 
Reference:
 
https://www.youtube.com/watch?v=f0lMGPB10bM