从 Windows 主机连接到 wsl2 Ubuntu docker

问题描述 投票:0回答:2

我有 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 的新手,所以请解释我可以使用哪个选项。

docker ubuntu pipe windows-subsystem-for-linux wsl-2
2个回答
15
投票

免责声明:不是专家,只是遇到了完全相同的问题并像这样解决了。

默认情况下,docker 守护进程已启动,仅在 unix 套接字上公开。
据我所知,没有办法直接在intellij中指定unix套接字,而是在windows部分需要一些解决方法,我不知道这会需要多少工作。

您可以将守护进程配置为通过 tcp 套接字公开自身,例如与 docker 桌面使用的相同 tcp 套接字 (

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 套接字仍然用于 ubuntu 内的任何 docker 操作
  3. 重启docker服务,使配置生效:
    sudo service docker restart
  4. 验证 docker 是否已启动

使用:
wsl 2、ubuntu 20.04、Windows 10.0.19043
docker 安装如下:https://docs.docker.com/engine/install/ubuntu/
警告:systemd 目前无法在 wsl2 上完全开箱即用,因此某些选项可能不可用。

由于 wsl2 处理 Windows 和 wsl2 之间文件传输的方式,这个新工作流程:“ubuntu 中的 docker”可能与您之前的体验有很大不同。
您可能需要考虑通过某种方式将所有文件移动到 ubuntu 或在 Windows 上运行 docker。


0
投票

我在使用最新版本的 Docker 时遇到了问题,因为它们不允许从服务入口点和 daemon.json 进行双重配置。

这是我的解决方案,似乎效果很好:

  1. 添加配置覆盖以公开 tcp 套接字:

     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
    

    注意:您可以复制

    /lib/systemd/system/docker.service
    中的行,然后在末尾添加
    -H tcp://0.0.0.0:2375

  2. 重新加载并重新启动 Docker 守护进程:

     sudo systemctl daemon-reload
     sudo systemctl restart docker
    
  3. 将 Windows 中的环境变量设置为正确的端点:

     DOCKER_HOST=tcp://localhost:2375
    
  4. 在 Windows 上安装 Docker CLI,例如使用 Scoop:

     scoop install docker
    
  5. 在 Windows 中运行任何 Docker 命令:

     docker ps
    
© www.soinside.com 2019 - 2024. All rights reserved.