dockerubuntupipewindows-subsystem-for-linuxwsl-2

Connect to wsl2 Ubuntu docker from Windows host


I have Windows 10 and had Docker Desktop installed. After they changed terms of commercial use I decided to remove Docker Desktop installation and use just docker engine itself (as I didn't use GUI). I've installed docker on Ubuntu under WSL 2 and it works fine:

localusr@MACHINE:~$ docker context ls
NAME            DESCRIPTION                               DOCKER ENDPOINT                             KUBERNETES ENDPOINT   ORCHESTRATOR
default *       Current DOCKER_HOST based configuration   unix:///mnt/wsl/shared-docker/docker.sock                         swarm
desktop-linux                                             npipe:////./pipe/dockerDesktopLinuxEngine
Warning: DOCKER_HOST environment variable overrides the active context. To use a context, either set the global --context flag, or unset DOCKER_HOST environment variable.
localusr@MACHINE:~$ docker run hello-world

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

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

localusr@MACHINE:~$

Right now I want to allow my JetBrains IDE to connect to Docker Engine. I have the following options: enter image description here

So what is the best way to configure connection? Can I somehow "create a link" to pipe to use Docker for Windows option? Seems like it just tries to connect to npipe:////./pipe/docker_engine. Or I can expose TCP/SSH ports.

I am new to configuring docker so please explain which option I can use.


Solution

  • Disclaimer: not an expert, just had the exact same problem and solved it like this.

    By default the docker daemon is started, exposed only on an unix socket.
    As far as i can tell there is no way to directly specify that unix socket in intellij, instead some workaround would be required on the windows part, i have no idea how much work this would be.

    You may configure the daemon to also expose itself via a tcp socket, for example same tcp socket that you used with docker desktop (tcp://localhost:2375).
    Once setup, you may once again configure intellij to interact with the docker daemon via tcp.
    Please note the involved security concerns of exposing your docker daemon to a network.

    1. ensure docker is installed and working (i.e. docker run hello-world)
    2. create the file /etc/docker/daemon.json
      with: { "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"] }
      note that the unix socket is still used for any docker operation from within ubuntu
    3. restart the docker service, to take config into effect: sudo service docker restart
    4. verify docker is stiu

    used:
    wsl 2, ubuntu 20.04, windows 10.0.19043
    docker installation as per: https://docs.docker.com/engine/install/ubuntu/
    caveat: systemd does not currently, fully, work out of the box on wsl2 therefor some options may not be available.

    This new workflow: "docker in ubuntu" may drastically vary from your previous experience due to the way wsl2 handles file transfer between windows and wsl2.
    You might want to consider moving all files to ubuntu or running docker on windows through some means.