postgresql docker compose 3.3无法访问其他容器

pqwbnv8z  于 2023-06-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(174)

我有一个docker-compose文件版本3.3,描述了一个Postgres(PostGIS)数据库示例和一个GeoServer(HTTP后端)。
我在没有依赖关系的情况下运行它们,但在同一个docker网络中。
稍后,一旦PostGIS完成了它的工作,GeoServer也完成了它的工作,我通过GeoServer API使用Postgres Docker服务名称作为主机配置了一个GeoServer数据存储区,但当我测试这些连接详细信息时,测试失败,即:GeoServer无法访问同一Docker网络中的Postgres。
有趣的事实:从localhost可访问Postgres容器。

# this works
PGCONNECT_TIMEOUT=2 psql "postgresql://adm_pg_user:1234abcd@localhost:6666/fgag_db" --command "SELECT NOW();"

这是一个纯粹的TCP/IP问题,因为我登录到GeoServer运行容器并使用命令psql查看Postgres无法访问,请参阅下文并注意pg-db而不是localhost

# from within the GeoServer container this doesn't work
PGCONNECT_TIMEOUT=2 psql "postgresql://adm_pg_user:1234abcd@pg-db:6666/fgag_db" --command "SELECT NOW();"

错误消息为:

psql: error: connection to server at "pg-db" (172.19.0.3), port 6666 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?
  • 下面的Docker Compose YAML有什么问题?
  • 如何实现联网检查?

完整性:我使用这个env var COMPOSE_PROJECT_NAME=fgag来将我首选的前缀添加到docker服务/ docker网络名称中。这可能与网络问题有关。

version: "3.3"
services:
  pg-db:
    image: postgis/postgis:15-3.3-alpine
    restart: always
    ports:
      - "${MYPG_PORT:-5432}:5432"
    environment:
      # https://github.com/docker-library/docs/tree/master/postgres#environment-variables
      POSTGRES_USER: "${MYPG_USER:-adm_pg_user}"
      POSTGRES_PASSWORD: "${MYPG_PASSWORD:-1234abcd}"
      POSTGRES_DB: "${MYPG_DB:-fgag_db}"
      PGPASSWORD: "${MYPG_PASSWORD:-1234abcd}"
      MYDB_SCHEMA: "${MYDB_SCHEMA:-fgag_schema}"
    volumes:
      - ./docker-volume/pg:/var/lib/postgresql/data
      - ./db-sql-init.sh:/docker-entrypoint-initdb.d/db-sql-init.sh
    networks:
      - fgagnet
  # https://github.com/geoserver/docker/blob/master/docker-compose-demo.yml
  # geoserver default admin:geoserver credentials
  #
  # to connect to the running container: `docker exec -it fgag_geoserver_1 /bin/bash`
  #
  # to be able to use `psql`:
  # apt-get update && apt-get install postgresql-client
  #
  # docker network inspect fgag_fgagnet
  geoserver:
    image:  docker.osgeo.org/geoserver:2.23.0
    ports:
      - "${MYGS_PORT:-7777}:8080"
    environment:
      INSTALL_EXTENSIONS: "true"
      STABLE_EXTENSIONS: wps,csw
      EXTRA_JAVA_OPTS: -Xms1G -Xmx2G
    volumes:
      - ./docker-volume/gs/geoserver_data:/opt/geoserver_data/:Z
      - ./docker-volume/gs/additional_libs:/opt/additional_libs:Z # by mounting this we can install libs from host on startup
    networks:
      - fgagnet
networks:
  fgagnet:
    driver: bridge
qojgxg4l

qojgxg4l1#

您确定此命令连接到同一个docker容器吗?

# this works
PGCONNECT_TIMEOUT=2 psql "postgresql://adm_pg_user:1234abcd@localhost:6666/fgag_db" --command "SELECT NOW();"

您确定已正确配置容器以公开端口6666吗?

export MYPG_PORT=6666

相关问题