[GIS] How to write docker-compose.yml after enabling postgis_raster for PostGIS Docker Image

bashdockerpostgispostgis-rasterpostgresql

I'm using the docker image of https://github.com/postgis/docker-postgis and it works fine with the commands below which is also defined in README file.

docker run --name some-postgis -e POSTGRES_PASSWORD=mysecretpassword -d postgis/postgis and
docker run -it --link some-postgis:postgres --rm postgres \ sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'

However, my task is encapsulating the raster datatype and this image doesn't include postgis_raster extension. So, I've modified the files for enabling the postgis_raster extension in the Alpine version of PostgreSQL 13.

initdb-postgis.sh

#!/bin/sh

set -e

# Perform all actions as $POSTGRES_USER
export PGUSER="$POSTGRES_USER"

# Create the 'template_postgis' template db
"${psql[@]}" <<- 'EOSQL'
CREATE DATABASE template_postgis IS_TEMPLATE true;
EOSQL

# Load PostGIS into both template_database and $POSTGRES_DB
for DB in template_postgis "$POSTGRES_DB"; do
    echo "Loading PostGIS extensions into $DB"
    "${psql[@]}" --dbname="$DB" <<-'EOSQL'
        CREATE EXTENSION IF NOT EXISTS postgis;
        CREATE EXTENSION IF NOT EXISTS postgis_topology;
        CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
        CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;
        CREATE EXTENSION IF NOT EXISTS postgis_raster;
EOSQL
done

update-postgis.sh

#!/bin/sh

set -e

# Perform all actions as $POSTGRES_USER
export PGUSER="$POSTGRES_USER"

POSTGIS_VERSION="${POSTGIS_VERSION%%+*}"

# Load PostGIS into both template_database and $POSTGRES_DB
for DB in template_postgis "$POSTGRES_DB" "${@}"; do
    echo "Updating PostGIS extensions '$DB' to $POSTGIS_VERSION"
    psql --dbname="$DB" -c "
        -- Upgrade PostGIS (includes raster)
        CREATE EXTENSION IF NOT EXISTS postgis VERSION '$POSTGIS_VERSION';
        ALTER EXTENSION postgis  UPDATE TO '$POSTGIS_VERSION';
        -- Upgrade Topology
        CREATE EXTENSION IF NOT EXISTS postgis_topology VERSION '$POSTGIS_VERSION';
        ALTER EXTENSION postgis_topology UPDATE TO '$POSTGIS_VERSION';
        -- Install Tiger dependencies in case not already installed
        CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
        -- Upgrade US Tiger Geocoder
        CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder VERSION '$POSTGIS_VERSION';
        ALTER EXTENSION postgis_tiger_geocoder UPDATE TO '$POSTGIS_VERSION';
        CREATE EXTENSION IF NOT EXISTS postgis_raster VERSION '$POSTGIS_VERSION';
        ALTER EXTENSION postgis_raster UPDATE TO '$POSTGIS_VERSION';
    "
done

You can also reach the files via https://github.com/berkesenturk/postgis_image_raster_enabled.
Finally, I think I need to add a docker-compose.yml but I didn't see any examples for how to do it for PostGIS. How should I create the docker-compose.yml file?

Best Answer

version: '3'
services:
  db:
    build: .
    container_name: postgres-db
    environment:
      - POSTGRES_USER=username
      - POSTGRES_PASSWORD=mysecretpassword
    volumes:
      - ./data:/var/lib/postgresql/data
    expose:
      - "5432"

Something like that. This assumes your Dockerfile is at the same level as your docker-compose.yml. The precise configuration depends on what you need to do and how you want to configure it. I would recommend working through some Docker Compose tutorials until you understand it a bit more, it's a pretty thin layer on top of what you're already doing, it's just a lot easier than slinging around messy CLI parameters and escaping characters; it really becomes more useful when you add networking capability.