Introduction
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.


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
- docker images
- 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.
- docker ps

Mageshwaran RPosted Nov 14, 2019, 11:47 PM
Nice Article...