Deploying a .netcore application into a docker container


In this blog, I am going to illustrate the deploying of microservices (.net core) using a docker container. The service can be consumed by any font-end like HTML, etc. I will be demonstrating step-by-step how to deploy an ASP.NET Core application and create the docker image and respective container from it. This article is more on docker container implementation rather than implementing ASP.Net core application. Hence, I will not be going into the details of creating a .net core web service. The focus of this article will be on the creation of a docker image and container using the application.

You can download the attached VS projects to start directly from the docker image and container section. You can also refer to my other article, how to build ASP.NET Core application, to build a microservice. Open the power shell and check that the docker is installed in your machine by typing this command, ‘docker version’.

Docker installer: ''.

Let's start with Visual Studio to create a new web service application using .net core.

1. Open VS, go to File > New and select Project > ASP .NET Core Web Application. Select the solution name (e.g. Training service) and the location in your local directory. Click ok.

Once the project is created, you can either take the full attached code or create your own application to deploy it into docker.

2. Check your default docker file created by VS. You can also create your own docker file by mentioning each step to create image with your application. I have modified my docker file here to deploy my XML file which is my database in this example.

  1. FROM AS base  
  2. WORKDIR /app  
  3. EXPOSE 80  
  4. EXPOSE 443  
  6. FROM AS build  
  7. WORKDIR /src  
  8. COPY . TrainingService/  
  9. RUN dotnet restore "TrainingService/TrainingService.csproj"  
  10. COPY . .  
  11. WORKDIR "/src/TrainingService"  
  12. RUN dotnet build "TrainingService.csproj" -c Release -o /app/build  
  14. FROM build AS publish  
  15. RUN dotnet publish "TrainingService.csproj" -c Release -o /app/publish  
  17. FROM base AS final  
  18. WORKDIR /app  
  19. COPY ./Resource/Trainingdata.xml /app  
  20. COPY --from=publish /app/publish .  
  21. ENTRYPOINT ["dotnet""TrainingService.dll"]  

The docker file takes the pre-defined images from Microsoft and builds a new image on top of that using the application. The last step of the docker file is an entry point which executes the given dll in order to start service inside of the container.

Let's build new images using docker and run a container.

3. Go to the power shell and set the current directory where the docker file is present. Execute the below command to build a new image which will depend on the .net core images mentioned in "Step 1" of docker file. Every step defined in docker file is executed in a separate container managed by the docker. You should find the below log when the image is created.

  1. docker build -t yourImagename:tagname .  
  1. 1>Step 1/6 : FROM AS base  
  2. 1> ---> 930743cb4e19  
  3. 1>Step 2/6 : WORKDIR /app  
  4. 1> ---> Using cache  
  5. 1> ---> 2a4a43881115  
  6. 1>Step 3/6 : EXPOSE 80  
  7. 1> ---> Using cache  
  8. 1> ---> 259b306c93c7  
  9. 1>Step 4/6 : EXPOSE 443  
  10. 1> ---> Using cache  
  11. 1> ---> bd850eea4b02  
  12. 1>Step 5/6 : LABEL  
  13. 1> ---> Running in eda0cd31d44c  
  14. 1>Removing intermediate container eda0cd31d44c  
  15. 1> ---> 042e53255110  
  16. 1>Step 6/6 : LABEL  
  17. 1> ---> Running in aab6c185b307  
  18. 1>Removing intermediate container aab6c185b307  
  19. 1> ---> 76f60ecf543b  
  20. 1>Successfully built 76f60ecf543b  
  21. 1>Successfully tagged trainingservice:dev  
Once the image is created, (i.e. trainingservice:dev where dev is the tag name of image), check all images present in your local machine by executing the docker images. 
  1. docker images  
4. Once the image is created successfully, the next step is to create a container from this image.
  1. docker run -p 44382:443 trainingservice:dev  

'Docker run' creates a new container from a given image and -p defined port connectivity from the container to the host machine. Port 443 was exposed to the image mapped to 44382 on the host machine. Check the running container using the below command. 

  1. docker ps  
Once the container is running, you can check the response in JSON format by using this URL 'https://localhost:44382/TrainingService', where the training service is the controller name. This URL can be consumed by any of your front-end applications.
Here, we learned how to deploy microservices developed in .NET Core language using a docker container. In the next article, you will learn how to deploy your application into an Azure container instance using an Azure container registry.