SSH port forwarding disconnecting if initiated from a docker container

Viewed 174

I am port forwarding with ssh to a remote db like so from a docker container:

ssh -o "ServerAliveInterval 15” -o -f -N -q -L 8000:localhost:8080 root@6.10.125.60

This works and I can access the database normally. I added ServerAliveInterval so that the connection is kept alive even when client is idle. I have realised that this does not work from a docker container and connection is lost after several minutes.

I used the exact same command locally (not from docker) and it works as intended, i.e. no connection loss.

So my workaround to this was to port forward locally and then access the database from docker with an hostname alias.

The above worked and solved my connection lost issue. My question is why is this the case? I.e. why does ServerAliveInterval seem to have no effect when run from insid a docker container?

1 Answers

You need also another option ServerAliveCountMax

ServerAliveCountMax

Sets the number of server alive messages (see below) which may be sent without ssh(1) receiving any messages back from the server. If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session. It is important to note that the use of server alive messages is very different from TCPKeepAlive (below). The server alive messages are sent through the encrypted channel and therefore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive is spoofable. The server alive mechanism is valuable when the client or server depend on knowing when a connection has become unresponsive.

The default value is 3. If, for example, ServerAliveInterval (see below) is set to 15 and ServerAliveCountMax is left at the default, if the server becomes unresponsive, ssh will disconnect after approximately 45 seconds.

http://man.openbsd.org/ssh_config#ServerAliveCountMax

Related