Getting started with Docker for Windows

Summary

In this article I'm going to take you through the process of getting up and running with Docker on Windows, as well as telling you a few reasons why I think it might be a good idea. We'll start with a new installation of Windows 10, install Docker for Windows and then pull a trivial Docker image and get it to run. There will be more articles dealing with other aspects of running Docker on Windows, such as creating your own containers (dockerising) and getting a development environment up and running using Microsoft SQL Server for Linux in Docker Engine. But first, we need to install Docker for Windows and this article tells you how to do that.

Background

Ever since building my first PC more than twenty years ago, I've had a tendency to install absolutely everything I might need on my main development machine, despite this obviously being suboptimal. For example, when I wanted to have a look at RabbitMQ a few years ago, I had to install Erlang on my dev machine and then I hardly ever used it, but I don't think I ever uninstalled it (or even disabled it), either. I've also tried running separate virtual machines (VMs) for everything as well, such as SQL Server databases, Domain Controllers, TeamCity installations, etc, etc. Yes, I could have tweaked my startup scripts to disable certain services or to power off certain virtual machines, but I didn't. Sometimes the VM approach might still be a good idea and I do have a nice server lying around on which I might install Azure Stack and then I could do all sorts of cool stuff, but I haven't done it yet and it's definitely a matter for another day.

Now that I've got a new and bargainous laptop (which I think is great, but which is not mega powerful and is mentioned briefly in this post), I thought I would try a new approach.

This time I'm going to install Visual Studio 2017 on my new laptop, but I'm going to try putting everything else in Docker containers and only running them when I need to use them.

Six reasons why I'm using Docker on my new laptop

  1. I don't want to clog up my machine with stuff I'm not using all the time, especially since my new laptop is not of a very high spec.
  2. It's only a development machine, so it doesn't really matter if things break. I'm still of the opinion that persistent databases should not be stored in containers, due to the by-design volatile nature of containers (although I may well be wrong in believing that and the use of docker volumes might fix it).
  3. Docker has one VM and separate environments, rather than multiple VMs. So it is less resource-intensive, which is important for this laptop.
  4. I might use Docker in production for some of these services (not the database itself, as explained above) and doing it this way means I shouldn't ever have the old "It works on my machine" issues of yesteryear.
  5. It's very easy to get other people to help to code a project where they can just grab the code from GitHub and grab a few containers and get up and running on almost any hardware/OS. Gone are the days of people having to use Windows machines to write code in .NET.
  6. It might be fun1.

Installing Docker on Windows

First download Docker for Windows from: https://www.docker.com/docker-windows2

It's called Docker CE meaning Community Edition (which is the free version) and you'll download a file called Docker for Windows Installer.exe. If you're like me, the filename will make you cringe (whitespace, no version). I chose the stable channel as I didn't want to experiment with bleeding edge stuff which might not work right away.

What do you need?

In order to be able to install Docker for Windows, you need the following:

  • 64-bit (x64) version of Windows 10 Pro or Enterprise
  • Ensure hardware virtualisation is turned on in your bios
  • Hyper-V must be enabled
  • You need to login to Windows with a password, as it needs access to networking and Hyper-V stuff. But you should be doing this anyway.

Checking hardware virtualisation is enabled

Check Task Manager → More Details → Performance → and look for Virtualisation: Enabled, like this:

Task Manager showing Virtualisation Enabled

Aside: I like how, if you have installed English (UK), it spells virtualisation the British English way with an s.

If it's not enabled, go into the BIOS and choose: Virtualisation Technology (VTx): Enabled.

Enabling Hyper-V

To enable Hyper-V, press Win, type feat and select Turn Windows features on or off. Enable the Hyper-V checkbox like this:

Enable Hyper-V

As you can see, when you try to enable Hyper-V it won't give you a checkmark, instead giving you a black square which usually means that one or more of the internal features is not selected. Expanding the tree node shows that this is not the case, however. A bit odd. After you've enabled it, you will get the proper (and expected) checkmark.

Hyper-V Enabled

NOTE: You can enable Hyper-V after you install Docker if you forget to do it beforehand.

At this point I should probably have enabled Containers (via Turn Windows features on or off) too, but I forgot. When Docker eventually started, it prompted me to do so (and I did):

Docker Containers Not Enabled

