简体   繁体   English

无法通过 SSH 隧道从本地 docker 容器连接到远程 PostgreSQL 数据库; 连接超时

[英]Could not connect to a remote PostgreSQL database from a local docker container through an SSH tunnel; connection timed out

I am trying to connect我正在尝试连接
to a PostgreSQL database hosted on a remote server远程服务器上托管的PostgreSQL数据库
from a PostGIS docker container which is started on my laptop (Ubuntu 18.04).来自在我的笔记本电脑(Ubuntu 18.04)上启动的PostGIS docker 容器

My very first step was naturally to open an SSH tunnel from my laptop to the remote server.我的第一步自然是打开从我的笔记本电脑到远程服务器的SSH 隧道 Everything is OK to this point because I can connect to a terminal on the remote server through the SSH tunnel.到目前为止一切正常,因为我可以通过 SSH 隧道连接到远程服务器上的终端。

Then, based on this answer: https://stackoverflow.com/a/24326540/6630397 I started the PostGIS docker container on my Ubuntu 18.04 machine to connect to the remote PostgreSQL database.然后,基于这个答案: https://stackoverflow.com/a/24326540/6630397我在我的 Ubuntu 18.04 机器上启动了 PostGIS docker 容器以连接到远程 PostgreSQL 数据库。 (This is necessary for me because I have some version mismatch issue between my native pg_tools and the pg version on the remote server.) (这对我来说是必要的,因为我的本地pg_tools 和远程服务器上的 pg 版本之间存在一些版本不匹配问题。)

But when trying to connect to the remote database from my local container, the connection times out.但是当尝试从我的本地容器连接到远程数据库时,连接超时。

Technical details技术细节

Let's assume the PostgreSQL port on the remote server is the default 5432 , the SSH tunnel was mapped to my local 5433 as follows:假设远程服务器上的 PostgreSQL 端口是默认的5432 , SSH 隧道映射到我本地的5433如下:

ssh -f -N <user@remote-server> -L 5433:127.0.0.1:5432

And the PostGIS docker container was started as: PostGIS docker 容器启动为:

$ docker run --rm -d \
  --add-host host.docker.internal:host-gateway \
  --name postgis \
  -v "/path/to/local/data_folder:/data" \
  -e POSTGRES_DB=mydbname \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=password \
  postgis/postgis:13-3.1

Then I simply connect to it using:然后我只需使用以下方法连接到它:

$ docker exec -it postgis bash

(connecting to this postgis container by specifying -u postgres doesn't solve the issue) (通过指定-u postgres连接到此 postgis 容器并不能解决问题)

And from inside the container:从容器内部:

root@c1246180b316:/# psql -U postgres -h host.docker.internal -p 5433

But the connection actually times out (where it should ask for the remote db password):但连接实际上超时(它应该要求远程数据库密码):

psql: error: could not connect to server: Connection timed out
    Is the server running on host "host.docker.internal" (172.17.0.1) and accepting
    TCP/IP connections on port 5433?

If I run the exact same psql command from outside the docker container, directly on my local machine, replacing host.docker.internal by localhost it works fine!如果我从 docker 容器外部运行完全相同的psql命令,直接在我的本地机器上,将host.docker.internal替换为localhost ,它工作正常!

How can I fix this problem and successfully connect to my remote database from inside my postgis container?如何解决此问题并从我的 postgis 容器内成功连接到我的远程数据库?

Solution for me was to make sure that the tunnel accepts all incoming connections.我的解决方案是确保隧道接受所有传入连接。 For some reason, it seems that docker doesn't count as coming from the machine or something like that.出于某种原因,似乎 docker 不算来自机器或类似的东西。

So, changing the tunnel line to this:因此,将隧道线更改为:

ssh -f -N <user@remote-server> -L 0.0.0.0:5433:127.0.0.1:5432

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM