Search code examples
dockerdocker-composepostgisqgis

Connect QGIS project in docker container with Postgres+Postgis in another docker container


There is a qgis project which has a layer that is connected to database and gets data from http://localhost:5432. When the project is opened in Qgis desktop - everything works perfect - you update data in postgres (localhost:5432) - the layer is updated on the map.

There is a QGIS server which is started inside a docker container. I place the project inside the qgis-server docker container (/etc/qgisserver), but the project can not set connection (localhost:5432) with database in another docker container.

I can not understand how to make qgis server inside container refer to its localhost:5432 but get to the host`s localhost:5432 (where postgres from another container is accessible).

I have tried to do smth like this but have not succeeded:

services:
  app_postgres:
    image: kartoza/postgis:latest
    container_name: app_postgres
    volumes:
      - ~/volumes/jhipster/app/postgresql/:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=app
      - POSTGRES_PASSWORD=app
      - POSTGRES_DBNAME=app
      - POSTGRES_MULTIPLE_EXTENSIONS=postgis,hstore,postgis_topology,postgis_raster,pgrouting
    ports:
      - 5432:5432

  qgisserver:
    image: camptocamp/qgis-server:latest
    volumes:
      - ~/qgis/projects:/etc/qgisserver
    links:
      - app_postgres:db
    ports:
      - 8380:80
    depends_on:
      - app_postgres

Solution

  • I have succeeded this way:

    • I have connected QGIS and Postgis containers in bridge network;

    • Explicit IP addresses were assigned to Postgis and Qgis containers;

    • I have changed /etc/hosts file in my QGIS container so that calling 'localhost' from QGIS container will lead to Postgis container.

    So now the project that is deployed on QGIS sever is connected to Postgis from the nearby container. The docker-compose YAML now looks like:

    version: '3.4'
    services:
      db:
        image: kartoza/postgis:latest
        container_name: docker_app_postgres
        volumes:
          - ~/volumes/jhipster/app/postgresql/:/var/lib/postgresql/data/
        environment:
          - POSTGRES_USER=app
          - POSTGRES_PASSWORD=app
          - POSTGRES_DBNAME=app
          - POSTGRES_MULTIPLE_EXTENSIONS=postgis,hstore,postgis_topology,postgis_raster,pgrouting
        networks:
          bridge_geo:
            ipv4_address: 172.28.1.1
        ports:
          - 5432:5432
    
      qgisserver:
        image: camptocamp/qgis-server:latest
        container_name: docker_app_qgis
        volumes:
          - ~/qgis/projects:/etc/qgisserver
        networks:
          bridge_geo:
            ipv4_address: 172.28.1.2
        extra_hosts:
          localhost: 172.28.1.1
        ports:
          - 8380:80
        depends_on:
          - db
    
    networks:
      bridge_geo:
        ipam:
          driver: default
          config:
            - subnet: 172.28.0.0/16