Welcome to weblogs.com.pk Sign in | Join | Help

Docker on Windows

Docker on Windows

Setting up Docker on Windows is slightly different; as Docker needs Linux kernel and expects certain namespaces for its working. Therefore on Windows; we need to setup a Virtual Machine (VM) as a Docker Host. You can setup any Docker compatible Linux in a VM; boot2docker is a small Linux OS especially made for this purpose. The official way is to use Docker Toolbox; it comes with Docker Engine, Compose, Machine and Kinematic. There is a step by step guide available. It installs Virtual Box and setup boot2docker VM in it.

Docker /w HyperV

I wanted to use HyperV; as I am already using it for other VMs. If you want to use Docker with HyperV; you only need Machine (docker-machine); its a Command Line Interface (CLI) to manage Docker VMs. It lets us create Docker hosts on our computers, cloud providers or remote servers in the data centers. It accomplish this by having a notion of “drivers” and HyperV is supported driver. Get the latest docker-machine binary from its GitHub repository. At the time of this writing its 0.7 and I downloaded x86_64 version; kept it somewhere which was already in my PATH so I can directly call it from anywhere!

Static Ip and Internet Connectivity for Docker VM

The VM for Docker need to have a static ip; docker-machine will generate the certificates for authentication and they are bound to the ip; if Docker VM ip gets changed, we will have to regenerate the certificate every time and it becomes tedious. The Docker VM also need internet connectivity so it can connect to Docker Hub / Registry to download the images on demand. In HyperV; if we have a DHCP server available (Wifi Router scenarios) we can use “External” interface and have the DHCP server assign the static ip bound to the VM’s MAC Address or we can have an internal interface in HyperV and share the internet connection; doing so you will get private ip on the VM and it will have the internet connectivity automatically.


Boot2Docker VM

Once our HyperV switch is ready; we can give docker-machine create command and it will download latest boot2docker.iso and configure a VM all in one go!

C:\Users\khurram>docker-machine create --driver hyperv --hyperv-virtual-switch Docker Boot2Docker
Creating CA: C:\Users\khurram\.docker\machine\certs\ca.pem
Creating client certificate: C:\Users\khurram\.docker\machine\certs\cert.pem
Running pre-create checks...
(Boot2Docker) No default Boot2Docker ISO found locally, downloading the latest release...
(Boot2Docker) Latest release for github.com/boot2docker/boot2docker is v1.11.2
(Boot2Docker) Downloading C:\Users\khurram\.docker\machine\cache\boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v1.11.2/boot2docker.iso...
(Boot2Docker) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(Boot2Docker) Copying C:\Users\khurram\.docker\machine\cache\boot2docker.iso to C:\Users\khurram\.docker\machine\machines\Boot2Docker\boot2docker.iso...
(Boot2Docker) Creating SSH key...
(Boot2Docker) Creating VM...
(Boot2Docker) Using switch "Docker"
(Boot2Docker) Creating VHD
(Boot2Docker) Starting VM...
(Boot2Docker) Waiting for host to start...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env Boot2Docker

Once our Docker VM is running; we can simply SSH into it and run the Container; I am going to run the microsoft/dotnet

C:\Users\khurram>docker-machine ssh Boot2Docker
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
_                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.11.2, build HEAD : a6645c3 - Wed Jun  1 22:59:51 UTC 2016
Docker version 1.11.2, build b9f10c9
docker@Boot2Docker:~$ docker run -it microsoft/dotnet:latest
Unable to find image 'microsoft/dotnet:latest' locally
latest: Pulling from microsoft/dotnet
51f5c6a04d83: Pull complete
a3ed95caeb02: Pull complete
7004cfc6e122: Pull complete
5f37c8a7cfbd: Pull complete
a85114b33970: Pull complete
62c4b050934f: Pull complete
Digest: sha256:7d93320d8be879967149b59ceed280bca70cbdf358a2a990467ca502f0e1a4be
Status: Downloaded newer image for microsoft/dotnet:latest
root@439c959eaa28:/# mkdir hello_world
root@439c959eaa28:/# cd hello_world/
root@439c959eaa28:/hello_world# dotnet new
Created new C# project in /hello_world.
root@439c959eaa28:/hello_world# dotnet restore

And then after some time…

    113 package(s) to /hello_world/project.json
root@439c959eaa28:/hello_world# dotnet run
Project hello_world (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling hello_world for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:03.7668485

Hello World!
root@439c959eaa28:/hello_world# cat /etc/issue
Debian GNU/Linux 8 \n \l

root@439c959eaa28:/hello_world# exit
C:\Users\khurram>docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
microsoft/dotnet    latest              098162c455c7        5 hours ago         576 MB

C:\Users\khurram>docker ps -a
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS                      PORTS               NAMES
439c959eaa28        microsoft/dotnet:latest   "/bin/bash"         2 hours ago         Exited (0) 38 seconds ago                       pedantic_chandrasekhar


Published Saturday, June 4, 2016 7:11 AM by khurram


No Comments

New Comments to this post are disabled