Running Docker containers on Bash on Windows

When the Windows Subsystem for Linux (WSL) – or, as most people even at Microsoft often refer to it – Bash on Ubuntu on Windows – was announced on Microsoft’s Build conference 2016, a world of new tools opened up to us Windows devs. Personally, I love being able to choose between PowerShell, Bash or plain old cmd when I want to script something. And it’s always bugged me that I couldn’t get Docker working from Bash on Windows – until now.

The original title of this post was “Running Docker from Bash on Windows”, but that would have been a slight overstatement. Docker requires access to quite a of lot system calls which aren’t necessarily all implemented on Windows, so getting the engine running under the WSL is probably not so easy. Instead, we’ll run the Docker Engine on Windows, and connect to it from Bash. This also has the advantage that you can start a container from PowerShell and interact with it from Bash, or the other way around – in other words, your computer will still feel just like one machine.

Here’s how to do it:

1. Install Docker on Windows

To install the Docker engine on Windows, just go to docker.com and download the appropriate distribution. Also, make sure hardware virtualization is enabled and Hyper-V is installed, lest the engine won’t start.

Shortcut: Install Windows 10 Creators Update

With Windows 10 Creators Update*, accomplishing all of this has become a lot simpler, since it allows you to run Windows executables from Bash. Just add these two lines to your .bashrc and you’re done!

You can now run docker --version from Bash, and you don’t even have to read the rest of this blog post :)

*) Windows 10 Creators Update is available to Insiders since April 11, 2017, and will be released to the public on April 25, 2017.

Making it work on Windows 10 Anniversary Edition

To install Docker on the WSL, you’ll need to jump through a few more hoops. There’s a description for Ubuntu in general here, which works for the WSL as well, with the exceptions of some of the optional steps. Here’s what I did:

Of course, there’s also the option of downloading and extracting the binaries we’ll need, and put them somewhere in your PATH. There are instructions here for how to get the latest version.

Where did that get us?

We now actually have the Docker engine installed on both Windows and the WSL, but it isn’t started on either. The Windows installer helpfully created a Docker shortcut on the desktop and/or in the Start menu – use that to start the Docker engine. Then, you can try running e.g. docker images from PowerShell and from Bash:

PowerShell:

We haven’t created any images yet, so that’s fine.

Bash:

Clearly unsatisfying. But with one extra step, we’ll get it all working.

2. Connect Docker on WSL to Docker on Windows

Running docker against an engine on a different machine is actually quite easy:

Much better! Finally, to make it permanent, add an alias to add the host option without having to type it every time:

Now, running docker commands from Bash works just like they’re supposed to.

Mission accomplished!


The versions used in this post

Of course, things like these might always have dependencies of the versions of the tools you’re using. In this blog post, I’ve been using the following versions:

Windows
Windows 10 Pro Anniversary Edition (Version 1607, OS Build 14393.1066)
Windows 10 Pro Creators Update (Version 1703, OS Build 15063.138)
WSL Ubuntu
14.04.5 LTS
Docker on Windows
17.03.1-ce, build c6d412e
Docker on Ubuntu
17.03.1-ce, build c6d412e

1 Comment

  1. Hi !

    Thank you for your update with windows Creator’s update, but I would just add something, WSL can launch windows executables but you have to provide the extension, so to launch docker you would have to type docker.exe which is not very practical.

    Just add aliases for docker.exe and docker-compose.exe and you’re done :)

Leave a Reply