SSH port forwarding in Docker

百般思念 提交于 2019-12-20 04:08:29

问题


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

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

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

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.


回答1:


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

Because the public server and third server are the same computer/container you have to use localhost :)



来源:https://stackoverflow.com/questions/39633057/ssh-port-forwarding-in-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!