Docker for Windows and WSL1 to Work together

Viewed 17510

Exactly the same problem as Ubuntu WSL with docker could not be found

$ docker

The command 'docker' could not be found in this WSL 1 distro.
We recommend to convert this distro to WSL 2 and activate
the WSL integration in Docker Desktop settings.

See https://docs.docker.com/docker-for-windows/wsl/ for details.

But my requirement is different -- I want to

  • stick with WSL1 (for reasons beyond this topic)
  • and use Docker for Windows as-is

I.e., I have WSL1 and Docker for Windows installed parallel to each other. This is my current info:

C:> ver
Microsoft Windows [Version 10.0.18363.1379]

C:> wsl -l -v
  NAME      STATE           VERSION
* Debian    Running         1

I don't see integration in "Resources -> WSL Integration", and I don't have WSL2 backend enabled in Docker Desktop settings.

enter image description here

Just that I'm getting the above problem -- my docker works anywhere, in CMD, Powershell, git-bash, etc, just not in my WSL.

All solutions that I found are to install Docker for Windows within WSL1 or WSL2, but I want to keep everything as is -- WSL and Docker for Windows installed parallel to each other.

Any solution for that?

2 Answers

The command 'docker' could not be found in this WSL 1 distro.
We recommend to convert this distro to WSL 2 and activate
the WSL integration in Docker Desktop settings.

This means in WSL2, it has a real linux kernel which is required to install docker daemon, then in docker-desktop you could have chance to set docker daemon in WSL2. Otherwise, the docker daemon is running in Hyper-V machine. But, if you stick in WSL1, no chance to run docker-daemon in WSL, so the only option is running docker daemon in Hyper-V machine.

Although above is the fact, still we have chance to let you operate docker ps, docker pull etc. in WSL1 bash just like you operate through CMD, Powershell, git-bash, that is allow Docker to accept requests from remote hosts.

For your case, the steps maybe next:

1. Expose docker daemon in docker desktop settings as next, then click Apply & Restart:

enter image description here

2. Install standalone docker client in WSL1:

$ wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.5.tgz
$ tar zxvf docker-20.10.5.tgz
$ cd docker

3. Set default docker daemon:

$ export DOCKER_HOST=tcp://localhost:2375

4. Verify docker client command:

$ ./docker info

Just FTA, this is the quick hacky workaround that I found, while trying to solve it myself

Change the C:\Program Files\Docker\Docker\resources\bin\docker file to

#!/usr/bin/env sh
#
# Copyright (c) Docker Inc.

binary=$(basename "$0")
"$binary.exe" "$@"

Then docker can work anywhere now, in CMD, Powershell, git-bash, and as well as WSL1.

Note that this hacky workaround is specially for the situation described in OP, might not work for anything else.

Related