我最近一直在使用Docker和QGIS,并按照本教程中的说明安装了一个容器。

一切都很好,尽管我无法连接到包含我所有GIS数据的本地主机postgres数据库。我想这是因为我的postgres数据库没有配置为接受远程连接,并且一直在编辑postgres conf文件以允许使用本文中的说明进行远程连接。

I'm still getting an error message when I try and connect to my database running QGIS in Docker: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections to port 5433? The postgres server is running, and I've edited my pg_hba.conf file to allow connections from a range of IP addresses (172.17.0.0/32). I had previously queried the IP address of the docker container using docker ps and although the IP address changes, it has so far always been in the range 172.17.0.x

知道为什么我无法连接到这个数据库吗?我想可能是非常简单的事情!

我运行的是Ubuntu 14.04;Postgres 9.3


当前回答

这里发布的解决方案对我不起作用。因此,我张贴这个答案来帮助那些面临类似问题的人。

注意:此解决方案也适用于Windows 10,请检查下面的评论。

操作系统:Ubuntu 18 PostgreSQL: 9.5(在Ubuntu上托管) Docker:服务器应用程序(连接到PostgreSQL)

我正在使用docker-compose。Yml构建应用程序。

步骤1:请添加host.docker.internal:<docker0 IP>

version: '3'
services:
  bank-server:
    ...
    depends_on:
      ....
    restart: on-failure
    ports:
      - 9090:9090
    extra_hosts:
      - "host.docker.internal:172.17.0.1"

要找到docker的IP,即172.17.0.1(在我的情况下),你可以使用:

$> ifconfig docker0
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255

OR

$> ip a
1: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

step2:在postgresql.conf中修改listen_addresses为listen_addresses = '*'

步骤3:在pg_hba.conf中添加这一行

host    all             all             0.0.0.0/0               md5

步骤4:现在重新启动postgresql服务使用,sudo service postgresql restart

步骤5:请使用host.docker.internal主机名从服务器应用程序连接数据库。 例:jdbc: postgresql: / / host.docker.internal: 5432 / bankDB。是否

享受! !

其他回答

在Ubuntu中:

首先,您必须检查Docker数据库端口是否在您的系统中可用,执行命令-

sudo iptables -L -n

样例输出:

Chain DOCKER (1 references)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.2           tcp dpt:3306
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.3           tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.3           tcp dpt:22

此处以“3306”作为172.17.0.2 IP地址的“Docker数据库端口”,如果没有此端口执行以下命令-

sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

现在,您可以通过以下配置从本地系统轻松访问Docker数据库

  host: 172.17.0.2 
  adapter: mysql
  database: DATABASE_NAME
  port: 3307
  username: DATABASE_USER
  password: DATABASE_PASSWORD
  encoding: utf8

In CentOS:

首先,您必须检查Docker数据库端口是否在您的防火墙中可用

sudo firewall-cmd --list-all

样例输出:

  target: default
  icmp-block-inversion: no
  interfaces: eno79841677
  sources: 
  services: dhcpv6-client ssh
  **ports: 3307/tcp**
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules:

此处以“3307”作为172.17.0.2 IP地址的“Docker数据库端口”,如果没有此端口执行以下命令-

sudo firewall-cmd --zone=public --add-port=3307/tcp

在服务器端,可以永久添加端口

sudo firewall-cmd --permanent --add-port=3307/tcp
sudo firewall-cmd --reload

现在,您可以通过上述配置轻松地从本地系统访问Docker数据库。

为了建立一个简单的Postgresql连接,从docker容器到我的本地主机,我在Postgresql .conf中使用了这个:

listen_addresses = '*'

并添加了这个pg_hba.conf:

host    all             all             172.17.0.0/16           password

然后重新启动。我的客户端从docker容器(在172.17.0.2)可以连接到Postgresql运行在我的本地主机使用host:密码,数据库,用户名和密码。

以防万一,上述解决方案并不适用于任何人。使用以下语句从docker连接到主机postgres(在mac上):

psql --host docker.for.mac.host.internal -U postgres

博士TL;

Use 172.17.0.0/16 as IP address range, not 172.17.0.0/32. Don't use localhost to connect to the PostgreSQL database on your host, but the host's IP instead. To keep the container portable, start the container with the --add-host=database:<host-ip> flag and use database as hostname for connecting to PostgreSQL. Make sure PostgreSQL is configured to listen for connections on all IP addresses, not just on localhost. Look for the setting listen_addresses in PostgreSQL's configuration file, typically found in /etc/postgresql/9.3/main/postgresql.conf (credits to @DazmoNorton).

长版本

172.17.0.0/32不是一个IP地址范围,而是一个地址(即172.17.0.0)。任何Docker容器都不会得到这个地址,因为它是Docker桥接(docker0)接口的网络地址。

当Docker启动时,它会创建一个新的网桥网络接口,当你调用ip a时可以很容易地看到:

$ ip a
...
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever

如您所见,在我的例子中,docker0接口的IP地址为172.17.42.1,子网掩码为/16(或255.255.0.0)。这意味着网络地址是172.17.0.0/16。

IP地址是随机分配的,但是没有任何额外的配置,它将始终位于172.17.0.0/16网络中。对于每个Docker容器,将从该范围内分配一个随机地址。

这意味着,如果您希望从所有可能的容器授予对数据库的访问权限,请使用172.17.0.0/16。

对于docker-compose,您可以尝试只添加

network_mode: "host"

例子:

version: '2'
services:
  feedx:
    build: web
    ports:
    - "127.0.0.1:8000:8000"
    network_mode: "host"

https://docs.docker.com/compose/compose-file/#network_mode