允许docker容器连接到本地/主机postgres数据库 [英] Allow docker container to connect to a local/host postgres database

查看:1523
本文介绍了允许docker容器连接到本地/主机postgres数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我仍然收到一条错误消息我尝试连接到在Docker中运行QGIS的数据库:无法连接到服务器:连接拒绝服务器在主机localhost(:: 1)上运行,并接受端口5433的TCP / IP连接?
Postgres服务器正在运行,我已经编辑了我的pg_hba.conf文件,以允许来自一系列IP地址(172.17.0.0/32)的连接。我以前使用 docker ps 查询了docker容器的IP地址,虽然IP地址发生了变化,但它始终在172.17.0.x范围内。 p>

任何想法为什么我无法连接到这个数据库?可能是我想象的东西很简单!



我正在运行Ubuntu 14.04; Postgres 9.3

解决方案

TL; DR




  1. 使用 172.17.0.0/16 作为IP地址范围,而不是 172.17.0.0/32

  2. 不要使用 localhost 连接到主机上的PostgreSQL数据库,而是使用主机的IP。要保持容器可移植,请使用 - add-host = database:< host-ip> 标志启动容器,并使用数据库作为连接到PostgreSQL的主机名。

  3. 确保PostreSQL配置为侦听所有IP地址上的连接,而不仅仅是在 localhost 。在PostgreSQL的配置文件中查找设置 listen_addresses ,通常位于 /etc/postgresql/9.3/main/postgresql.conf ($ @ $ d $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ code> 172.17.0.0/32 不是IP地址的范围,而是单个地址(namly 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永远preferred_lft永远

    正如你所看到的,在我的例子中,$ c> 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


    I've recently been playing around with Docker and QGIS and have installed a container following the instructions in this tutorial.

    Everything works great, although I am unable to connect to a localhost postgres database that contains all my GIS data. I figure this is because my postgres database is not configured to accept remote connections and have been editing the postgres conf files to allow remote connections using the instructions in this article.

    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

    Any ideas why I can't connect to this database? Probably something very simple I imagine!

    I'm running Ubuntu 14.04; Postgres 9.3

    解决方案

    TL;DR

    1. Use 172.17.0.0/16 as IP address range, not 172.17.0.0/32.
    2. 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.
    3. Make sure PostreSQL 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).

    Long version

    172.17.0.0/32 is not a range of IP addresses, but a single address (namly 172.17.0.0). No Docker container will ever get that address assigned, because it's the network address of the Docker bridge (docker0) interface.

    When Docker starts, it will create a new bridge network interface, that you can easily see when calling 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
    

    As you can see, in my case, the docker0 interface has the IP address 172.17.42.1 with a netmask of /16 (or 255.255.0.0). This means that the network address is 172.17.0.0/16.

    The IP address is randomly assigned, but without any additional configuration, it will always be in the 172.17.0.0/16 network. For each Docker container, a random address from that range will be assigned.

    This means, if you want to grant access from all possible containers to your database, use 172.17.0.0/16.

    这篇关于允许docker容器连接到本地/主机postgres数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