Welcome back to .NET Core Central. Today I am going to write an introductory blog on docker containers. In this blog, I will create a sample .NET Core Console application and run in docker. Very basic implementation with just a Console.Write
statement. In my future blogs, I will dig deeper into some complex implementation and running them in docker.
What is Docker Container?
Docker is a set of services that provides operating system-level virtualization. This virtualization helps deliver software in packages called containers. Containers are isolated from each other and extremely lightweight compared to virtual machines. Hence it is a perfect candidate for hosting microservices.
Docker Container Client for Windows 10
Windows 10 client for Docker is available for download at https://hub.docker.com/. SIgn-Up or Sign-in to download the client.
During installation, keep the default configuration to run as a Linux container, instead of a Windows container. This is to keep the container in line with the production environment.
Testing Docker Installation
Post-installation test and ensure Docker is installed properly.
Firstly, test docker images using command docker images
Secondly, to check the containers use the command docker ps -a
.NET Core Console Application
For creating a .NET Core Console application, open Visual Studio. I am using Visual Studio 2017. Firstly, click on New -> Project -> Console App (.Net Core).
Since this is an example to run in docker. Therefore, I will keep the default implementation of the Program.cs
class. I will change the Console.Write
statement with the string
“Hello Docker World!”.
using System;
namespace Docker.Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Docker World!");
}
}
}
Creating a Docker image for the application
Before I can create a docker image, certainly, I will have to build the image. For building the docker image I will use the docker file. An example of that is available in https://docs.docker.com/engine/examples/dotnetcore/
Firstly, I will create a DockerFile for my solution.
FROM microsoft/dotnet:2.0-runtime AS base
WORKDIR /app
FROM microsoft/dotnet:2.0-sdk AS build
WORKDIR /src
COPY *.sln ./
COPY . /Docker.Demo/Docker.Demo.csproj /Docker.Demo/
RUN dotnet restore
COPY . .
WORKDIR /src/
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app/ .
ENTRYPOINT ["dotnet", "Docker.Demo.dll"]
Secondly, I will execute the following command to create the docker image docker build -t dockerdemo -f DockerFile .
Finally, I will verify if the image is created using the command docker images
Creating the container
Since the image is created, hence I will create a new container using the command docker create dockerdemo
.
Since the image is created, therefore I will verify with command docker ps -a
.
Finally, I will run the container using the command docker start -i cool_engelbart
. Where cool_engelbart
is the name of the container. And I am using the switch -i
to run it in interactive mode. So that I can see the write statement.
Conclusion
Running .NET Core application in docker is a breeze. Most importantly it integrates seamlessly. I will continue my exploration of docker and writing those experiences.
In addition, below is the complete YouTube video: https://youtu.be/6QiR0O5Irzs