代码之家  ›  专栏  ›  技术社区  ›  BlowMan

Docker容器Postgres连接错误

  •  1
  • BlowMan  · 技术社区  · 7 年前

    下面是我重新创建问题的步骤。

    dockerfile开始

    FROM postgres
    ENV POSTGRES_DB db 
    ENV POSTGRES_USER postgres
    ENV POSTGRES_PASSWORD postgres
    COPY db_schema.sql /docker-entrypoint-initdb.d/
    

    我像这样建立了docker文件

    $ docker build -t db_con .
    

    并创建了一个容器

    $ docker run -it -p 5432:5432  --name test_db db_con /bin/bash
    

    运行容器视图如下

    $ docker ps -a
    
    CONTAINER ID    IMAGE   COMMAND                  CREATED       STATUS       PORTS                    NAMES
    82347f1114c4    db   "docker-entrypoint..."      3 hours ago    Up 2 sec    0.0.0.0:5432->5432/tcp   test_db
    

    我检查了集装箱的地址信息。。

    $ docker inspect test_db
    
    --extract start--
    "Networks": {
                    "bridge": {
                        "Gateway": "172.17.0.1",
                        "IPAddress": "172.17.0.2",
    
                    }
                }
    --extract end--
    

    root@82347f1114c4:/# psql -U postgres -h 0.0.0.0 -p 5432 -d db
    root@82347f1114c4:/# psql -U postgres -h 172.17.0.1 -p 5432 -d db
    root@82347f1114c4:/# psql -U postgres -h 172.17.0.2 -p 5432 -d db
    **response for all the above**
    psql: could not connect to server: Connection refused
        Is the server running on host "0.0.0.0" and accepting
        TCP/IP connections on port 5432?
    

    如果有人能给我指出正确的方向,我会很高兴。我遇到了麻烦,非常感谢您的帮助。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Bukharov Sergey    7 年前

    看起来您将默认postgres cmd覆盖为 /bin/bash . 你为什么把 在命令结束时?

    docker run -it -p 5432:5432  --name test_db db_con /bin/bash
    

    docker run -it -p 5432:5432  --name test_db db_con
    

    此外,postgres仅在db转储恢复时可用。

        2
  •  0
  •   Camilo Camargo    7 年前

    您需要在pg_hba中添加新规则。形态:

    nano /etc/postgresql/9.3/main/pg_hba.conf
    

    添加:

    host    all             all             [Docker Server IP]/16            md5
    

    接下来,您需要取消对postgres中follow行的注释。形态:

    listen_addresses = '*'                  # what IP address(es) to listen on;
    

    现在重新启动postgres服务,然后重试。