I'm not sure if enabling containers in Windows 10 is necessary only if you want to use Windows containers. I am going to use only Linux containers at first.

When I first tried to run Docker, it gave me an error whilst initialising:

Unable to stop: The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The specified module 'Hyper-V' was not loaded because no valid module file was found in any module directory.  
at <ScriptBlock>, <No file>: line 79  
   at Docker.Backend.HyperV.RunScript(String action, Dictionary`2 parameters) in C:\gopath\src\github.com\docker\pinata\win\src\Docker.Backend\HyperV.cs:line 177
   at Docker.Backend.ContainerEngine.Linux.DoStop() in C:\gopath\src\github.com\docker\pinata\win\src\Docker.Backend\ContainerEngine\Linux.cs:line 295
   at Docker.Backend.ContainerEngine.Linux.Start(Settings settings, String daemonOptions) in C:\gopath\src\github.com\docker\pinata\win\src\Docker.Backend\ContainerEngine\Linux.cs:line 121
   at Docker.Core.Pipe.NamedPipeServer.<>c__DisplayClass9_0.<Register>b__0(Object[] parameters) in C:\gopath\src\github.com\docker\pinata\win\src\Docker.Core\pipe\NamedPipeServer.cs:line 47
   at Docker.Core.Pipe.NamedPipeServer.RunAction(String action, Object[] parameters) in C:\gopath\src\github.com\docker\pinata\win\src\Docker.Core\pipe\NamedPipeServer.cs:line 145

At this point I right-clicked the whale in the system tray, chose Check for Updates... and installed an Upgrade and then Docker was able to start.

Docker Upgrade

Docker Settings
Right-click on the whale in the system tray and choose Settings...

On the Shared Drives tab, let it see the C drive (and possibly other drives, too). Click Apply

Docker Share C Drive

NOTE: If you sign in to your computer using Azure Active Directory then, when you attempt to share the drive, it will prompt you for your credentials and then unshare the drive again. See this companion article of mine to fix that problem. It may be worth noting that shared drives are not necessary if you are using Windows Containers (Docker defaults to using Linux Containers).

On the Advanced tab, set the number of CPUs and RAM. Since my laptop is a bit feeble, I'm trying 2 CPUs and 2GB RAM at first, but will probably bump this up to 4GB RAM (and possibly 3 CPUs) soon.

On the Network tab, make sure your internal subnet address is 10.0.75.0 (unless that's already in use) and that the subnet mask is 255.255.255.0 and automatic DNS server.

If you use a proxy to access the internet, make sure it's set on the Proxies tab.

NOTE: When you right-click the whale in the system tray you will see: Switch to Windows Containers.... This means that you are currently using Linux containers. I have decided to stay with Linux, for no particular reason, so I left this setting.

I've installed Docker, now what?

Now it's time to familiarise yourself with a few basic commands and to pull an image and run it.

Open a PowerShell window in Admin mode (Win+X, A) and run the following three commands:

docker -v shows the version.

docker images shows us all of the images in our local docker repo (there won't be many yet).

docker pull hello-world will grab a simple example container.

Doing all of the above will result in something like this:

Docker pull hello-world

Now let's run the container we just pulled.

docker run hello-world runs it. It's just a sample to show that Docker is installed correctly:

Docker run hello-world

docker ps shows running docker containers. docker ps -a shows all docker containers.

docker rm container-name-or-id will remove the container, but not the image and not if it's running.

docker rmi hello-world will remove the image, but not if it's being referenced by a container. docker rmi hello-world -f will remove the image even if it is being referenced by a container.

Docker rmi force

docker stop hello-world stops it. Can then remove it. Can also use the first few characters of the image id (guid). Check this!

Check out the general documentation at https://docs.docker.com/docker-for-windows/ and the command reference at https://docs.docker.com/engine/reference/commandline/docker/#child-commands

Conclusion

Installing Docker for Windows is easy (AzureAD issues notwithstanding). I think it's worth checking it out, for lots of reasons, including those I listed earlier.

At the time of writing, I'm on version 17.12.0-ce-win46 (15048):
Docker About


  1. And if it isn't, I'll probably stop doing it (and let you know).

  2. Actually it's from here: https://store.docker.com/editions/community/docker-ce-desktop-windows, but you should probably go via the main Docker for Windows page in case that changes.


image credit: me