Use WSL's Docker from Windows like it's native
I have already explained how to get just Docker CLI tools without installing massive Docker distribution and running agent permanently all the time. This relies on having Podman installed on Windows though. What if you don’t even have Podman, but have WSL Docker instead of Windows Docker? Here’s how to do it.
First, you need to enable remote access to Docker daemon inside WSL. This is done by following the official instructions. When done properly, you should see port 2375 open on WSL’s IP address, which you can check with something like TCPView:
Lines I have added to docker.service
:
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://127.0.0.1:2375
This is pretty much the hardest part. Now you can use docker commands from Windows.
Use from Windows
When you type something like docker ps
on Windows, you’ll get an error.
error during connect: in the default daemon configuration on Windows, the docker client must be run with elevated privileges to connect: Get "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.47/containers/json": open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run with elevated privileges to connect. This error may also indicate that the docker daemon is not running.
This is because Docker CLI is not configured to use WSL’s Docker daemon. You can fix this by setting DOCKER_HOST
environment variable to tcp://127.0.0.1:2375
. You can do this in PowerShell with:
$env:DOCKER_HOST="tcp://127.0.0.1:2375"
Which will make it work just fine. Just a though, I haven’t actually checked, but my WSL has IPv6 disabled for other reasons. So if this doesn’t work, try to follow my instructions from another post.
Making it better
If you don’t want to set environment variable every time you open a new shell, you can add it to your profile. For PowerShell, this is done by adding the line to your profile script. You can find it by typing $PROFILE
in PowerShell.
$env:DOCKER_HOST="tcp://127.0.0.1:2375"
Even better with Docker contexts
You can make it even cleaner by using Docker contexts. Contexts are like profiles, or environments for docker CLI. Your default context is called “default”, and assumes you are using local Docker daemon. You can create a new context for WSL Docker with:
docker context create wsl --docker host=tcp://127.0.0.1:2375
Then activate it with
docker context use wsl
After which docker commands will be forwarded to the “wsl” context. To come back to the default context, use docker context use default
.
To contact me, send an email anytime or leave a comment below.