Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save riazul701/836aceb07d0a730bd81bc3a0e5ce5ef4 to your computer and use it in GitHub Desktop.
Save riazul701/836aceb07d0a730bd81bc3a0e5ce5ef4 to your computer and use it in GitHub Desktop.
Running VSCode remote containers on windows without docker desktop

Running VSCode remote containers on windows without docker desktop

Due to the license issues with docker desktop and the fact that you don't really need this buggy bit of software, this guide will walk you through the steps to use VSCode+remote-containers in combination with WSL2 without using docker desktop.

Get rid of docker desktop

Only if you have docker desktop currently installed of course

Uninstall docker desktop application

Via app & features in the windows settings menu

Remove WSL2 environments created by docker

in windows cmd.exe:

wsl --unregister docker-desktop-data
wsl --unregister docker-desktop

Remove docker settings from WSL

If you were using the docker CLI in WSL2 that came with Docker Desktp let's get rid of all traces of that

rm -rf ~/.docker
sudo rm -rf /usr/local/lib/docker

Set up docker + compose in WSL2

Install docker-ce

In theory you should also be able to sunstitute docker with podman, but I haven't tried that. Here are the instructions for using docker-ce, based roughly on this guide: https://docs.docker.com/engine/install/ubuntu/

Add the docker package registry with pre-built binaries

sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
   "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install docker deamon and CLI

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Add a groupp docker and add yourself so you can run docker without sudo:

sudo groupadd docker
sudo gpasswd -a $USER docker

Install docker compose and buildkit

Base on https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04 To be hones I am a bit lost as what goes where exacly, so will accpet suggestions/improvements. This is what worked for me.

# create directory for plugins
mkdir $HOME/.docker
mkdir $HOME/.docker/cli-plugins

# install docker-compose v2.2.3, see https://github.com/docker/compose/releases for latest
sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

# create symbolic link in plugins directory
ln -s /usr/local/bin/docker-compose $HOME/.docker/cli-plugins/docker-compose

# install buildx v0.7.1, see https://github.com/docker/buildx/releases for latest
sudo curl -L "https://github.com/docker/buildx/releases/download/v0.7.1/buildx-v0.7.1.linux-amd64" -o $HOME/.docker/cli-plugins/docker-buildx
sudo chmod +x $HOME/.docker/cli-plugins/docker-buildx

Start docker deamon on startup

There are multiple ways to accomplish this, see also:

I tried this and it sems to work adequately. Note that I user ZSH as terminal, if you use bash, you will need to modify .bashrc instead of .zshrc

Based on https://blog.nillsf.com/index.php/2020/06/29/how-to-automatically-start-the-docker-daemon-on-wsl2/

# Allow docker to be started without requesting sudo password:
sudo visudo
# add line 
<your user name> ALL=(ALL) NOPASSWD: /usr/bin/dockerd

# add docker stuff to ~/.zshrc
echo '# Start Docker daemon automatically when logging in if not running.' >> ~/.zshrc
echo 'RUNNING=`ps aux | grep dockerd | grep -v grep`' >> ~/.zshrc
echo 'if [ -z "$RUNNING" ]; then' >> ~/.zshrc
echo '    sudo dockerd > /dev/null 2>&1 &' >> ~/.zshrc
echo '    disown' >> ~/.zshrc
echo 'fi' >> ~/.zshrc

After this, finally docker should work, you can check with this test image

>> sudo docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:975f4b14f326b05db86e16de00144f9c12257553bba9484fed41f9b6f2257800
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

[Optional] Enable docker buildkit

To enable docker buildkit you need to set DOCKER_BUILDKIT=1. However if you set that in ~/.zshrc this doesn't get loaded when VSCode calls docker. So we use the WSLENV variable like so:

  • Add DOCKER_BUILDKIT=1 to your windows environmental variables
  • set/append DOCKER_BUILDKIT/u: to the WSLENV variable Now every WSL instance will have the DOCKER_BUILDKIT variable set

Finally

[Optional] log into private container registries

From WSL2, log into docker

echo $CONTAINER_REGISTRY_PASSWORD | docker login -u $CONTAINER_REGISTRY_USERNAME --password-stdin  $CONTAINER_REGISTRY

Allow vscode to locate docker and docker compose

Make sure you have these extensions installed in vscode:

  • ms-vscode-remote.remote-wsl
  • ms-vscode-remote.remote-containers

Open WSL, and open vscode from there (use code . to open vscode in the current folder). In the lower left corner it should say WSL, to indicate that WSL is running from inside your WSL environment.

Troubleshooting

If you get an error message like Docker returned an error code ENOENT, message: ... you have most likely tried to open a folder on your windows drive in containers. With docker desktop this is possbile (but a bad idea giving bad performance), but with this setup it is not possible as there is not docker client installed in Windows, and that is how we want it. Make sure to move your projects to the wsl dirve (accessible as \\wsl$\Ubuntu\home from windows)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment