从Windows主机连接到WSL2 Ubuntu Docker

18

我使用的是Windows 10,之前安装了Docker Desktop。由于他们改变了商业使用条款,我决定卸载Docker Desktop,并仅使用docker引擎本身(因为我不使用GUI)。我已经在WSL 2下的Ubuntu上安装了docker,它可以正常运行:

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:~$

现在我想允许我的JetBrains IDE连接到Docker引擎。我有以下选项:

进入图片描述

那么配置连接的最佳方式是什么?是否可以通过某种方式“创建链接”以使用Docker for Windows选项?似乎它只是尝试连接npipe:////./pipe/docker_engine。或者我可以公开TCP/SSH端口。

我对配置Docker很新,请解释我可以使用哪个选项。

1个回答

14
免责声明: 我不是专家,只是曾经遇到过完全相同的问题,并且通过这种方式解决了它。
默认情况下,docker 守护程序仅在一个 unix socket 上启动和暴露。据我所知,在 intellij 中无法直接指定该 unix socket,而是需要在 windows 部分进行一些绕过操作,我不清楚这需要多少工作量。
你可以配置守护程序通过 tcp socket 暴露自己,例如与 docker desktop 使用的同一个 tcp socket (tcp://localhost:2375)。设置完成后,你可以再次配置 intellij 通过 tcp 与 docker 守护程序交互。请注意,将 docker 守护程序暴露到网络中涉及到安全问题。
步骤如下:
  1. 确保 docker 已安装并运行正常 (比如执行 docker run hello-world)
  2. 创建文件 /etc/docker/daemon.json,内容为:{ "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"] },请注意 unix socket 仍然用于 ubuntu 内部的任何 docker 操作
  3. 重启 docker 服务以使配置生效: sudo service docker restart
  4. 验证 docker 是否成功配置:
使用环境:
wsl 2, ubuntu 20.04, windows 10.0.19043根据https://docs.docker.com/engine/install/ubuntu/的说明安装Docker。
注意:目前WSL2上的systemd无法完全正常工作,因此某些选项可能不可用。

由于WSL2处理Windows和WSL2之间文件传输的方式,因此这种名为“Ubuntu中的Docker”的新工作流程可能与您以前的体验大不相同。
您可能需要考虑将所有文件移动到Ubuntu或通过某些手段在Windows上运行Docker。


正是我所需要的!非常感谢,通过TCP连接成功配置并能够在JetBrains IDE中连接到Docker。 - Roman
那么我应该放哪个地址呢?localhost:2375吗? - WesternGun
@WesternGun,是的。http://localhost:2375 - Ashishkel
请注意,当您使用禁用局域网流量的VPN(如Cisco AnyConnect)时,此方法无效。Docker桌面应用程序在幕后可能有所不同,但我还无法弄清楚具体是什么。VPNKit似乎拥有这个谜题的一些要素,但我还不能完全将它们组合起来。 - Kevin Rak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接