macos - 在 macos Mojave 10.14 上从 docker 容器设置 ssh 隧道

标签 macos docker ssh

我在我的 mac 机器上设置 ssh 隧道时遇到问题。我在我的 ubuntu 机器上设置隧道没有问题。这是我运行的命令
ssh -nNT -L 172.18.0.1:4000:production-database-url:3306 jump-point
当我在我的 mac 上运行它时,我收到以下错误:

bind [172.18.0.1]:4000: Can't assign requested address
channel_setup_fwd_listener_tcpip: cannot listen to port: 4000 Could
not request local forwarding.

如果我在没有 bind_address (172.18.0.1) 的情况下运行,我可以通过隧道连接到数据库。

如果我绑定(bind)到所有接口(interface)(0.0.0.0),那么隧道是打开的,但是,从 docker 容器内部到数据库的连接不起作用。

最佳答案

172.18.0.1是docker的default bridge network gateway的IP ,而不是您主机的 IP。
您可以运行此命令进行检查。

$ docker network inspect bridge

Docker for Mac has limitations


  • macOS 上没有 docker0 网桥(它位于 Mac 和 Windows 上的 docker VM 主机中)
  • 您无法 ping 容器(无需剃掉一堆牦牛)
  • 无法按容器 IP 寻址

  • 另请注意,这意味着 docker run 选项 --net-host Mac 不支持,但也许这是件好事

    There is a workaround


  • 这些魔术地址从容器内解析为主机的 IP
  • docker.for.mac.localhost(已弃用)
  • docker.for.mac.host.internal(已弃用)
  • host.docker.internal
  • 这解析到主机mac的网关
  • gateway.docker.internal

  • 使用名称host.docker.internal从容器内,就像您直接在 mac 上使用 localhost 一样。

    不用担心隧道的绑定(bind)地址:
    ssh -nNT -L 4000:production-database-url:3306 jump-point
    

    您没有提到哪个数据库,但我从端口 3306 中得知它是 MySQL。

    要使用容器内的 mysql cli 通过主机上的 ssh 隧道连接到远程 mysql 数据库服务器,您可以运行:
    mysql --host host.docker.internal [... other options go here]

    关于macos - 在 macos Mojave 10.14 上从 docker 容器设置 ssh 隧道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53766291/

    相关文章:

    django - 使用 docker/fig 运行 Django 开发服务器时,为什么隐藏了一些日志输出?

    git - 多个 gitlab 用户帐户的多个 key 的 SSH 配置

    cocoa - 在 NSSplitView 中设置 View 不起作用

    macos - Spotlight或Alfred的“Open folder in Terminal”

    ubuntu - 在 Ubuntu 14.04 上使用 docker 时通过对等错误重置连接

    linux - Linux 容器的应用程序隔离?

    java - SSH 连接 Java

    java - 使用JCraft Jsch - 执行sshpass命令不成功

    objective-c - 如何从其他类(NSViewController 的子类)获取主窗口(App Delegate)?

    macos - 使用计算着色器的 Metal 模拟几何着色器