This guide is for Windows 11, as at the moment it is using [boot] section of WSL.conf, which is not available in Windows 10, unless you are enrolled in insider version of Windows. If you are not running on Windows 11, I recommend you check this guide -> (this guide also implements it, but you have ensure you do the win10 config part in the particular section)
I’ve always preferred the terminal experience when Linux is considered, so it makes sense to configure WSL2 and VSCode to support Docker use scenarios just the same way.
- If you are looking for most convenient ”express use”- experience with WSL I’d recommend that you use docker desktop
- https://www.docker.com/pricing/faq (check the link for licensing info)
Prerequisites
- While not mandatory, I recommend, that you create copy of your WSL distro, and perform this setup on duplicate distro. There is handy guide here
Example: WSL export
wsl --install -d "Ubuntu-20.04"
wsl --export "Ubuntu-20.04" "Ubuntu-20.04-z.tar.gz"
wsl --import "2004" c:\vm\ .\Ubuntu-20.04-z.tar.gz
✅ Have WSL2 installed and Ubuntu -> https://docs.microsoft.com/en-us/windows/wsl/install#install
Tip: You might want to also change the default user in distro, by creating new WSL.conf file (Tip from https://www.hanselman.com/blog/how-to-set-the-default-user-for-a-wsl-distro-that-has-been-manually-installed-with-wsl-import)
[user] default=jose
Terminal

VScode

Setup
- Ensure VSCode is connected via remote SSH connection to WSL
- using remote WSL extension (needs to be installed)


code .
from the WSL2 instance- Select WSL distro from VSCode (CTRL SHIFT P)



- Run docker installation script

curl -sSL get.docker.com | sh
# Add yourself to docker group
sudo usermod $USER -aG docker


- After installation has completed run from Windows
wsl --shutdown

- Now let’s ensure init.d and docker start run on boot (based on this guide)
- Windows 10 version is sourced on this guide Running Docker on WSL2 without Docker Desktop (the right way) – DEV Community
Set Docker to start on boot
#Windows 11 version
sudo su -
sudo touch /etc/wsl.conf
sudo chmod 777 /etc/wsl.conf
sudo echo "[boot]
command=\"/etc/init.d/docker start\"" > /etc/wsl.conf
# You might setup some more suitable permissions than chmod 777, chmod was only given to ensure the test works
#If in Windows 10
echo "
# NonInsidersVersion
if service docker status 2>&1 | grep -q \"is not running\"; then
wsl.exe -d "${WSL_DISTRO_NAME}" -u root -e /usr/sbin/service docker start >/dev/null 2>&1
fi" >> ~/.profile
- After rebooting you should be able to use docker as follows

- Now test docker hello-world
docker run hello-world

Setup extensions
- This part of the guide is based on following advice https://github.com/microsoft/vscode-docker/issues/1582#issuecomment-578882428
- If you have existing Docker extension then remove it
- From extensions install the extension in WSL (Ensure you are connected via remote WSL)

- After this setup you should have docker shown as here!

curl -sSL get.docker.com | sh ; sudo usermod $USER -aG docker
# Add yourself to docker group
wsl --shutdown
sudo su -
sudo touch /etc/wsl.conf
sudo chmod 777 /etc/wsl.conf
sudo echo "[boot]
command=\"/etc/init.d/docker start\"" > /etc/wsl.conf
wsl --shutdown
docker run hello-world
References
https://github.com/microsoft/vscode-docker/issues/1582#issuecomment-578882428
https://cloudconvert.com/mp4-to-gif (Video to GIF conversion)
thx for the great post!
TykkääTykkää
Thx for the feedback!
TykkääTykkää