Running Docker on Hyper-V

It has been clear that the obvious choice when running Linux on Windows has been using VirtualBox or VMWare. It’s easier. VirtualBox and VMWare allows you to configure host-to-guest folder sharing without having to configure something called Samba/CIFS and you could get decent video performance.

Then again, many of us running Windows do however use Hyper-V on a regular basis. At least for me the Windows Phone emulators runs on Hyper-V. You could dual-boot your Windows (If this is something you’d like then check out Scott HanselmansSwitch easily between VirtualBox and Hyper-V”), and I did for while. It got tedious. Why can’t we have it all?

Then this product called Docker came along:

Using Docker on Linux we could package up our apps and distribute them. The apps would run inside containers which is sandbox for running a complete virtualized system inside another system. Basically we’re talking about virtualization. The thing that should strike you is the notion of replacing your README.md file with a Dockerfile that describes how to create a ready-to-use image for running your app.

You can then distribute this Dockerfile and let other people build images off of it, or even better, create images and upload them to Dockerhub for everybody (or privately) to pull and use.

I have begun using it, and even though I’ve hit some problems (mostly due to the fact that I run Windows and Hyper-V) I’m excited to use it and start using it to package our web-apps in particular at work.

This post will be a quick guide on how to get Docker up and running using Boot2Docker or from a plain Ubuntu Server installation.

Using Boot2Docker

1. Download the boot2docker ISO file

Boot2Docker comes as an installer (which contains steps for installing VirtualBox, Git, etc), but for this purpose we will just use the ISO.

2. Create a new expanding VHD (VHDX) and name it DockerData-Base.vhdx

We’re going to use this base VHD for our initial setup, and then create a differencing disk later on that uses this Base disk as the parent. In case you want to start over, you can just create a new differencing disk without going through any setup

3. Create a new Virtual Machine

Create a new Generation 1 VM with all the defaults. You can omit the step where you add an ISO to boot from if you like.

4. Attach the boot2docker ISO file and boot the VM

Boot up the VM and see if the ISO works. After few seconds you should be in a shell prompt. Works? Good. Shut it down.

5. Attach the DockerData-Base.vhdx

Now, attach the disk we created in step 2.

6. Boot the VM again

7. Creating storage

The boot2docker image is meant to run off the internal memory, but we need a disk where we can persist docker containers.

At the shell? Good. Execute the following set of commands in the terminal (these are borrowed from this blog post):

You might have to enter a number between 1-4 somewhere in there. Just hit 1 and continue with the instruction set.

One little detail: See that “-L boot2docker-data” argument? Here we are labelling our drive with a specific name. The boot2docker image will check to see if a drive with that label is available and automount it for us. So no need to ever worry about that again.

8. Now turn off the VM again.

Now that we’ve setup our storage unit you can go ahead and turn of the VM again. We’re going to switch our disk with a new one.

9. Detach and create a new differencing disk

Detach the Base disk from our VM, and then head on to create a new disk. This time you’re going to select Differencing instead of Expanding as the type of disk. You can call this one DockerData-Diff if you want.

At the end you have to select a parent disk to use as the base. Fortunately we have that all set up with our DockerData-Base disk. Select that, and boom, you’re done.

 

10. Attach the new DockerData-Diff VHD and boot up. Now you’re ready to go!

It’s that simple. If you run into any problems, I might have missed something. Check out Chris Swan’s blogpost and if you notice anything different then let me know in the comments!

Using Ubuntu Server

This is the approach I’m currently using. Ubuntu is familiar and the package manager is golden. Boot2Docker uses a tiny distro called Tiny Core Linux which is a bit different and quite unfamiliar to me. It’s a whole lot smaller than a Ubuntu installation, so if that’s what you’re looking for then check out the first approach.

1. Create a VM and install Ubuntu Server

Download the ISO from the Ubuntu homepage, create a new virtual machine with a disk and boot from the ISO.

2. Skip networking if you’re not already set up

If you haven’t configured a network adapter then skip the networking and DHCP part.

3. Check the Networking section below for setting up network adapters

4. Install Docker

When all is set up, then we can install Docker:

This command will take care of setting everything up for you. To test that the installation was a success then run:

You might have noticed that Docker after installation gave you some instructions on what to do if you want to run docker without having to do it as root (sudo). In case you missed it, you can add your own user to the docker group:

5. You’re done!

Yep. Have fun! Be sure to check out the Shared Folders section below in case you want to share folders with your containers.

Networking

Boot2Docker

The Boot2Docker image will work with an external virtual switch. Wait, what’s that? An external virtual switch will bridge your internet connection with a new network adapter so that anyone who uses the virtual switch will be on equal footing as the real hardware. You’ll get access from the same router/dhcp server as the one your PC is using.

Head on over to the Virtual Switch Manager and create a new Virtual Switch. Select it to be External, give it a proper name and hit Apply. Attach this virtual switch to your VM and you should be good to go.

Ubuntu Server

For some reason external switches does not work with Ubuntu. Not out-of-the-box at least. The trick you can use here is to create an internal virtual switch and share your internet connection.

share-internet
From the adapter that gives you internet, hit properties, go into Sharing and select to share with the new Virtual Switch that you created

Shared folders

Hyper-V is a more isolated and restrictive environment than VMWare or VirtualBox is, so there’s no concept of shared folders. However we can mount and access Windows shares from our Docker VM. The first thing you need to do is to share a folder. This folder can be restricted to just your user. If the VM has access to the network through an External Virtual Switch or an Internal Virtual Switch you should be able to mount your folder from the docker VM.

 

To be able to mount a windows share from Boot2Docker/Tiny Core Linux we need to install some additional module (This might be included in your image):

Now we can mount the shared folder using the following command

An example of mine:

Sources: http://blog.thestateofme.com/2014/02/18/boot2docker-on-hyper-v/

  • Nice post, bro. Hyper-V is under-utilized, and should be developed more by Microsoft and their Windows team. At least Docker creates some new possibilities for it.

  • messy

    Hi, since I visited this page like more than 10 times I’ve to say thank you for your nice description of how to use docker with Hyper-V. I favor the Boot2Docker solution and I’m quite happy in combination with Dockerfiles. Easy dev set up with one small text file. 😉

    • Henning M. Stephansen

      Thanks!

  • chris strutton

    I need some clarification. Are you running Hyper-v on a windows machine or are you running windows under hyper-v.
    The reason I ask is that we are investigating using docker where I work. IT has given us a windows box (created under Hyper-V) to use. I need to run docker on this machine. I can’t run a linux box directly as corporate frowns on that.
    Does anyone know of any issues with this type of set up. Can it be done? Is it smart to do so?

    • Henning M. Stephansen

      Hi, I am running under Windows. Not sure if you can run virtualization software while being virtualized (Hyper-V under Hyper-V). The best advice I could give right now is to google/bing if it can be done. Not in a position at the moment to check myself.

      • chris strutton

        We have a Windows server 2012 running under Hyper V. On that I want to run Linux inside virtual box or vmware player. On that I want to run docker. This is required as or corporate environment frowns on running Linux. If this works as well as expected we might be able change their minds. I am wondering if there will be any performance issues.

        • Ben

          Chris, did you ever find out if you could do this?

  • Pingback: .NET Core 跨平台 #4, 記憶體管理大考驗 – Docker @ Ubuntu / Boot2Docker – Andrew's Blog()