Docker Containers with .NET Core, Worth it?
.net core and docker
What is .Net Core?
.NET Core is a cross platform, open source re-implementation of the .NET Framework. It is actively maintained by Microsoft and a huge community of developers over on GitHub.
and the main difference between the traditional .net framework is
- cross platform
- open source
read more from here if you want
also you will need .net core, download it from here and fallow the install instructions
you will need visual studio 2019 free community edition also
And, what is Docker?
A Package Software into Standardized Environment for Development, Shipment and Deployment.
You must know that the you can deploy a protect in a controlled environment and share it throughout different places and ensure that it works uniformly in all places, for example, you can deploy your code in .net core with docker in a Linux container but running in a windows machine with visual studio!
The containers virtualize the operating system, not the hardware, or the functionality of a physical computer. Then are, more portable and more efficient.
and of course the docker engine, download from the link below and fallow the install instructions
once you have all installed, you can start to containerize your code
Hello World!
run the fallowing code in your windows power shell or Linux bash, remember that .net core it’s cross platform, so it can run in both.
for practical use we will use windows, then open your power shell and run
dotnet new console -o HelloWorld -n HelloWorld
now a folder with the next structure will appear
HelloWorld
-obj
-HelloWorld.csproj
-Program.cs
now let’s run the simple program doing this
dotnet run
if you did all correctly you should see a Hello World! message in the console.
Now Replace the file with the following code that counts numbers every second:
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
var counter = 0;
var max = args.Length != 0 ? Convert.ToInt32(args[0]) : -1;
while(max == -1 || counter < max)
{
counter++;
Console.WriteLine($"Counter: {counter}");
System.Threading.Tasks.Task.Delay(1000).Wait();
}
}
}
}
and again dotnet run
you will see a counter in your console, hit ctrl+x to stop the program.
and finally
dotnet publish -c Release
you will see the new dll release files, where is your running copy of the time counter.
Let’s Dockerize
now you have your first program working and running we will create a new container and docker it to see the magic!
you can start setting up docker for .net core and for that create a file named Dockerfile in your working folder and add the following line inside
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
COPY HelloWorld/bin/Release/netcoreapp2.1/publish HelloWorld/
ENTRYPOINT ["dotnet", "HelloWorld/HelloWorld.dll"]
and now you must dockerize your program with the next command line
docker build -t myimage -f Dockerfile .
you will see something like this:
use docker images
to see your docker images we will use myimage that we created above and type
docker create
and finally we will see our created image with this command
docker ps -a
The final result must similar to this
in white text you will see the name of the container, it will be used to run the dockerfile in this way
docker start dreamy_ritchy
docker ps
once the project it’s running in a docker container you will see this finally
and with this command you can see it in action running inside a docker container
docker attach --sig-proxy=false dreamy_ritchie
if you miss something you can follow this great tutorial
you can download the full code from link below
Let’s Do Some Magic! ⚡️
at this point it starts to become interesting, because instead share your code and re-complie it again and again in another machine, you will share your container, and run it in different places, for instance, I want to move my code from windows tho a Linux machine and run my application, or from my computer and Azure, you can divide your code in different containers to keep more control over it, does it sound familiar to you? it’s called microservices, keep different containers with different services of your program running isolated from the main core of the program.
Microservices are amazing!
let’s write some code to see it in action with visual studio
Create a Microservice (.NET Core Web API) with Docker support as shown below:
Be secure that tick enable docker support
The following Application Structure will be created along with “Docker File.” and also add Docker Compose to manage all containers
For the MicroService1 you should have
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
ENV ASPNETCORE_URLS http://+:81
EXPOSE 81
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["MicroService1/MicroService1.csproj", "MicroService1/"]
RUN dotnet restore "MicroService1/MicroService1.csproj"
COPY . .
WORKDIR "/src/MicroService1"
RUN dotnet build "MicroService1.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "MicroService1.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .ENTRYPOINT ["dotnet", "MicroService1.dll"]
For the MicroService2 you should have
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
ENV ASPNETCORE_URLS http://+:82
EXPOSE 82
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["MicroService2/MicroService2.csproj", "MicroService2/"]
RUN dotnet restore "MicroService2/MicroService2.csproj"
COPY . .
WORKDIR "/src/MicroService2"
RUN dotnet build "MicroService2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "MicroService2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MicroService2.dll"]
and for MisroService3
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
ENV ASPNETCORE_URLS http://+:83
EXPOSE 83
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["MicroService3/MicroService3.csproj", "MicroService3/"]
RUN dotnet restore "MicroService3/MicroService3.csproj"
COPY . .
WORKDIR "/src/MicroService3"
RUN dotnet build "MicroService3.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "MicroService3.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MicroService3.dll"]
Docker Compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
and finally you must have as the image below 🔽
now you are ready to run the protect, if you did all well, docker compose will dockerize all and run in 3 different micro services like image below
and in the browser now you have your 3 micro services 👊
keep in mind that you can add as many services as you want running in an isolated environment.
you can download the full code from link below
.net Core and Docker gives you a amazing solution to port your projects to a different environments and also gives you the power of separate the solution in parts to work with teams in separate solutions. also gives the power of scale out and up a just a part of the application instead the whole one
some useful links: