Search code examples
linuxdockerpostgisgeoserver

Can't connect to Postgis running in docker from Geoserver running in another docker continer


I used kartoza's docker images for Geoserver and Postgis and started them in two docker containers using the provided docker-compose.yml:

version: '2.1'

volumes:
   geoserver-data:
   geo-db-data:

services:
   db:
      image: kartoza/postgis:12.0
      volumes:
         - geo-db-data:/var/lib/postgresql
      ports:
        - "25434:5432"
      env_file:
      - docker-env/db.env
      restart: on-failure
      healthcheck:
        test: "exit 0"

   geoserver:
      image: kartoza/geoserver:2.17.0
      volumes:
        - geoserver-data:/opt/geoserver/data_dir
      ports:
        - "8600:8080"
      restart: on-failure
      env_file:
        - docker-env/geoserver.env
      depends_on:
        db:
          condition: service_healthy
      healthcheck:
        test: curl --fail -s http://localhost:8080/ || exit 1
        interval: 1m30s
        timeout: 10s
        retries: 3

The referenced .env files are:

db.env

POSTGRES_DB=gis,gwc
POSTGRES_USER=docker
POSTGRES_PASS=docker
ALLOW_IP_RANGE=0.0.0.0/0

geoserver.env

GEOSERVER_DATA_DIR=/opt/geoserver/data_dir
ENABLE_JSONP=true
MAX_FILTER_RULES=20
OPTIMIZE_LINE_WIDTH=false
FOOTPRINTS_DATA_DIR=/opt/footprints_dir
GEOWEBCACHE_CACHE_DIR=/opt/geoserver/data_dir/gwc
GEOSERVER_ADMIN_PASSWORD=myawesomegeoserver
INITIAL_MEMORY=2G
MAXIMUM_MEMORY=4G
XFRAME_OPTIONS='false'
STABLE_EXTENSIONS=''
SAMPLE_DATA=false
GEOSERVER_CSRF_DISABLED=true

docker-compose up brings both containers up and running with no errors giving them names backend_db_1 (Postgis) and backend_geoserver_1 (Geoserver). I can access Geoserver running in backend_geoserver_1 under http://localhost:8600/geoserver/ as expected. I can connect an external, AWS-based Postgis as a data store to my docker-based Geoserver instance without any problems. I can also access the Postgis running in the docker container backend_db_1 from PgAdmin, with psql from the command line and from the Webstorm IDE.

However, if I try to use my Postgis running in backend_db_1 as a data store for my Geoserver running in backend_geoserver_1, I get the following error:

> Error creating data store, check the parameters. Error message: Unable
> to obtain connection: Cannot create PoolableConnectionFactory
> (Connection to localhost:25434 refused. Check that the hostname and
> port are correct and that the postmaster is accepting TCP/IP
> connections.)

So, my Geoserver in backend_geoserver_1 can connect to Postgis on AWS, but not to the one running in another docker container on the same localhost. The Postgis in backend_db_1 in its turn can be accessed from many other local apps and tools, but not from Geoserver running in a docker container.

Any ideas what I am missing? Thanks!


Solution

  • just add the network_mode in YAML in db and geoserver and set it to host

    network_mode: host
    

    note that this will ignore the expose option and will use the host network an containers network