NET Core Docker 'Hello World'
I suspect there are a lot of folks like myself that have been developing applications on Windows using .NET, and now find themselves wanting to deploy their projects in containers on public infrastructure.
Net Core is a free, open source platform/runtime that I can easily port most of my C# applications to. Being cross-platform, I can host these NET Core applications in a Docker container. I present here some simple instructions for a minimal 'Hello World' application that demonstrates some core concepts in solutions of this type.
I've been using Kestrel a lot lately as the server side component technology for web apps. In this simple example, I am using it like you would use Node.js to spin up a minimal fuss web server.
I'm assuming you have an Ubuntu 16.04 Linux server for these instructions. First, we'll install the NET Core SDK ...
wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.1
We'll also need to install Docker ...
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce
Make a new directory and create a new application ...
mkdir helloworld
cd helloworld
dotnet new console
dotnet add package Microsoft.AspNetCore --version 2.1.1
The add package
command, uses NuGet to install the dependency we need for our Kestrel web server.
Edit Program.cs
to contain these class definitions ...
namespace helloworld
{
public class Startup
{
public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment hostingEnvironment)
{
applicationBuilder.Run(async context =>
{
await context.Response.WriteAsync("Hello World");
});
}
}
class Program
{
static void Main(string[] args)
{
WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.UseKestrel()
.UseUrls("http://0.0.0.0:5050")
.Build()
.Run();
}
}
}
This is the minimal code needed to start a web server on port 5050, and to provide a class, Startup
, that will return the 'Hello World' string.
Net Core has a publish command that creates a folder with all the files you need to distribute your application for a particular OS. To publish the application for Linux we run ...
dotnet publish --runtime=linux-x64
Once, this has completed successfully we need a `dockerfile' definition to build the Docker image ...
FROM microsoft/dotnet:2.1-runtime
COPY ./bin/Debug/netcoreapp2.1/linux-x64/publish/ ./
ENTRYPOINT ["dotnet", "helloworld.dll"]
This simple Docker file uses the Microsoft supplied base image, copies over the published application we made in the previous step, and sets the application we want to use as the entrypoint when the container is instantiated.
The Docker image is built and run using these two commands, respectively ...
docker build . -t helloworld
docker run -p 5050:5050 helloworld
You can then test your application using your favorite web browser (e.g. by using a URL of http://127.0.0.1:5050
on your machine.)