Running docker on WSL (Windows Subsystem for Linux)

UPDATE 2019/11/22:
There is a much better way to do this now with WSL2.  You can actually host the Docker engine inside WSL2 and have it accessible to windows & Ubuntu without needing to expose without TLS. If you did the steps below previously, remove the export DOCKER_HOST declaration before updating to the new WSL2 version.
https://docs.docker.com/docker-for-windows/wsl-tech-preview/

————

My build toolchain is frequently in there for node based applications, and I was looking for a way to run docker commands from WSL (Windows Subsystem for Linux https://docs.microsoft.com/en-au/windows/wsl/install-win10 ).

Note: The below steps are a quick way to get it working but involves disabling TLS authentication. A more involved way to do this without disabling TLS can be found at https://blogs.technet.microsoft.com/virtualization/2017/12/08/wsl-interoperability-with-docker/

This setup puts the docker engine in Windows on top of Hyper-V, but allows you to control it either from Windows, or within WSL by connecting it to the Windows Docker Engine.

  1. Install docker for windows https://docs.docker.com/docker-for-windows/install/
  2. Go into the Docker for Windows settings and expose daemon on localhost
    docker windows settings
  3. Install docker in WSL (it’ll just install the client) https://docs.docker.com/install/linux/docker-ce/ubuntu/
  4. Tell WSL to look for a docker engine on localhost
    TIP: put the export into your ~/.bashrc so you don’t need to run it ever time.
    export DOCKER_HOST=tcp://127.0.0.1:2375

Now you will be able to run standard docker commands run new images, show the running containers, etc. docker linux

5 thoughts on “Running docker on WSL (Windows Subsystem for Linux)

  1. How do volume mounts work with this? I had this setup, and was running docker-compose of a file which had some (relative) volume binds. E.g.

    docker-compose.yaml had something like:
    service0:
    volumes:
    – ./foo:/root/foo

    `docker inspect service0` shows the binding done correctly: from `~/myrepo/foo` to `/root/foo`. However when I actually look at the folder contents at “~/myrepo/foo”, it’s empty.

    I suspect this is because the docker daemon runs in Windows, and it doesn’t know that “~/myrepo/foo:” path is in WSL. I tried searching for that path on Windows directly (under C:\), but turns out no such path exists. So I don’t know where the volume lies “physically’.

Leave a comment