Host a .net webapi on docker on a nano server

How to Have its dotnet application hosted on a docker (on nano server) ?

Install NanoServer

I’ve install nano server on a Hyper-V. And i get the ip address of my container by powershell

(Get-VMNetworkAdapter -VMName myVMName).IpAddresses

But you can install it where you want. Hyper-V, physical Machine, Virtual Box ….

Once it’s installed we will run windows update in our nano server.

Windows Update

$sess = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
Invoke-CimMethod -InputObject $sess -MethodName ApplyApplicableUpdates

Once the updates are installed

Restart-Computer

Install Docker

Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Install-Package -Name docker -ProviderName DockerMsftProvider

once it’s done

Restart-Computer -Force

Now that we have finished to install docker we can test it :

docker run --rm microsoft/sample-dotnet

Access to your NanoServer on Remote

It’s cool to have its own Nano Server BUT, i hate to don’t be able to copy/paste commands, and I don’t want to create all my projects by command line.

Let’s activate the remoting …

Connecting to your Nano Server Instance using PowerShell Remoting

On your Windows local open an elevated PowerShell window.

powershell

Start the WinRM service

net start WinRM

Add your remote Nano Server instance to your TrustedHosts list.

$nanoServerIpAddress = "NANO_SERVER_IP"

(of course you should replace NANO_SERVER_IP, by the ip of your Nano Server)

Set-Item WSMan:\localhost\Client\TrustedHosts "$nanoServerIpAddress" -Concatenate -Force

Connect by powershell :

$nanoServerSession = New-PSSession -ComputerName $nanoServerIpAddress -Credential ~\Administrator
Enter-PSSession $nanoServerSession

Enter the password of the Administrator account of your Nano Server.

You should have your input changed by something like :

[xx.xx.xx.xx]: PS C:\Users\Administrator\Documents>

So now if you run the command dir, you should see that its the content of your Nano Server directory.

Create a Shared Space on your Nano Server

Create a directory on your Nano Server, where you will share the code you will do on your local machine.

mkdir C:\SharedCodeDirectory
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes
net share SharedCodeDirectory=c:\SharedCodeDirectory /GRANT:EVERYONE`,FULL

So now you will be able to see this directory from your windows local typing in an File explorer :

\\<NANO_SERVER_IP>\SharedCodeDirectory

Let’s create our first webapi that run in a docker.

Create a WebApi

I’ve created an empty WebApi :

Then I create a HelloWorldController.cs, with the following code :

[RoutePrefix("api/helloworld")]
public class HelloWorldController : ApiController
{
[HttpGet]
[Route("")]
public string Get()
{
return "Hello, World !";
}
}

After have tested it, I published the code on Deploy Folder. is this code that we will deploy on our Docker.

Create the Dockerfile

I wanted to created a dockerfile FROM microsoft/nanoserver, but nanoserver is just not extensible. I can’t do .net framework 4.7 works on nanoserver, so I used a windowsservercore (with IIS) (4 Go that’s a big container)

FROM microsoft/iis:latest
SHELL ["powershell"]
RUN Install-WindowsFeature NET-Framework-45-ASPNET ; \  
Install-WindowsFeature Web-Asp-Net45
COPY Deploy HelloWorldApi  
RUN Remove-WebSite -Name 'Default Web Site'  
RUN New-Website -Name 'helloworld' -Port 80 \
-PhysicalPath 'C:\HelloWorldApi' -ApplicationPool '.NET v4.5'
EXPOSE 80

When you do that prepare yourself to wait a long time, you need to download somes GB.

Do the magic docker things

docker build -t helloworldwebapidotnet45 .
docker run --name hello -d -p 8000:80 helloworldwebapidotnet45

Then with your browser go to the ip of your nano server on the port 8000. You should see :

Hello, World !

That’s great.

Remarks

I’m really disapointed by nano server. My main goal was to deploy my .NET 4.5 applications in a nano server, on docker. (I don’t want to migrate them all to .net core)

FROM Microsoft/nanoserver

But Nano server on docker just don’t support others framework than .net CORE. (it’s for that I used microsoft/iis 4 Go WTF ?) And I don’t know in what case i can use nanoserver on docker.

Linux has apt-get, yuml, …. to be able to start from a simple linux , and upgrade it, with others component. Nano if it’s not on the starting image you will never be able to import it on nano. Example : If you don’t start a Nano server with IIS inside, you won’t be able later to import it on your docker container (WTF ?)

Links