我在我的 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
另请注意,这意味着 docker run 选项
--net-host
Mac 不支持,但也许这是件好事There is a workaround
使用名称
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/