Uncategorized

WSL2 – Use docker with VSCode without docker desktop (Windows 11)

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

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)
alternatively type code . from the WSL2 instance
  • Select WSL distro from VSCode (CTRL SHIFT P)
You should following view before proceeding
  • Run docker installation script
Ensure you are not running as root (Whoami should not display root)
curl -sSL get.docker.com | sh
# Add yourself to docker group
sudo usermod $USER -aG docker
You can ignore the following message and wait 20 secs
  • After installation has completed run from Windows
wsl --shutdown

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

  • If you have existing Docker extension then remove it
  • From extensions install the extension in WSL (Ensure you are connected via remote WSL)
You should following view enabled before proceeding
  • 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)

https://stackoverflow.com/questions/65813979/sudo-systemctl-enable-docker-not-available-automatically-run-docker-at-boot-o/65814529#65814529

4 comments on “WSL2 – Use docker with VSCode without docker desktop (Windows 11)

  1. thx for the great post!

    Liked by 1 henkilö

  2. should be worked also with wsl 1 or just wsl 2?

    Tykkää

Jätä kommentti