docker compose 3.3 无法访问其他容器

huangapple go评论57阅读模式
英文:

docker compose 3.3 cannot reach other containers

问题

我有一个描述Postgres(PostGIS)数据库实例和GeoServer(HTTP后端)的Docker Compose文件,版本为3.3。

我在没有依赖项的情况下运行它们,但在同一个Docker网络中。

稍后,一旦PostGIS完成其工作,GeoServer也完成其工作,我通过GeoServer API配置了一个使用Postgres连接详细信息的GeoServer数据存储,使用Postgres Docker服务名称作为主机,但然后当我测试这些连接详细信息时,测试失败,即GeoServer无法在同一个Docker网络中访问Postgres。

有趣的事实是:从localhost可以访问Postgres容器。

# 这个可以工作
PGCONNECT_TIMEOUT=2 psql "postgresql://adm_pg_user:1234abcd@localhost:6666/fgag_db" --command "SELECT NOW();"

这是一个纯粹的TCP/IP问题,因为我登录到运行GeoServer的容器中并使用psql命令,发现Postgres无法访问,如下所示,注意pg-db而不是localhost

# 从GeoServer容器内部,这不起作用
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有什么问题?
  • 我如何实施网络检查?

为了完整起见:我使用这个环境变量 COMPOSE_PROJECT_NAME=fgag,以便能够向Docker服务/网络名称添加我的首选前缀。也许这与网络问题有关。

version: "3.3"
services:
  pg-db:
    image: postgis/postgis:15-3.3-alpine
    restart: always
    ports:
      - "${MYPG_PORT:-5432}:5432"
    environment:
      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
  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 # 通过挂载此目录,我们可以在启动时从主机安装库
    networks:
      - fgagnet
networks:
  fgagnet:
    driver: bridge
英文:

I have a docker-compose file version 3.3 describing a Postgres (PostGIS) database instance and a GeoServer (HTTP backend).

I run them with no dependencies, but in the same docker network.

Later once PostGIS has done its thing and GeoServer has done its thing as well, I configure a GeoServer datastore with the Postgres connection details via the GeoServer API using the Postgres Docker service name as host, but then when I test these connection details the test fails i.e. GeoServer cannot reach Postgres in the same Docker network.

Interesting fact: the Postgres container is on reach from localhost.

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

It's a pure TCP/IP issue as I logged into the GeoServer running container and used the command psql to see that Postgres is not on reach, see below and notice pg-db instead of 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();"

The error message is:

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?
  • What's wrong with the Docker Compose YAML below?
  • How can I implement networking checks?

For completeness: I use this env var COMPOSE_PROJECT_NAME=fgag to be able to add my preferred prefix to docker services / docker network names. Maybe this is relevant to the networking issues.

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

答案1

得分: 1

你确定这个命令连接到相同的 Docker 容器吗?

# 这个命令有效
PGCONNECT_TIMEOUT=2 psql "postgresql://adm_pg_user:1234abcd@localhost:6666/fgag_db" --command "SELECT NOW();"

你确定已正确配置容器以暴露端口 6666 吗?

export MYPG_PORT=6666
英文:

Are you sure that this command connects to the same docker container ?

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

Are you sure that you have correctly configured the container to expose port 6666 ?

export MYPG_PORT=6666

huangapple
  • 本文由 发表于 2023年6月1日 23:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定