skip to Main Content

How to run Docker in Windows Subsystem Linux

How To Run Docker In Windows Subsystem Linux

Introduction

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Why Docker?

In the dim and distant past, when a developer wanted to make a change to production code, they may have SSHed onto the server and changed the code manually. (SSH means Secure Shell, and it’s a way to operate network services on an unsecured network.) Of course, there was very little experience and guidance in terms of good testing and good build pipelines for PHP then. Thankfully, this has changed.

Today, development teams expect to be able to test code in production-like environments. Doing this allows teams to deploy code with confidence. For this to happen, local development environments need to be identical (or as close as possible) to the production environments the code runs on.

Virtual machines were a step in this direction. With these, developers can spin up versions of production software on their local machines. VMs work fine, but they’re slow and use a lot of system resources.

Docker fixes these problems and many more. The ability to have isolated environments between projects, test code with different versions easily, and do both these things with a small footprint are only a few of the reasons Docker has won my heart.

Get hands-on experience with the Getting started with Docker tutorial.

Before diving in, you’ll need to make sure that your system has the following:

Prerequisites

  • Windows 10 Version 1803 Build 1734 & up
  • Ubuntu 18.04

Installing Docker-CE

Older versions of Docker were called dockerdocker.io, or docker-engine. If these are installed, uninstall them:

$ sudo apt-get remove docker docker-engine docker.io containerd runc

Now, we will need to setup Docker-CE’srepository in apt-get so we can install a specific version without building it from source ourselves. Following the steps on Docker’s official website, this can be done as follows:

Update the source listing

$ sudo apt-get update

Ensure that you have the binaries needed to fetch repo listing

$ sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

Fetch the repository listing from docker’s site & add it

$ curl -fsSL <a href="https://download.docker.com/linux/ubuntu/gpg">https://download.docker.com/linux/ubuntu/gpg</a> | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] <a href="https://download.docker.com/linux/ubuntu">https://download.docker.com/linux/ubuntu</a> $(lsb_release -cs) stable"

Update source listing now that we’ve added Docker’s repo

$ sudo apt-get update

Next, we install the specific version of Docker we need

$ sudo apt-get install docker-ce

Finally, we need to add your current user to the ‘docker’ group so that you are allowed to interface with the Docker Engine which will be running on your system as root.

$ sudo usermod -aG docker $USER

Verify that Docker Engine is installed correctly by running the hello-world image.

$ docker run --rm hello-world

You will get the error after you run command above

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Error above requires you to set up Docker for Windows and WSL to work Flawlessly.

With a couple of tweaks the WSL (Windows Subsystem for Linux, also known as Bash for Windows) can be used with Docker for Windows.

Configure Docker for Windows (Docker Desktop)

In the general settings, you’ll want to expose the daemon without TLS.

Docker for Windows has been recently renamed to Docker Desktop, so if your settings look slightly different than the screenshot, no worries. It’s the same thing.

This is going to allow your local WSL instance to connect locally to the Docker daemon running within Docker for Windows. The traffic isn’t even leaving your dev box since the daemon is only bound to localhost, so not even other machines on your local network will be able to connect. In other words, it’s very safe for this data to be transmit over plain text.

Configure WSL to Connect to Docker for Windows

The next step is to configure WSL so that it knows how to connect to the remote Docker daemon running in Docker for Windows (remember, it’s listening on port 2375).

Connect to a remote Docker daemon with this 1 liner:

$ echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc &amp;&amp; source ~/.bashrc

That just adds the export line to your .bashrc file so it’s available every time you open your terminal. The source commands reloads your bash configuration so you don’t have to open a new terminal right now for it to take effect.

Verify Everything Works

# You should get a bunch of output about your Docker daemon.
# If you get a permission denied error, close + open your terminal and try again.
docker info
# You should get back your Docker Compose version.
docker-compose --version

You see link screenshot below after you run command below

$ docker run --rm hello-world

Sources

  1. https://medium.com/faun/docker-running-seamlessly-in-windows-subsystem-linux-6ef8412377aa
  2. https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly

This Post Has One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
Search