我最近一直在使用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


当前回答

你可以在docker运行命令时通过——network=host来访问容器内的localhost。

Ex:

docker run --network=host docker-image-name:latest

如果你想传递环境变量与localhost使用——env-file参数来访问容器内的环境变量。

Ex:

docker run --network=host --env-file .env-file-name docker-image-name:latest

注意:在docker镜像名称之前传递参数,否则参数将无效。(我面对过这个问题,所以要小心!)

其他回答

我的设置还需要添加一个东西

172.17.0.1  localhost

到 /etc/hosts

这样Docker就会指向172.17.0.1作为数据库主机名,而不是依赖于改变外部ip来找到DB。希望这能帮助其他人解决这个问题!

3步解决方案

1. 更新docker-compose文件

首先,由于数据库是本地的,我们需要通过向容器服务添加以下配置来将主机网络绑定到容器

services:
    ...
    my-web-app:
        ...
        extra_hosts:
          -  "host.docker.internal:host-gateway"
        ...
    ...

2. 更新/etc/postgresql/12/main/pg_hba.conf

如果这个目录下不存在这个文件,使用find / -name 'pg_hba.conf'来找到它。

在注释标签# IPv4本地连接下添加以下一行:

host    all             all             172.17.0.1/16           md5

3.更新/etc/postgresql/12/main/postgresql.conf

如果这个目录下不存在这个文件,使用find / -name 'postgresql.conf'找到它。

找到下面的行(这行可能有注释)

#listen_addresses = 'localhost'

并将其更改为以下一行,以便能够从localhost和172.17.0.1连接到postgres

listen_addresses = 'localhost,172.17.0.1'

您还可以将其更改为如下所示,但不建议在生产环境中这样做,因为它将向世界公开数据库(意味着任何IP地址都可以连接到数据库)

listen_addresses = '*'

最后别忘了:

使用sudo systemctl Restart postgresql重新启动postgress服务 将连接字符串host更新为host.docker.internal

Mac解决方案的Docker

17.06以后

多亏了@Birchlabs的评论,现在有了这个特殊的mac专用DNS名称就简单多了:

docker run -e DB_PORT=5432 -e DB_HOST=docker.for.mac.host.internal

从17.12.0-cd-mac46开始,应该使用docker.for.mac.host.internal而不是docker.for.mac.localhost。详见发行说明。

旧版本

@helmbert的回答很好地解释了这个问题。但是Mac的Docker不暴露网桥网络,所以我不得不做这个技巧来解决这个限制:

$ sudo ifconfig lo0 alias 10.200.10.1/24

打开/usr/local/var/postgres/pg_hba.conf,添加如下一行:

host    all             all             10.200.10.1/24            trust

打开/usr/local/var/ postgresql.conf,编辑change listen_addresses:

listen_addresses = '*'

重新加载服务并启动容器:

$ PGDATA=/usr/local/var/postgres pg_ctl reload
$ docker run -e DB_PORT=5432 -e DB_HOST=10.200.10.1 my_app 

这个解决方案所做的基本上与@helmbert的答案相同,但使用一个附加到lo0的IP地址,而不是docker0网络接口。

在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数据库。

你可以在docker运行命令时通过——network=host来访问容器内的localhost。

Ex:

docker run --network=host docker-image-name:latest

如果你想传递环境变量与localhost使用——env-file参数来访问容器内的环境变量。

Ex:

docker run --network=host --env-file .env-file-name docker-image-name:latest

注意:在docker镜像名称之前传递参数,否则参数将无效。(我面对过这个问题,所以要小心!)