A blog about tech & coding

Use Docker on Windows using WSL 1 and VirtualBox

By Marco on Tue Aug 13 20193 min readhow to

Update 29/05/2020: Microsoft released the Windows 10 May 2020 update, which includes WSL2. WSL2 uses virtualization to run a Linux kernel in a lightweight VM. This allows you to install Docker natively in WSL. For developers, this is a far better experience.


In 2016 Microsoft released Windows Subsystem for Linux, which gives you a full-featured Linux terminal. However, it doesn't come with a Linux kernel (which WSL2 will do), which means that Docker won't work. However, there are workarounds available. In this article, I will explain my own personal setup that makes Docker work on WSL flawlessly by using a VirtualBox virtual machine.

Steps to be taken

  1. Create a virtual machine with Ubuntu 18.04 LTS
  2. Install Docker on the virtual machine and modify the Docker service file to expose the API over TCP
  3. Install Docker on WSL and set environment variable DOCKER_HOST to connect to remote Docker API on your VM

Prerequisites

Before we start, please make sure you have have the following installed:

  • Windows Subsystem for Linux running on Ubuntu 18.04 LTS
  • VirtualBox 6

Create a virtual machine with Ubuntu 18.04 LTS

First we start by creating a virtual machine with Ubuntu 18.04 LTS that will host the Docker API.

  1. Start VirtualBox and create a new virtual machine by clicking "New" button. Give your VM a name that is easy to recognize. Assign at least 2048MB ram, and leave all other settings on default.
  2. Download the Ubuntu 18.04 LTS ISO.
  3. Right-click the virtual machine, click on Settings, then click on Storage and click on the label "Empty" with the CD icon in front of it. Then click on the CD icon on the right next to "Optical Drive" and select the Ubuntu ISO.
  4. While still in the Settings pane, click Network and then Adapter 2. Enable the network adapter and make sure it is attached to Host-only Adapter.
  5. Start the virtual machine and install Ubuntu.
  6. Modify /etc/netplan/01-netcfg.yaml to enable the use of Adapter by adding an entry for adapter enp0s8. The file should look like this after editting:
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes
    enp0s8:
      dhcp4: yes
      optional: true
01-netcfg.yaml

Now run sudo netplan apply and retrieve the IP address of your second adapter by running the ip a command. Your IP address should be listed in the block of enp0s8. Example:

3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:36:2b:51 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.102/24 brd 192.168.56.255 scope global dynamic enp0s8
       valid_lft 1042sec preferred_lft 1042sec
    inet6 fe80::a00:27ff:fe36:2b51/64 scope link
       valid_lft forever preferred_lft forever

In this case, the IP address of the virtual machine is 192.168.56.102.

Install Docker on the virtual machine

  1. After the virtual machine has been installed, install Docker according to the instructions on the Docker website.
  2. Adjust the Docker service definition to expose the Docker API over TCP. Start the editor by entering the following command sudo systemctl edit --full docker.
  3. Add the following argument to the line starting with ExecStart: -H tcp://0.0.0.0:2375 . The line starting with ExecStart should now look like this:
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
Docker service file

Now make sure to execute systemctl restart docker to make sure Docker starts with the API exposed over port 2375.

Install Docker on WSL

  1. Open WSL
  2. Use the same instructions to install Docker as we used for installing Docker on the virtual machine.
  3. Execute echo "export DOCKER_HOST=tcp://<IP of virtual machine>:2375" >> ~/.bashrc && source ~/.bashrc to set the DOCKER_HOST environment variable to the IP address of your virtual machine by default. Don't forget to add the IP address in this command!
  4. Now you should be good to. Run docker run hello-world to make sure everything is working correctly.

Wrap-up

That's it! Now you can use Docker on WSL seamlessly. Other things you can do to make your life even more easier:

  1. Start the VM in in headless mode so you don't always have an icon for the VM in your taskbar. Right-click the VM, hover over start and click on Headless Start.
  2. You can even install Docker Compose by following the one-line install command on the Docker documentation.

Tags

Set Python 3 as default in Ubuntu 20.04 LTS Focal Fossa

As you might know, starting from Ubuntu 20.04 LTS, Python 2 is no longer a dependency and not installed by default anymore. However, there are still some caveats.

Thu Apr 30 2020

Check if gzip support is enabled on your webserver

Gzip is a file format used for file compression and decompression. It is commonly used in HTTP compression to speed up websites. However, this compression technique can be exploited using the BREACH attack. For security reasons, it may be beneficial to disable gzip compression. However, it is not always easy to determine whether gzip support is enabled or disabled. The following curl command provides a simple command-line solution to check whether your webserver supports gzip compression: $ cu

Wed Apr 11 2018