John Galea's Blog

My blog on Gadgets and the like

Windows server 2016 docker containers quick start

Ok let’s start with what are containers? They are basically a light way to compartmentalize applications. The containers instead of replicating the OS the way VMs do, over, and over again, the containers call APIs to get whatever needs to get done from the OS. So they are super light weight. Windows server 2016 added containers and it’s a simple add of a feature:

Then you install docker for windows. There are two versions consumer and enterprise editions CE/EE. At install time for CE you need to choose between wanting to run Windows or Linux Containers. You can switch anytime you like from the docker taskbar. EE can run both. The way Linux containers work is inside HyperV a VM called MobyLinuxVM is created and the containers are then run under that.
Once installed your ready to get started. There’s a list of all readily available containers.

You can also install a series of powershell container commands by running the powershell command:
install-packageprovider containerimage -force
The you get powershell commands like:
install containerimage blah

So let’s get started with a simple windows nano container. The simple command:
docker run -it –network=NAT microsoft/nanoserver
will get you off to the races. You probably want to use the –name option to give a name to the container that makes any sense, and your also probably going to want to use –hostname to give the machine a more memorable name inside the container. All commands are managed by docker. Docker for windows is unique so be careful when googling that your looking at docker for windows. There’s no pretty GUI for docker, so get ready to pretend like your on Unix 🙂 Docker will go and download (for the first time) an image file that will be used by anything that is nano based. So this gives you a Windows command prompt.

By the way, this can also be done on Windows 10.

It’s worth noting the docker run command takes an image, creates a container and starts it. If you keep doing docker runs your going to end up with a bunch of docker containers around. The command below will show you a list of all containers:
docker ps -a
The command below will show the list of all images that have currently been downloaded
docker image ls
The command below will allow you to start a container and connect to it (the -i) (the jibberish numbers are the container ids which you get from docker ps -a command)
docker start -i e710b8182d2b
The command below will show you all currently running containers
docker ps
The command below will allow you to connect to a running container
docker attach 785ceca8c01d
When you exit from the command prompt from nano this shuts down the container. If you connect to the same container more than once, the commands are echoed, ie they are not separate sessions.
The command below allows you to clean up all containers you may have inadvertently created by running instead of starting:
FOR /f “tokens=*” %i IN (‘docker ps -a -q’) DO docker rm %i

Ok woohoo first container. So let’s look at networking. Out of the box Windows creates a NAT network. A NAT creates an internal network that you can talk to the host and get to the internet if you wish. This is assigned by a form for DHCP. So next up would be to get a container on the real network, not NAT. This article tells you all about the different kind of networks available to containers. This Youtiube video I found helpful to fix an issue with my docker network stack. I wanted a transparent so I created a new network inside docker that containers can then use. The command below took care of this for me.
docker network create -d transparent TNET
Magically transparent networks were also created on each of my adapters, which as luck would have it is what I wanted. Once the network is created you can now start a new container on that network using the command:
docker run -it –network=WAN microsoft/nanoserver (Where WAN is the name of my transparent network on the WAN side).
We are getting closer to being useful. I had some issues with the MAC address changing each time I started the container, meaning the IP kept changing. So I used the command below to fix this. I found a mac I could use by noting one it had created before (using ipconfig /all) and then kept it. This will use DHCP on your network.
docker run -it –network=WAN –mac-address=enteramacaddresshere microsoft/nanoserver

So in all the command with all my learning becomes:
docker run -it –network=WAN –hostname=iis-nano-wan –name=iis-nano-wan –mac-address=addyourmacaddress nanoserver/iis

To copy files from the host to the container you can use:
docker cp iis-nano-wan:c:\

Once in the container you can use expand-archive powershell command to extract it!

In Windows you can do Windows containers, or Linux containers but not both at the same time, and this is decided at hyperv install time.

Lots more to learn but this is a good quick start.

June 14, 2018 Posted by | Container stuff | Leave a comment

Using a DSLR camera in a Kayak

Rather than do the same post twice, I thought I’d put a link to my blog post about Using a DSLR camera in a kayak.

June 12, 2018 Posted by | Uncategorized | Leave a comment