Dockerised Postgis: granular control

Posted on Oct 12, 2023

Dockerized applications offer tremendous advantages in production settings, especially when integrated with Kubernetes. For a comprehensive and well-maintained solution suitable for general purposes, I highly recommend Kartoza’s PostGIS Docker image. It comes with most essentials out of the box, and the repository is actively maintained.

However, in some cases, precise version control is essential. If you find yourself in such a situation, creating your own PostGIS image might be the way to go. The following example can be used as a model to build your own custom PostGIS:

FROM postgres:15.3

# Select a version
ENV POSTGIS_VERSION 3.3.3

RUN apt-get update 

# General purpose libs
RUN apt-get -y install \
    libpq-dev \
    build-essential \
    sqlite3 \
    libsqlite3-dev \
    pkg-config \
    libudunits2-dev \
    postgresql-server-dev-15 \
    software-properties-common \
    wget

# GIS packages essential for PostGIS
RUN apt-get -y install \
    libgeos-dev \
    libgdal-dev \
    libproj-dev 

# Installs PostGIS
RUN wget https://postgis.net/stuff/postgis-$POSTGIS_VERSION.tar.gz
RUN tar xvzf postgis-$POSTGIS_VERSION.tar.gz
RUN cd postgis-$POSTGIS_VERSION && \
    ./configure --with-pgconfig=/usr/lib/postgresql/15/bin/pg_config && \
    make && make install

# Removes source files
RUN rm -r postgis-$POSTGIS_VERSION postgis-$POSTGIS_VERSION.tar.gz
 
# If you need entrypoints, create a directory called 'entrypoint'
# and uncomment the COPY statement below.
# COPY ./entrypoint/ /docker-entrypoint-initdb.d/ 

# This is something that I found helpful to add so locale does not solve invalid
# Change the LANG environment depending on your region/language.
RUN localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8

Keep in mind that this Dockerfile only provides a starting point and that adjustments to PostgreSQL or PostGIS versions may result in issues when building image. Again, treat this as a base model and be sure to conduct thorough testing of the database before deploying it in a production environment.

Enjoy!