Skip to content

Commit e136dd0

Browse files
authored
Update docker-on-wsl instructions (#11)
Fixes #3.
1 parent f41d230 commit e136dd0

2 files changed

Lines changed: 76 additions & 46 deletions

File tree

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Docker on WSL
1+
# Install Docker on Windows Subsystem for Linux (WSL)
22

33
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.
44

@@ -34,28 +34,44 @@ wsl --list --verbose
3434
wsl --set-version "YOUR_DISTRO_HERE" 2
3535
```
3636

37+
??? bug "WSL commands only output usage instructions"
38+
39+
If the `wsl` command outputs usage instructions instead of the expected output regardless of given parameters, ensure that your system has following Windows features enabled:
40+
41+
- Windows Subsystem for Linux
42+
- Virtual Machine Platform
43+
- Hyper-V
44+
45+
To do this, open the _Turn Windows Features on or off_ dialog from _Start_ menu, Settings, or Control Panel, and ensure that the features mentioned above are selected.
46+
3747
## Install recent version of Ubuntu
3848

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.
49+
Install recent version of Ubuntu from either Microsoft Store or PowerShell. These instructions use Ubuntu 24.04. Other distributions might also work, if you know what you are doing.
4050

41-
If you want to install distro through the PowerShell, run `wsl --list --online` and follow the instructions.
51+
=== "PowerShell"
4252

43-
```pwsh
44-
wsl --list --online
45-
wsl --install -d Ubuntu-24.04
46-
```
53+
If you want to install distro through the PowerShell, run `wsl --list --online` and follow the instructions:
54+
55+
```pwsh
56+
wsl --list --online
57+
wsl --install -d Ubuntu-24.04
58+
```
59+
60+
=== "Microsoft Store"
61+
62+
If you want to install distro through Microsoft Store: Open Microsoft Store, search for `Ubuntu 24`, select _Ubuntu 24.04 LTS_, and click _Install_.
4763

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_.
64+
---
4965

50-
If not opened automatically, open the installed Ubuntu distro and follow instruction in installation wizard.
66+
If not launched automatically, open the installed Ubuntu distro (e.g., by clicking it in the _Start_ menu) and follow instruction in installation wizard. You will have to choose an username and a password to use in the WSL instance. To make your life easier, use only lowercase letters and numbers in your Linux username.
5167

5268
## Install Docker
5369

5470
Open Ubuntu you just installed and follow [Install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/) instructions.
5571

5672
__tl;dr:__ Run the convenience script available in `get.docker.com`:
5773

58-
```bash
74+
```sh
5975
curl -fsSL https://get.docker.com -o get-docker.sh
6076
sudo sh get-docker.sh
6177
```
@@ -64,21 +80,45 @@ Ignore the `WSL DETECTED: We recommend using Docker Desktop for Windows.` warnin
6480

6581
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.
6682

67-
```bash
83+
```sh
6884
sudo usermod -aG docker $USER
6985
```
7086

7187
## Configure networking
7288

7389
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.
7490

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.
91+
To configure working DNS inside containers you can either modify IP range used by Dockers default network or use public DNS. If you are working behind a corporate firewall, public DNS might not work.
92+
93+
=== "Configure IP range for Docker"
94+
95+
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`:
96+
97+
```json title="/etc/docker/daemon.json"
98+
{
99+
"bip": "172.21.0.1/16",
100+
"default-address-pools": [
101+
{
102+
"base":"172.22.0.0/16",
103+
"size":24
104+
}
105+
]
106+
}
107+
```
108+
109+
If you already have dockerd running, you have to restart it for the changes to take effect.
110+
111+
```sh
112+
sudo service docker restart
113+
```
114+
115+
If containers cannot resolve domains or have other issues with internet access with above configuration, ensure that the above networks do not overlap with network interface configured by WSL. To do this, run `ip a` or `ifconfig` command ( or `ipconfig` in powershell) and check the IP address assigned for `eth0` interface it output. If the second part of the IP address matches either IP block defined in the above configuration, change the value in `/etc/docker/daemon.json`. Any private IP v4 address block should work. You can use, for example, some blocks between `172.16.0.0/16` and `172.31.0.0/16`, i.e., change the second part of the IP address.
76116

77117
=== "Use public DNS in WSL 2"
78118

79119
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`:
80120

81-
```conf
121+
```conf title="/etc/wsl.conf"
82122
[network]
83123
generateResolvConf = false
84124
```
@@ -91,58 +131,46 @@ To configure working DNS inside containers you can either use public DNS or modi
91131

92132
Finally, configure DNS server manually by creating `/etc/resolv.conf` with following content, for example by opening it with `sudo nano /etc/resolv.conf`:
93133

94-
```conf
134+
```conf title="/etc/resolv.conf"
95135
nameserver 8.8.8.8
96136
```
97137

98-
=== "Configure IP range for Docker"
138+
---
99139

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`:
140+
## Ensure Docker daemon is running
101141

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-
```
142+
Recent version of WSL enable systemd by default. In this case, Docker daemon should be started automatically when ever WSL is running. To verify Docker has been started, run the hello-world container:
119143

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
144+
```sh
145+
docker run hello-world
146+
```
123147

124-
Start `dockerd` as a background process by using `service` or, alternatively, start `dockerd` directly.
148+
If Docker was not started automatically, start `dockerd` as a background process by using `service` or, alternatively, start `dockerd` directly.
125149

126-
```bash
150+
```sh
127151
sudo service docker start
128152
```
129153

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.
154+
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 to start the Docker service.
131155

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`.
156+
??? example "Launch docker with boot settings"
133157

134-
```conf
135-
[boot]
136-
command = service docker start
137-
```
158+
If you are working on earlier version of 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`.
159+
160+
```conf title="/etc/wsl.conf"
161+
[boot]
162+
command = service docker start
163+
```
164+
165+
Note that this should not be done if systemd is enabled.
138166

139167
## Install Docker compose
140168

141169
Follow [Install Docker Compose](https://docs.docker.com/compose/install/) instructions for Linux.
142170

143171
__tl;dr:__ Install Docker Compose with the distros package manager:
144172

145-
```bash
173+
```sh
146174
sudo apt-get update
147175
sudo apt-get install docker-compose-plugin
148176
```

mkdocs.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ plugins:
5757
enable_creation_date: true
5858

5959
markdown_extensions:
60+
- admonition
61+
- pymdownx.details
6062
- pymdownx.highlight:
6163
anchor_linenums: true
6264
line_spans: __span
@@ -65,4 +67,4 @@ markdown_extensions:
6567
- pymdownx.snippets
6668
- pymdownx.superfences
6769
- pymdownx.tabbed:
68-
alternate_style: true
70+
alternate_style: true

0 commit comments

Comments
 (0)