|
| 1 | +# Docker on WSL |
| 2 | + |
| 3 | +If you do not already have Windows Subsystem for Linux (WSL) installed, see [Install Linux on Windows with WSL](https://docs.microsoft.com/en-us/windows/wsl/install) for instructions. |
| 4 | + |
| 5 | +This document describes how to configure WSL 2, install recent version of Ubuntu (24.04 LTS), and install Docker as well as Docker compose. |
| 6 | + |
| 7 | +## Configure WSL settings |
| 8 | + |
| 9 | +Open PowerShell and check current WSL status by running `wsl --status`. |
| 10 | + |
| 11 | +```pwsh |
| 12 | +wsl --status |
| 13 | +``` |
| 14 | + |
| 15 | +If default version is not 2, run `wsl --set-default-version 2`. This enables more recent, virtual machine based WSL, that is required for us to be able to run Docker daemon in the WSL. |
| 16 | + |
| 17 | +```pwsh |
| 18 | +wsl --set-default-version 2 |
| 19 | +``` |
| 20 | + |
| 21 | +If output indicates any problems with WSL 2 Kernel, such as `The WSL 2 kernel file is not found.`, run `wsl --update` and `wsl --shutdown`, as suggested. These command might require admin priviledges. To obtain these, find PowerShell, right click it, and select _Run as System Administrator_. |
| 22 | + |
| 23 | +```pwsh |
| 24 | +wsl --update |
| 25 | +wsl --shutdown |
| 26 | +``` |
| 27 | + |
| 28 | +Run `wsl --status` again. The output should display 2 as default version, recent last update date, and kernel version. |
| 29 | + |
| 30 | +If you already have recent version of Ubuntu installed, ensure that it uses WSL 2 by running `wsl --list --verbose`. If necessary, use `wsl --set-version` to update your instance to use WSL 2. Skip next section. |
| 31 | + |
| 32 | +```pwsh |
| 33 | +wsl --list --verbose |
| 34 | +wsl --set-version "YOUR_DISTRO_HERE" 2 |
| 35 | +``` |
| 36 | + |
| 37 | +## Install recent version of Ubuntu |
| 38 | + |
| 39 | +Install recent version of Ubuntu from either Microsoft Store or PowerShell. These instructions use Ubuntu 22.04. Other distributions might also work, if you know what you are doing. |
| 40 | + |
| 41 | +If you want to install distro through the PowerShell, run `wsl --list --online` and follow the instructions. |
| 42 | + |
| 43 | +```pwsh |
| 44 | +wsl --list --online |
| 45 | +wsl --install -d Ubuntu-24.04 |
| 46 | +``` |
| 47 | + |
| 48 | +If you want to install distro through Microsoft Store: Open Microsoft Store, search for `Ubuntu 22`, select _Ubuntu 22.04 LTS_, and click _Install_. |
| 49 | + |
| 50 | +If not opened automatically, open the installed Ubuntu distro and follow instruction in installation wizard. |
| 51 | + |
| 52 | +## Install Docker |
| 53 | + |
| 54 | +Open Ubuntu you just installed and follow [Install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/) instructions. |
| 55 | + |
| 56 | +__tl;dr:__ Run the convenience script available in `get.docker.com`: |
| 57 | + |
| 58 | +```bash |
| 59 | +curl -fsSL https://get.docker.com -o get-docker.sh |
| 60 | +sudo sh get-docker.sh |
| 61 | +``` |
| 62 | + |
| 63 | +Ignore the `WSL DETECTED: We recommend using Docker Desktop for Windows.` warning. |
| 64 | + |
| 65 | +To be able to run docker commands without `sudo`, add current user to `docker` group with `usermod`. Note that you might have to logout and login for the changes to take effect. |
| 66 | + |
| 67 | +```bash |
| 68 | +sudo usermod -aG docker $USER |
| 69 | +``` |
| 70 | + |
| 71 | +## Configure networking |
| 72 | + |
| 73 | +By default, WSL 2 uses local DNS server to reflect network settings, such as VPN, from the host Windows to WSL instances. The IP address of this DNS server might fall into IP range used by the Dockers default bridge network. In that case, containers will not be able to resolve domains. |
| 74 | + |
| 75 | +To configure working DNS inside containers you can either use public DNS or modify IP range used by Dockers default network. If you are working behind a corporate firewall, public DNS might not work. |
| 76 | + |
| 77 | +=== "Use public DNS in WSL 2" |
| 78 | + |
| 79 | + To use public DNS, you must first disable WSL from automatically generating DNS settings to `/etc/resolv.conf`. To do this, add following rows to `/etc/wsl.conf` file in your WSL instance, for example, by editing (or creating) it with `sudo nano /etc/wsl.conf`: |
| 80 | + |
| 81 | + ```conf |
| 82 | + [network] |
| 83 | + generateResolvConf = false |
| 84 | + ``` |
| 85 | + |
| 86 | + For the changes to take effect, restart WSL by running `wsl --shutdown` in powershell and launching WSL instance again. |
| 87 | + |
| 88 | + ```pwsh |
| 89 | + wsl --shutdown |
| 90 | + ``` |
| 91 | + |
| 92 | + Finally, configure DNS server manually by creating `/etc/resolv.conf` with following content, for example by opening it with `sudo nano /etc/resolv.conf`: |
| 93 | + |
| 94 | + ```conf |
| 95 | + nameserver 8.8.8.8 |
| 96 | + ``` |
| 97 | + |
| 98 | +=== "Configure IP range for Docker" |
| 99 | + |
| 100 | + To configure Docker to use IP ranges that wont overlap with DNS server configured by WSL, add following content to `/etc/docker/daemon.json`, for example, by opening it with `sudo nano /etc/docker/daemon.json`: |
| 101 | + |
| 102 | + ```json |
| 103 | + { |
| 104 | + "bip": "172.21.0.1/16", |
| 105 | + "default-address-pools": [ |
| 106 | + { |
| 107 | + "base":"172.22.0.0/16", |
| 108 | + "size":24 |
| 109 | + } |
| 110 | + ] |
| 111 | + } |
| 112 | + ``` |
| 113 | + |
| 114 | + If you already have dockerd running, you have to restart it for the changes to take effect. |
| 115 | + |
| 116 | + ```bash |
| 117 | + sudo service docker restart |
| 118 | + ``` |
| 119 | + |
| 120 | + If containers cannot resolve domains with above configuration, ensure that the above networks do not overlap with the DNS server configured by WSL by running `ipconfig` in powershell and checking the networks it outputs. |
| 121 | + |
| 122 | +## Start Docker daemon |
| 123 | + |
| 124 | +Start `dockerd` as a background process by using `service` or, alternatively, start `dockerd` directly. |
| 125 | + |
| 126 | +```bash |
| 127 | +sudo service docker start |
| 128 | +``` |
| 129 | + |
| 130 | +Note that this must be done every time WSL is restarted. If `docker` commands print `Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?` error, run `sudo service docker status` command to check if `dockerd` is running and, if necessary, run `sudo service docker start` command again. |
| 131 | + |
| 132 | +If you are working on Windows 11, you can use [boot settings](https://docs.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings) to start Docker daemon automatically on WSL instance startup. To do this, add following rows to `/etc/wsl.conf` file in your WSL instance, for example, by editing (or creating) it with `sudo nano /etc/wsl.conf`. |
| 133 | + |
| 134 | +```conf |
| 135 | +[boot] |
| 136 | +command = service docker start |
| 137 | +``` |
| 138 | + |
| 139 | +## Install Docker compose |
| 140 | + |
| 141 | +Follow [Install Docker Compose](https://docs.docker.com/compose/install/) instructions for Linux. |
| 142 | + |
| 143 | +__tl;dr:__ Install Docker Compose with the distros package manager: |
| 144 | + |
| 145 | +```bash |
| 146 | +sudo apt-get update |
| 147 | +sudo apt-get install docker-compose-plugin |
| 148 | +``` |
0 commit comments