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 Hanselmans “Switch 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:
1 <em>Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments.</em>
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.
1 boot2docker is a lightweight Linux distribution based on Tiny Core Linux made specifically to run Docker containers. It runs completely from RAM, weighs ~27MB and boots in ~5s (YMMV)
1. Download the boot2docker ISO file
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):
1234567891011 sudo fdisk /dev/sdane<enter for default start><enter for deafult end>nl<enter for default start><enter for deafult end>wsudo mkfs.ext4 -L boot2docker-data /dev/sda5
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:
1 $ wget -qO- https://get.docker.com/ | sh
This command will take care of setting everything up for you. To test that the installation was a success then run:
1 $ sudo docker run hello-world
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:
1 $ sudo usermod -aG docker YOURUSERNAME
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.
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.
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.
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):
12 wget http://distro.ibiblio.org/tinycorelinux/5.x/x86/tcz/cifs-utils.tcztce-load -i cifs-utils.tcz
Now we can mount the shared folder using the following command
1 sudo mount -t cifs //HOST-IP-HERE/SharedFolderPath /path/where/we/want/it/mounted -o username=HOST_USERNAME_HERE
An example of mine:
1 sudo mount -t cifs //10.0.0.1/Github /mnt/github -o username=foo