Connect to wsl2 Ubuntu docker from Windows host
Asked Answered
L

2

20

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.

Lynlyncean answered 28/1, 2022 at 9:36 Comment(0)
C
15

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.

Comte answered 28/1, 2022 at 11:21 Comment(7)
Just what I was looking for! Great thanks, managed to configure it via tcp connection and connect to docker from JetBrains IDELynlyncean
So what address should I put indeed? localhost:2375?Lomond
@WesternGun, Yes. localhost:2375Dianthe
Note that this doesn't work when you have a VPN (like Cisco AnyConnect) which disables LAN traffic. The Docker Desktop application must do something different behind the scenes, but I can't figure out what yet. VPNKit seems to have the pieces to the puzzle, but I can't quite put them together yet.Shrapnel
I tried this, but after that sudo service docker restart won't work.Protomartyr
Ok, might be related to the comment from @KevinRakProtomartyr
Exactly what I am looking for. Worked for me! windows 10, WSL2, Docker installed on ubuntu, docker cli installed on windows + git bash. PS I just needed to add this env var: DOCKER_HOST=tcp://localhost:2375. Otherwise it won't workDeni
S
2

I had issues with more recent versions of Docker as they do not allow double-configurations from the service entry point and the daemon.json.

Here is my solution that seems to work great:

  1. Add a config override to also expose the tcp socket:

     sudo mkdir -p /etc/systemd/system/docker.service.d
     sudo tee /etc/systemd/system/docker.service.d/override.conf > /dev/null <<EOT
     [Service]
     ExecStart=
     ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
     EOT
    

    Note: You can copy the line from /lib/systemd/system/docker.service and just add -H tcp://0.0.0.0:2375 at the end.

  2. Reload and Restart the Docker daemon:

     sudo systemctl daemon-reload
     sudo systemctl restart docker
    
  3. Set the environment variable in Windows to the correct endpoint:

     DOCKER_HOST=tcp://localhost:2375
    
  4. Install a Docker CLI on Windows, for example with Scoop:

     scoop install docker
     scoop install docker-buildx
    
  5. Run any Docker command in Windows:

     docker ps
    

Note: If you want to mount a volume from Windows, this does not work directly as the Docker "context" is the one from WSL, so you need to use paths from WSL. So if you want to mount the folder c:\foo\bar into the container, you can use a command like (note the double //!):

docker run --rm -it -v //mnt/c/foo/bar://bar debian
Smart answered 25/4 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.