Create and Run a Sample C# Program Using .NET Core RC2 Image in Docker

You all may be knowing that Microsoft is in the process of rebuilding the .NET framework from the scratch to embrace the rapid advancements happening in the technology world. Last week they released the RC2 version on .NET Core which is vastly imporved from the last release with more APIs and performance improvements. You can read more details about the changes in one of the posts I have blogged earlier here.

In this post I am going to show you how to make use of the Docker container images for .NET Core to create and execute a C# program in Docker. Meaning I am not going to install the framework in my machine, rather host the runtime in a docker container and use it for compiling and running the program.

So one of the pre-requisite for this procedure to work is to have Docker installed in your machine. I am going to use a Windows 10 machine for this example and I have the latest Docker for Windows Beta installed in it. Please read my earlier posts on how to install Docker for Windows Beta in your machine and to troubleshoot the issues which may come up during the installation.

1. Installing Docker For Windows Beta
2. Docker : Troubleshooting Installation Issues

Downloading the Image and Running the Container

The first step is to download the RC2 version of the image from the Docker Repository. You can use the following command in the powershell window for that. It will download the image and runs the container when the download is completed.

docker run -it microsoft/dotnet:latest

It will pull the latest version, that is RC2 in our case from the repository and once it completes the download you will get a bash shell prompt, from where you can execute the CLI commands. You can refer the Microsoft repoository in Docker to know about more options such as downloading a specific version etc.

If you execute the docker ps command in a powershell window, it will list the newly added container as shown below.

Intitializing Code

Now, let's create a folder to save our source code. We will use the mkdir command in the shell prompt to create the directory and cd to go inside the folder

As you can see from the above screenshot that the contents of the folder is empty. Let's use dotnet new command to initialize a sample project in our folder. It will create a C# file called Program.cs and a project.json file in the directory.

To list the contents of the file, you can use the cat command from the prompt

Program.cs

root@43bf75841bf4:/hello_world# cat Program.cs
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

project.json

root@43bf75841bf4:/hello_world# cat project.json
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}

Running the application

In this step we will restore the dependencies using the dotnet restore command to pull the packages mentioned in the project.json file.

Once the download completes, we can compile and execute the program using the dotnet run command.

If you check the contents of the directory, we will now see the bin and debug folders there which has been created by the compile operation

So there it is, we have compiled and executed a C# program from a linux shell without using any of the Windows features, which was an unthinkable scenario sometime ago.


No Comments

Add a Comment