Sunday, January 12, 2020

Solution for Docker in WSL and connection problems for Vscode running Remote-Container cmds


Have you already set up an machine running on WSL version 2 with Docker and is about to run and set up your project in an remote container from vscode but can't connect to Docker from Visual Studio Code?

You might encounter errors in Vscode when running Remote-Container: Opening folder in container like these or similar.

error during connect: Get http://localhost:2375/v1.40/version: dial tcp [::1]:2375
ECONNREFUSED 127.0.0.1:2375

Even though you have set up environment variable DOCKER_HOST to "tcp://localhost:2375" and confirmed that the communication works to the Docker server from the terminal/cmd/shell?

Then you might need to change the environment variable of DOCKER_HOST to the actual IP-adress of the WSL machine running the docker server. At least if you want VsCode to be able to run the containers with docker.

You'll find the IP adress of your machine by running i.e. this command from a windows terminal:
wsl.exe -d CHANGE_THIS_TO_YOUR_DIST_NAME /bin/bash -c "ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1"
CHANGE_THIS_TO_YOUR_DIST_NAME could be Ubuntu-16.04 or Ubuntu-18.04 or any other OS you've installed.
Copy the IP-adress and update DOCKER_HOST with the actual IP of the WSL machine. Close down vscode and open again to refresh the env-variables. Vscode should now be able to run the docker commands.

Remember to update the IP-adress in your DOCKER_HOST environment variable when you restart windows and since your virtual machine in WSL doesn't have a static IP, yet. See this issue about WSL dynamic and missing static IP-config

Tip! Guide for setting up WSL2 with Docker server in Windows.