为什么我无法连接到 docker 中的 postgres?

Why I can't connect to postgres in docker?

我使用了 docker-compose from this 项目。 docker 两个容器都已成功启动。

kshnkvn@kshnkvn-vb:~$ docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                    NAMES
10fafbab73dc        openpoiservice_gunicorn_flask   "/ops_venv/bin/gunic…"   23 minutes ago      Up 22 minutes       0.0.0.0:5000->5000/tcp   openpoiservice_gunicorn_flask_1
a66fe5691455        kartoza/postgis:11.0-2.5        "/bin/sh -c /docker-…"   23 minutes ago      Up 22 minutes       5432/tcp                 openpoiservice_psql_postgis_db_1

但是在尝试检查服务的功能时 - 他无法连接到数据库。我尝试手动完成:

kshnkvn@kshnkvn-vb:~$ docker exec -it 10fafbab73dc /bin/bash
root@10fafbab73dc:/deploy/app# psql -h localhost -U gis_admin-gis
psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
root@10fafbab73dc:/deploy/app# 

奇怪,检查了一下,以防容器网络的类型是网桥:

kshnkvn@kshnkvn-vb:~$ docker network ls
NETWORK ID          NAME                         DRIVER              SCOPE
81001dac99c0        bridge                       bridge              local
8e65fb4ef6f8        host                         host                local
94ce4e1605ef        none                         null                local
a3f48ac3facc        openpoiservice_default       bridge              local
e3d4286df013        openpoiservice_poi_network   bridge              local

已检查 postgres 启动日志:

kshnkvn@kshnkvn-vb:~$ docker logs a66fe5691455
Add rule to pg_hba: 0.0.0.0/0
Add rule to pg_hba: replication replicator 
Setup master database
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
2020-02-08 13:50:20.675 UTC [25] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-02-08 13:50:20.683 UTC [25] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-02-08 13:50:20.756 UTC [37] LOG:  database system was interrupted; last known up at 2020-02-08 13:35:17 UTC
2020-02-08 13:50:21.830 UTC [48] postgres@postgres FATAL:  the database system is starting up
psql: FATAL:  the database system is starting up
2020-02-08 13:50:22.726 UTC [37] LOG:  database system was not properly shut down; automatic recovery in progress
2020-02-08 13:50:22.730 UTC [37] LOG:  redo starts at 0/21CCC50
2020-02-08 13:50:22.730 UTC [37] LOG:  invalid record length at 0/21CCC88: wanted 24, got 0
2020-02-08 13:50:22.730 UTC [37] LOG:  redo done at 0/21CCC50
2020-02-08 13:50:22.867 UTC [25] LOG:  database system is ready to accept connections
                              List of databases
   Name    |   Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+-----------+----------+---------+---------+-----------------------
 gis       | gis_admin | UTF8     | C.UTF-8 | C.UTF-8 | 
 postgres  | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
 template1 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
(4 rows)

postgres ready
Setup postgres User:Password
Creating superuser gis_admin
ALTER ROLE
Creating replication user replicator
ALTER ROLE
gis db already exists
                              List of databases
   Name    |   Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+-----------+----------+---------+---------+-----------------------
 gis       | gis_admin | UTF8     | C.UTF-8 | C.UTF-8 | 
 postgres  | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
 template1 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
(4 rows)

2020-02-08 13:50:24.785 UTC [25] LOG:  received smart shutdown request
2020-02-08 13:50:24.799 UTC [25] LOG:  background worker "logical replication launcher" (PID 58) exited with exit code 1
2020-02-08 13:50:24.801 UTC [53] LOG:  shutting down
2020-02-08 13:50:24.838 UTC [25] LOG:  database system is shut down
Postgres initialisation process completed .... restarting in foreground
2020-02-08 13:50:25.842 UTC [148] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-02-08 13:50:25.842 UTC [148] LOG:  listening on IPv6 address "::", port 5432
2020-02-08 13:50:25.850 UTC [148] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-02-08 13:50:25.880 UTC [150] LOG:  database system was shut down at 2020-02-08 13:50:24 UTC
2020-02-08 13:50:25.887 UTC [148] LOG:  database system is ready to accept connections

看起来 postgre 在 ip 0.0.0.0 上启动

我查看了docker ip addr show 命令使用的ip。尝试使用此 ip 重新连接:

psql: could not connect to server: Connection refused
    Is the server running on host "172.17.0.1" and accepting
    TCP/IP connections on port 5432?
root@10fafbab73dc:/deploy/app# psql -h 172.17.255.255 -U gis_admin-gis
psql: could not connect to server: Connection timed out
    Is the server running on host "172.17.255.255" and accepting
    TCP/IP connections on port 5432?

如何将脚本连接到数据库?

我是 运行 docker-compose.yml 并且想从您的主机连接 postgis,您需要映射端口,添加:

   ports:
     - "25432:25432"

psql_postgis_db 容器中(在 docker-compose.yml 文件中)。

此外,您可以使用 POSTGRES_USERPOSTGRES_PASS 环境变量覆盖用户名和密码。您可以在 docker-compose.yml 文件中看到默认的用户名和密码。

version: '2.2'

volumes:
 postgis-data:

services:
 gunicorn_flask:
  #network_mode: "host"
  build: .
  volumes:
  - ./osm:/deploy/app/osm
  - ./ops_settings_docker.yml:/deploy/app/openpoiservice/server/ops_settings.yml
  - ./categories_docker.yml:/deploy/app/openpoiservice/server/categories/categories.yml
  ports:
   - "5000:5000"
  mem_limit: 28g
  networks:
   - poi_network

 # Don't forget to change the host name inside ops_settings_docker.yml by the one given to docker container.
 # Also port should be set to 5432 (default value) inside the same file since they are on the same network
 psql_postgis_db:
  image: kartoza/postgis:11.0-2.5
  volumes:
   - postgis-data:/var/lib/postgresql
  environment:
   # If you need to create multiple database you can add coma separated databases eg gis,data
   - POSTGRES_DB=gis
   - POSTGRES_USER=gis_admin    # Here it's important to keep the same name as the one configured inside ops_settings_docker.yml
   - POSTGRES_PASS=admin        # Here it's important to keep the same name as the one configured inside ops_settings_docker.yml
   - POSTGRES_DBNAME=gis        # Here it's important to keep the same name as the one configured inside ops_settings_docker.yml
   - ALLOW_IP_RANGE=0.0.0.0/0
  ports:
   - "25432:25432"
  restart: on-failure
  networks:
   - poi_network

networks:
 poi_network:

顺便说一句,如果您想从您的主机连接,您的本地主机上必须有 postgresql-client