Introduction
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: 'https://docs.docker.com/docker-for-windows/install/'.
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.
- FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
- WORKDIR /app
- EXPOSE 80
- EXPOSE 443
-
- FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
- WORKDIR /src
- COPY . TrainingService/
- RUN dotnet restore "TrainingService/TrainingService.csproj"
- COPY . .
- WORKDIR "/src/TrainingService"
- RUN dotnet build "TrainingService.csproj" -c Release -o /app/build
-
- FROM build AS publish
- RUN dotnet publish "TrainingService.csproj" -c Release -o /app/publish
-
- FROM base AS final
- WORKDIR /app
- COPY ./Resource/Trainingdata.xml /app
- COPY --from=publish /app/publish .
- 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.
- docker build -t yourImagename:tagname .
- 1>Step 1/6 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
- 1> ---> 930743cb4e19
- 1>Step 2/6 : WORKDIR /app
- 1> ---> Using cache
- 1> ---> 2a4a43881115
- 1>Step 3/6 : EXPOSE 80
- 1> ---> Using cache
- 1> ---> 259b306c93c7
- 1>Step 4/6 : EXPOSE 443
- 1> ---> Using cache
- 1> ---> bd850eea4b02
- 1>Step 5/6 : LABEL com.microsoft.created-by=visual-studio
- 1> ---> Running in eda0cd31d44c
- 1>Removing intermediate container eda0cd31d44c
- 1> ---> 042e53255110
- 1>Step 6/6 : LABEL com.microsoft.visual-studio.project-name=TrainingService
- 1> ---> Running in aab6c185b307
- 1>Removing intermediate container aab6c185b307
- 1> ---> 76f60ecf543b
- 1>Successfully built 76f60ecf543b
- 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.
4. Once the image is created successfully, the next step is to create a container from this image.
- 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.
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.
Summary
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.