SSH port forwarding in Docker

2020-04-19 08:22发布

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条回答
2楼-- · 2020-04-19 08:45

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 :)

查看更多
登录 后发表回答