0

I'm trying to connect to a database via SSH from my docker container. I'm getting an error

could not connect to server: Connection refused
    Is the server running on host "0.0.0.0" and accepting
    TCP/IP connections on port 5433?

I have a script to open the port and it works fine on my development, OSX Catalina.

bin/tunnel.sh

#!/usr/bin/expect -f
spawn ssh -fNg -L 5432:0.0.0.0:5432 myserver
expect "connecting"
send "yes"
expect "assword:"
send "mypassword\r"
interact

ENTRYPOINT

set -e

if [ -f tmp/pids/server.pid ]; then
  rm tmp/pids/server.pid
fi

service cron restart
./bin/tunnel.sh
foreman start -f Procfile.production web

database.yml

production: &production
  <<: *default
  # sslmode: "require"
  host: 0.0.0.0
  # pool: 100
Antarr Byrd
  • 155
  • 10

1 Answers1

1

EDIT

I wrongly assumed the DB is running inside the docker container, that's why my previous answer didn't work. The problem in your script above is that you specified 0.0.0.0:5432 as tunnel-endpoint.

This doesn't work, because the ssh clients needs a real ip / hostname it can connect to, localhost:5432 for example. If your Database is running on the same host you are ssh'ing to, -L 5432:localhost:5432 should work.

Martin
  • 2,194
  • 7
  • 16
  • This doesn't work for me. Changing to `5432:localhost:5432` works fine locally. But the -R option does not. I tried pushing to production to test anyway, it doesn't work. I also stopped my local Postgres server to make sure I wasn't hitting it instead. – Antarr Byrd May 10 '21 at 14:40
  • hold on... where exactly is the database, and where do you want to connect it to ? The database is inside the docker container, and you want to connect it to the host "myserver" ? – Martin May 10 '21 at 14:50
  • The database is on a physical server that is accessible via a domain i.e. `myserver.com`. The rails application is running on the docker container – Antarr Byrd May 10 '21 at 14:54
  • then I misunderstood your question, sorry about that. I edited my answer ... – Martin May 10 '21 at 15:12
  • The application is running on a docker container on render.com. The db is running on an actual physical box somewhere else in the world – Antarr Byrd May 10 '21 at 15:44
  • I've tried that. It works locally on my mac, but not in the docker container – Antarr Byrd May 10 '21 at 21:09
  • check the logs of the docker container, maybe there is a clue inside, since ssh prints errors like 'port already in use' to stdout / stderr ... – Martin May 11 '21 at 08:27