Our first Dockerized .Net Core app.

--

Today we are going to create a simple Console Hello world in .Net Core 2.1 and we are gonna Dockerize it, if you don’t know what .Net Core is, go to my post where I explain what it is and what it is used for, But you may be asking yourself “What is Docker?” Well no problem, here I introduce you to Docker, and I also guide you across Docker’s installation in Linux Ubuntu, Windows and MAC.

Now, what’s Dockerize?, Dockerize is the action of packing your entire app into an Image (if you are not sure what an Image is on docker, you can visit my post, where I explain what it is) in order to be able to run it in one or multiple containers later on, or push it to Docker Hub and share it with whoever who needs it.

Now let’s start with creating our .Net Core console app (you need .Net Core SDK for this) by opening our preffered terminal, I will be using powershell today:

Lest verify if we have .Net Core properly installed on our pc by writing:

dotnet

Now if we are new to dotnet we can write:

dotnet new

And we will obtain a list of all the templates ready to be initialized as a project:

For this guide we will create a Console Application so first of all go to the directory we will use for the project and then let’s write:

dotnet new console

Some process will be initialized:

Now let’s see if the project is properly initialized, we can go to the path where we created the project, or in this case I will open Visual Studio Code by writing:

code .

Here as you see we have the DotNetDocker.csproj which is a basic XML file where is specified among other things the .Net Core Version, a bin where will be the output if we build the application, and the Program.cs where is the template code:

Now let’s try this to see if it works by writing on the terminal:

dotnet run

It will last a few seconds and then we should see this output:

YAYYYY! we have created our first application on .Net Core, but now I celebrated too loudly and my colleage wants to see if I can run this app on a Linux Ubuntu Machine, we can achieve it, traditionally we should have to:

  1. Get an USB.
  2. Get a Linux Ubuntu ISO.
  3. Boot USB.
  4. Create a partition on our disk.
  5. Install Ubuntu on the partition.

All this to only achieve a hello world? (that’s rough buddy) but we can achieve this much easier thanks to Docker, don’t need to believe me, just keep reading.

First of all we need to create a DockerFile, this file will contain instructions in order to prepare a Docker image which we will run into a container if you don’t know what’s an image in Docker look at this post where I explain it.

So let’s create a new file in VS code by:

It’s important to name the file as “DockerFile” in order to be recognized by VS code:

Now let’s write all instructions we want to be executed by the docker file:

(I know the little whale looks pretty cool)

Don’t be afraid im gonna explain you what every command does:

FROM: every image needs to use a base image to work, without this an image wont work, it’s a Docker rule to start always by using a base image, in some public image repositories in Docker Hub, you will see FROM Scratch, which is literally I will make this image with no base, constructing everything mysef, buy we wont go in depth of it, on this example I will use the Ubuntu image we’ve seen here and we will attatch .Net Core to it, so this image will need to use .Net Core as base image in order to allow us to write dotnet commands once we are inside Ubuntu, we acomplish that by writing microsoft/dotnet.

CMD: this command accepts two parameters, the first one being the command to execute in the terminal, and the second one being the parameter to attatch to that command, we use it to create a directory we will gonna use later.

WORKDIR: works like cd on Windows, stablish the path we are gonna be working on, I use it to go to the directory just created on the last command.

COPY: when we build a DockerFile, sometimes we need to use files which are in our host machine, Docker manage sharing of files between containers and hosts using Volumes, Mounts and Temporary Files all of these sent via terminal, but we need to do it during build time, that’s why we use COPY to copy a host path into a container path, the parameter “.” is used to select the current build context, so if you are in:

C :\Users\IronMan\Development\Docker

and you use “.” as parameter you will copy Docker directory into the path you decided as second parameter.

Now we have a DockerFile with enough instructions to send a message to console, let’s modify the message we send to the console to one more customized:

Open program.cs

Now we are ready to Dockerize our app! Let’s see:

Go back to your terminal and write the following command:

docker build -t ubuntu:ubuntuCoreApp . (don’t forget the point at the end)

In this command we told Docker to build a DockerFile in the current path using the “.” at the final to specify the build context, we tagged the image using the -t flag to the name we wanted (ubuntuCoreApp) and that he have to use ubuntu as base by writing “ubuntu:” suffix.

In this case we have dotnet previously downloaded and Docker recognize it to do not download it again.

If we list the images on our machine by writing:

docker image ls -a

We will see a new image which used ubuntu as base image followed by the containers docker used to build our image, and the tag of our image is ubuntuCoreApp as we specified in the command.

Now is the time to test the image we just created by writing:

docker run -it ubuntu:ubuntuCoreApp bash

You shall see this output:

here we could verify dotnet installation, let’s see:

All worked out, now let’s run our console app as we did it few moments ago:

YAYYY! now we successfully dockerized our console application and my work here is done.

Conclusion: Following this blog you learned the Docker’s basic and what it’s used for, don’t hesitate to start more ambitious projects using this powerful tool like using docker compose to run a complex project, an MVC .Net Core Project, or a .Net Core MVC with REST APIs project, you can learn every one of these in Platzi but no matter which one you choose to afront, i’m sure you will success.

Remember, never give up, and never stop learning.

All the content on this publication has been learned using Platzi, here you can find a lot of courses about what I exposed here and much more, feel free to take a look around it and watch some of their free courses.

I really enjoyed this guide and I’m sure we will meet again if we Keep coding, having fun, and being creative.

--

--