How to Code and Host a dotnet Core WebApi on a Linux Container

I want to code a .net Core WebApi on Ubuntu with Visual Studio Code. And Host it on a docker.

So let’s start (you can find the source on github :

Install Visual Studio Code

We need to get Visual Studio Code

It’s easy, go to Download Visual Studio Code .deb and install it.

We will need to code in .Net to get the extension C#

Install dotnet core

We need to install dotnet core

You have all information here

For my Ubuntu 16.04 I’ve done :

sudo sh -c 'echo "deb [arch=amd64] xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-key adv --keyserver --recv-keys 417A0893
sudo apt-get update
sudo apt-get install dotnet-dev-1.0.0-preview2-003131

type the command dotnet to check if all went well.

dotnet --help

Our first webapi coded on ubuntu

So first create a directory where you will code your first web api on ubuntu

mkdir FirstWebApi
cd FirstWebApi

Then we will create our dotnet solution.

dotnet new

Open thanks to Visual Studio Code your FirstWebApi folder. On project.json we will need to add the dependencies to work with AspNetCore and Kestrel (for those who don’t know Kestrel is a web server on ubuntu instead of using IIS).

"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0"

on Program.cs we will say to use Kestrel and search for all config on Startup.cs

var host = new WebHostBuilder()

I’ve also changed the namespace to put a more funy one (but it’s really not needed),

So now, we need to create the Startup.cs file and put inside the configuration :

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace HelloUniverse
public class Startup
public void ConfigureServices(IServiceCollection services)
        public void Configure(IApplicationBuilder app)

Now We are ready to code our Api Controller. I’ve added a new Folder called Controller, and inside i’ve create a file HelloWorldController.cs

using Microsoft.AspNetCore.Mvc;
namespace HelloUniverse
public class HelloWorldController : Controller
// GET api/HelloWorld
public string Get()
return "Hello, world!";

Yeah I know it’s very simple.

there are many ways to setup Kestrel But i prefer to open the project.json file and add

"web" : "Microsoft.AspNetCore.Server.Kestrel --server.urls"


dotnet restore
dotnet build

you should see :

So if all is OK, let’s roll :

dotnet run

If all is oki, he will tell you that he is listening on http://localhost:5000 So let’s try :

curl -i http://localhost:5000/api/helloworld

you should get :

HTTP/1.1 200 OK
Date: ############# the date #########
Transfert-Encoding: chunked
Content-Type: text/plain; charset=utf-8
Server: Kestrel
Hello, world !

Have you seen Kestrel :D

Dockerize it.

If you don’t have docker installed on your ubuntu install it please

On the parent folder of FirstWebApi create a dockerfile

FROM microsoft/dotnet:latest
COPY ./FirstWebApi /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
ENTRYPOINT ["dotnet", "run"]

then do the docker magic thing :

docker build -t dotnetcoretest .
docker run -it -d -p 5000:5000 dotnetcoretest

let’s test :

curl -i http://localhost:5000/api/helloworld

Yeah Hello, world !


dotnet core is on preview. So there are missing all the tooling. Kestrel need more tooling too.

So if i have to do a real project maybe i will think to put nginx in front and kestrel behind, to benefit of the tooling of nginx.

And Visual Studio Code without templating is a pain (we can use yeoman), but without a performant templating tools it will be hard to code in ubuntu.

Anyway I think that dotnet core will be a performant language We only need to wait a little bit (6 months, 1 year) to have the tooling and a bigger community doing a lot of good projects.


Like what you read? Give Felipe Dutra Tine e Silva a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.