DEVELOP MICROSERVICES USING .NET CORE & DOCKER

Sonu Kumar
5 min readApr 7, 2020

Guys.. Have you ever wondered how we can create and deploy our app or Micro-service using Docker (Containerize approach) ?

In this article, i am guiding you with very simple. clean and comprehensive manner step by step.

You’ll learn to:

  • Create and publish a simple .NET Core app
  • Create and configure a Dockerfile for .NET Core
  • Build a Docker image
  • Create and run a Docker container

Prerequisites :

. Docker Desktop (For windows)

  • .Net core framework ( we are using VS 2019 )

Creating a simple console app ,build and run it using Docker

Open command prompt (in folder where you want to start)

start executing below commands step by step…

create a new project in a subdirectory named app:

dotnet new console -o app -n myapp

Your folder tree will look like the following:

docker-working

└───app
│ myapp.csproj
│ Program.cs

└───obj
myapp.csproj.nuget.cache
myapp.csproj.nuget.dgspec.json
myapp.csproj.nuget.g.props
myapp.csproj.nuget.g.targets
project.assets.json

The dotnet new command creates a new folder named app and generates a "Hello World" app. Run the command dotnet run. You'll see the following output:

> dotnet run
Hello World!

Publish .NET Core app

.dotnet publish -c Release

This command compiles your app to the publish folder. The path to the publish folder from the working folder should be .\app\bin\Release\netcoreapp3.1\publish\

Create the Dockerfile

The Dockerfile file is used by the docker build command to create a container image. This file is a text file named Dockerfile that doesn't have an extension.

echo dockerfile>Dockerfile

The directory structure of the working folder should look like the following. Some of the deeper-level files and folders have been cut to save space in the article:

docker-working
│ Dockerfile

└───app
│ myapp.csproj
│ Program.cs

├───bin
│ └───Release
│ └───netcoreapp3.1
│ └───publish
│ myapp.deps.json
│ myapp.exe
│ myapp.dll
│ myapp.pdb
│ myapp.runtimeconfig.json

└───obj

notepad Dockerfile

Remove all content in it and paste the below content in docker file and save it:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
COPY bin/Release/netcoreapp3.1/publish/ app/
ENTRYPOINT [“dotnet”, “app/myapp.dll”]

The COPY command tells Docker to copy the specified folder on your computer to a folder in the container. In this example, the publish folder is copied to a folder named app in the container.

The next command, ENTRYPOINT, tells Docker to configure the container to run as an executable. When the container starts, the ENTRYPOINT command runs. When this command ends, the container will automatically stop.

From your terminal, run the following command:

docker build -t myimage -f Dockerfile .

Docker will process each line in the Dockerfile. The . in the docker build command tells Docker to use the current folder to find a Dockerfile. This command builds the image and creates a local repository named myimage that points to that image. After this command finishes, run docker images to see a list of images installed:

> docker build -t myimage -f Dockerfile .
Sending build context to Docker daemon 1.624MB
Step 1/3 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
---> 38db0eb8f648
Step 2/3 : COPY app/bin/Release/netcoreapp3.1/publish/ app/
---> 37873673e468
Step 3/3 : ENTRYPOINT ["dotnet", "app/myapp.dll"]
---> Running in d8deb7b3aa9e
Removing intermediate container d8deb7b3aa9e
---> 0d602ca35c1d
Successfully built 0d602ca35c1d
Successfully tagged myimage:latest

> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myimage latest 0d602ca35c1d 4 seconds ago 346MB
mcr.microsoft.com/dotnet/core/aspnet 3.1 38db0eb8f648 4 wee

Create a container

Docker provides the docker run command to create and run the container as a single command.

docker run myimage

The above command print “Hello world” on console.

Creating a simple Microservice ,build and run it using Docker

Open command prompt (in folder where you want to start)

start executing below commands step by step…

create a new project in a subdirectory named service.Api:

md service.Api

cd service.Api

dotnet new api

Open this project in visual studio and delete the default created weatherforecast controller and class file. Add new api controller named ValueController with read /write option in controller folder as below :

Execute it using solution as below:

Publish .NET Core app

.dotnet publish -c Release

Create the Dockerfile

echo dockerfile>Dockerfile

notepad Dockerfile

— Remove all content in it and paste the below content then save:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
COPY bin/Release/netcoreapp3.1/publish/ app/
ENV ASPNETCORE_URLS http://+:83
EXPOSE 83
ENTRYPOINT [“dotnet”, “app/service.Api.dll”]

Ip address and port mapping

Here the things get a bit tricky. We can configure our container to expose application running inside it to a particular port, generally we run it on default port 80 but we can configure it on 83 as above..

To build the image:

From your terminal, run the following command:

docker build -t myapiimage -f Dockerfile .

To run an application in docker container, we use docker command something like below:

docker run -d -p 8004:83 –name apicontainer1 myapiimage

Here, port 8004 refer to the server port and 83 refers to the container port. So the application running inside the container is exposed at port 83 and it is mapped to server port 8004.

Now the application is running in the apicontainer1 container with the URL http://localhost:8004.

RUN THE SAME IMAGE IN MULTIPLE CONTAINERS

We can run the same image in multiple containers at the same time by using:

docker run -d -p 8002:83 –name apicontainer2 myapiimage

docker run -d -p 8003:83 –name apicontainer3 myapiimage

Check the running containers by using Docker PS.

We can see that there are 3 containers running for the same image at 8002, 8003, and 8004.

--

--

Sonu Kumar

Software Consultant interested in Microservices / Serverless computing, Middleware / SOA, Event Driven Architecture & Machine Learning.