问题描述
我有这两个容器,例如backend
(CentOs)和mongo
.我想拥有的是,从backend
容器中,我可以连接到mongo数据库,就像它在本地运行一样,$> mongo localhost:27017
I have these two containers, say backend
(CentOs) and mongo
. What I would like to have is that from within the backend
container I can connect to the mongo database as if it was running locally, $> mongo localhost:27017
无论如何,据我所知,您可以像这样将端口localhost:27017映射到mongo:27017
Anyway, as far as I understand all this, you can map the port localhost:27017 to mongo:27017 like this
$backend> ssh -L 27017:mongo:27017 root@mongo
但是,如果执行此操作,则必须提供root密码,然后它会将我登录到mongo
容器中,并且没有端口转发发生
However, if I do this I have to provide the root password and after that it logs me into the mongo
container and no port forwarding is happening
背景:我想这样做是因为我正在运行一个Java程序,该程序连接到localhost上的Mongo数据库,而我无法更改它.
Background: I want to do this because I'm running a Java program which connects to a Mongo database on localhost and I cannot change that.
推荐答案
我找到了正确的SSH端口转发命令
I found the correct SSH port forwarding command
$> ssh root@mongo -L 27017:localhost:27017 -Nf
通常,使用此命令的想法是您将非公共端口映射-通过公共服务器映射到您自己的服务器/计算机.
Normally the idea with this command is that you map a non-public port - through a public server to you own server/compute.
* `root@mongo` - the public server
* -L <port on your server>:<third server address>:<port>
* `-Nf` - Do not login
由于public server
和third server
是同一台计算机/容器,因此必须使用localhost
:)
Because the public server
and third server
are the same computer/container you have to use localhost
:)
这篇关于Docker中的SSH端口转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!