docker compose 3.3 cannot reach other containers
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


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

