2021-01-27 18:06:59 +00:00
##############################################################################
# Base stage #
##############################################################################
2020-04-20 10:05:39 +00:00
ARG DISTRO = debian
2023-07-29 13:07:11 +00:00
ARG IMAGE_VERSION = bookworm
2020-10-01 15:55:53 +00:00
ARG IMAGE_VARIANT = slim
2021-01-27 18:06:59 +00:00
FROM $DISTRO:$IMAGE_VERSION-$IMAGE_VARIANT AS postgis-base
2021-01-30 00:13:10 +00:00
LABEL maintainer = "Tim Sutton<tim@kartoza.com>"
2021-11-26 12:15:51 +00:00
# Cache invalidation number is used to invalidate a cache.
# Simply increment the number by 1 to reset the cache in local and GitHub Action
# This is added because we can't purge GitHub Action cache manually
LABEL cache.invalidation.number= "1"
2021-11-26 12:18:45 +00:00
ARG CACHE_INVALIDATION_NUMBER = 1
2021-01-27 18:06:59 +00:00
2022-02-11 10:03:44 +00:00
2021-01-27 18:06:59 +00:00
# Reset ARG for version
ARG IMAGE_VERSION
RUN apt-get -qq update --fix-missing && apt-get -qq --yes upgrade
RUN set -eux \
&& export DEBIAN_FRONTEND = noninteractive \
&& apt-get update \
2024-09-26 08:36:33 +00:00
&& apt-get -y --no-install-recommends install \
2024-09-26 12:01:05 +00:00
locales gnupg2 wget ca-certificates rpl pwgen software-properties-common iputils-ping \
2024-09-26 09:32:30 +00:00
apt-transport-https curl gettext pgxnclient cmake && \
2024-09-26 12:01:05 +00:00
apt-get -y install build-essential autoconf libxml2-dev zlib1g-dev netcat-openbsd gdal-bin \
2024-09-26 09:32:30 +00:00
figlet toilet gosu; \
# verify that the binary works
2024-09-26 12:01:05 +00:00
gosu nobody true && \
2024-09-26 09:32:30 +00:00
dpkg-divert --local --rename --add /sbin/initctl
2023-08-06 03:02:07 +00:00
2021-01-27 18:06:59 +00:00
2024-09-26 12:01:05 +00:00
# Generating locales takes a long time. Utilize caching by runnig it by itself
2021-01-27 18:06:59 +00:00
# early in the build process.
# Generate all locale only on deployment mode build
# Set to empty string to generate only default locale
ARG GENERATE_ALL_LOCALE = 1
2021-01-27 18:59:12 +00:00
ARG LANGS = "en_US.UTF-8,id_ID.UTF-8"
2021-01-27 18:06:59 +00:00
ARG LANG = en_US.UTF-8
ENV LANG = en_US.UTF-8 \
LANGUAGE = en_US:en \
LC_ALL = en_US.UTF-8
2021-11-28 05:38:57 +00:00
COPY ./base_build/scripts/locale.gen /etc/all.locale.gen
COPY ./base_build/scripts/locale-filter.sh /etc/locale-filter.sh
2021-01-27 18:06:59 +00:00
RUN if [ -z " ${ GENERATE_ALL_LOCALE } " ] || [ $GENERATE_ALL_LOCALE -eq 0 ] ; \
2024-09-26 12:01:05 +00:00
then \
cat /etc/all.locale.gen | grep " ${ LANG } " > /etc/locale.gen; \
/bin/bash /etc/locale-filter.sh; \
else \
cp -f /etc/all.locale.gen /etc/locale.gen; \
fi ; \
set -eux \
&& /usr/sbin/locale-gen
2021-01-27 18:06:59 +00:00
RUN update-locale ${ LANG }
2023-08-06 03:02:07 +00:00
2021-01-27 18:06:59 +00:00
# Cleanup resources
2024-09-26 12:01:05 +00:00
RUN apt-get -y --purge autoremove \
2021-01-27 18:06:59 +00:00
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
##############################################################################
# Production Stage #
##############################################################################
FROM postgis-base AS postgis-prod
2021-01-02 21:34:42 +00:00
2014-05-04 19:14:46 +00:00
2019-07-30 07:50:52 +00:00
# Reset ARG for version
ARG IMAGE_VERSION
2023-10-08 15:31:55 +00:00
ARG POSTGRES_MAJOR_VERSION = 16
2021-01-27 18:06:59 +00:00
ARG POSTGIS_MAJOR_VERSION = 3
2023-08-19 18:22:21 +00:00
ARG POSTGIS_MINOR_RELEASE = 4
2024-09-26 12:01:05 +00:00
# https://packagecloud.io/timescale/timescaledb
2024-09-26 16:23:02 +00:00
ARG TIMESCALE_VERSION = 2
ARG BUILD_TIMESCALE = true
2020-10-09 06:09:56 +00:00
2024-09-26 12:56:00 +00:00
2020-02-20 18:08:49 +00:00
RUN set -eux \
&& export DEBIAN_FRONTEND = noninteractive \
2021-01-02 21:34:42 +00:00
&& apt-get update \
2024-09-26 12:01:05 +00:00
&& wget -O- https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sh -c 'cat > /usr/share/keyrings/postgresql.gpg' > /dev/null \
2023-10-10 20:53:00 +00:00
&& echo deb [ signed-by= /usr/share/keyrings/postgresql.gpg] https://apt.postgresql.org/pub/repos/apt/ ${ IMAGE_VERSION } -pgdg main | tee /etc/apt/sources.list.d/pgdg.list 2>/dev/null \
2020-02-20 18:08:49 +00:00
&& apt-get -y --purge autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& dpkg-divert --local --rename --add /sbin/initctl
2019-01-30 19:06:28 +00:00
2024-09-26 12:01:05 +00:00
2014-05-04 19:14:46 +00:00
#-------------Application Specific Stuff ----------------------------------------------------
2024-09-26 15:08:44 +00:00
# Use an alternative method to add pgBackRest repository and key
# If the key fails, try skipping key verification for now
RUN apt-get update
RUN apt-get install -y cron
RUN wget -qO- https://pgbackrest.org/pgbackrest.gpg | tee /etc/apt/trusted.gpg.d/pgbackrest.gpg && \
echo "deb http://apt.pgbackrest.org bullseye main" > /etc/apt/sources.list.d/pgbackrest.list && \
apt-get update && \
apt-get install -y pgbackrest && \
rm -rf /var/lib/apt/lists/*
# Create necessary directories for pgBackRest
2024-09-26 16:23:02 +00:00
RUN mkdir -p /etc/pgbackrest /var/log/pgbackrest /var/lib/pgbackrest /tmp/pgbackrest/
2024-09-26 15:08:44 +00:00
# Set appropriate permissions for pgBackRest directories
RUN chown -R postgres:postgres /etc/pgbackrest /var/log/pgbackrest /var/lib/pgbackrest
2024-09-26 16:23:02 +00:00
RUN chmod 777 /tmp/pgbackrest/
RUN touch /var/log/pgbackrest/postgres-stanza-create.log
RUN chmod 777 /var/log/pgbackrest/postgres-stanza-create.log
2024-09-26 15:08:44 +00:00
# Copy pgBackRest configuration file
COPY ./pgbackrest/pgbackrest.conf /etc/pgbackrest/pgbackrest.conf
2025-08-08 06:39:07 +00:00
# Copy stanza-create script
COPY ./stanza-create.sh /usr/local/bin/stanza-create.sh
RUN chmod +x /usr/local/bin/stanza-create.sh
2024-09-26 15:08:44 +00:00
# Add a backup script
COPY ./pgbackrest/backup-script.sh /usr/local/bin/backup-script.sh
RUN chmod +x /usr/local/bin/backup-script.sh
# Add the cron job for automated backups
COPY ./pgbackrest/backup-cron /etc/cron.d/backup-cron
RUN chmod 0644 /etc/cron.d/backup-cron
# Apply cron job configuration
2025-08-08 08:25:24 +00:00
# RUN crontab /etc/cron.d/backup-cron
2024-09-26 15:08:44 +00:00
2024-09-26 12:01:05 +00:00
# We add postgis as well to prevent build errors (that we dont see on local builds)
2015-08-08 21:46:13 +00:00
# on docker hub e.g.
# The following packages have unmet dependencies:
2023-07-29 13:07:11 +00:00
2024-09-26 12:56:00 +00:00
RUN set -eux \
2020-02-20 18:08:49 +00:00
&& export DEBIAN_FRONTEND = noninteractive \
2024-09-26 12:56:00 +00:00
&& apt-get update \
&& apt-get -y --no-install-recommends install postgresql-client-${ POSTGRES_MAJOR_VERSION } \
2020-10-09 06:09:56 +00:00
postgresql-common postgresql-${ POSTGRES_MAJOR_VERSION } \
2021-01-27 18:06:59 +00:00
postgresql-${ POSTGRES_MAJOR_VERSION } -postgis-${ POSTGIS_MAJOR_VERSION } \
2023-07-29 13:07:11 +00:00
postgresql-${ POSTGRES_MAJOR_VERSION } -ogr-fdw \
2021-01-27 18:06:59 +00:00
postgresql-${ POSTGRES_MAJOR_VERSION } -postgis-${ POSTGIS_MAJOR_VERSION } -scripts \
2020-10-09 06:09:56 +00:00
postgresql-plpython3-${ POSTGRES_MAJOR_VERSION } postgresql-${ POSTGRES_MAJOR_VERSION } -pgrouting \
2024-09-26 12:56:00 +00:00
postgresql-server-dev-${ POSTGRES_MAJOR_VERSION } postgresql-${ POSTGRES_MAJOR_VERSION } -cron \
postgresql-${ POSTGRES_MAJOR_VERSION } -mysql-fdw && \
2024-09-26 12:01:05 +00:00
pgxn install h3
2024-09-25 11:42:09 +00:00
2024-09-26 12:56:00 +00:00
# Install OpenSSH server and configure SSH
RUN set -eux \
&& apt-get update \
&& apt-get install -y openssh-server \
&& mkdir /var/run/sshd
2024-09-26 13:58:39 +00:00
# Configure SSH to allow root login and use public key authentication
2024-09-26 14:57:15 +00:00
RUN echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config \
2024-09-26 12:56:00 +00:00
&& echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config \
2024-09-26 13:58:39 +00:00
&& echo 'PubkeyAuthentication yes' >> /etc/ssh/sshd_config \
2024-09-26 14:57:15 +00:00
&& echo 'AuthorizedKeysFile /shared-ssh/authorized_keys' >> /etc/ssh/sshd_config \
2024-09-26 13:58:39 +00:00
&& echo 'AllowTcpForwarding yes' >> /etc/ssh/sshd_config \
2024-09-26 14:57:15 +00:00
&& echo 'PermitEmptyPasswords yes' >> /etc/ssh/sshd_config
# Set the root password to an empty string
RUN echo 'root:' | chpasswd -e
2024-09-26 12:56:00 +00:00
# Start the SSH service
2024-09-26 12:01:05 +00:00
RUN service ssh start
2022-02-11 10:03:44 +00:00
2024-09-26 16:23:02 +00:00
# Enable archive_mode in postgresql.conf
# Enable archive_mode in postgresql.conf
RUN set -eux \
&& echo "archive_mode = on" >> /etc/postgresql/${ POSTGRES_MAJOR_VERSION } /main/postgresql.conf \
&& echo "archive_command = 'pgbackrest --stanza=postgres archive-push %p'" >> /etc/postgresql/${ POSTGRES_MAJOR_VERSION } /main/postgresql.conf \
&& echo "archive_timeout = 120s" >> /etc/postgresql/${ POSTGRES_MAJOR_VERSION } /main/postgresql.conf
2022-08-18 08:40:41 +00:00
# TODO a case insensitive match would be more robust
RUN if [ " ${ BUILD_TIMESCALE } " = "true" ] ; then \
2022-02-11 10:03:44 +00:00
export DEBIAN_FRONTEND = noninteractive && \
sh -c " echo \"deb [signed-by=/usr/share/keyrings/timescale.keyring] https://packagecloud.io/timescale/timescaledb/debian/ ${ IMAGE_VERSION } main\" > /etc/apt/sources.list.d/timescaledb.list " && \
2024-09-26 12:01:05 +00:00
wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | gpg --dearmor -o /usr/share/keyrings/timescale.keyring && \
2022-02-11 10:03:44 +00:00
apt-get update && \
2024-09-26 12:01:05 +00:00
apt-get -y --no-install-recommends install timescaledb-${ TIMESCALE_VERSION } -postgresql-${ POSTGRES_MAJOR_VERSION } timescaledb-tools; \
2022-02-11 10:03:44 +00:00
fi ;
2020-10-09 06:09:56 +00:00
2024-09-26 12:01:05 +00:00
RUN echo $POSTGRES_MAJOR_VERSION >/tmp/pg_version.txt && echo $POSTGIS_MAJOR_VERSION >/tmp/pg_major_version.txt && \
echo $POSTGIS_MINOR_RELEASE >/tmp/pg_minor_version.txt
2021-01-02 21:34:42 +00:00
ENV \
PATH = " $PATH :/usr/lib/postgresql/ ${ POSTGRES_MAJOR_VERSION } /bin "
2020-06-20 16:24:20 +00:00
# Compile pointcloud extension
RUN wget -O- https://github.com/pgpointcloud/pointcloud/archive/master.tar.gz | tar xz && \
cd pointcloud-master && \
./autogen.sh && ./configure && make -j 4 && make install && \
cd .. && rm -Rf pointcloud-master
# Cleanup resources
2024-09-26 12:01:05 +00:00
RUN apt-get -y --purge autoremove \
2020-02-20 18:08:49 +00:00
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
2014-05-04 19:14:46 +00:00
2014-08-05 08:45:28 +00:00
# Open port 5432 so linked containers can see them
2014-05-04 19:14:46 +00:00
EXPOSE 5432
2020-01-21 11:03:14 +00:00
2020-02-20 18:08:49 +00:00
# Copy scripts
2021-11-28 05:38:57 +00:00
ADD ./scripts /scripts
2020-04-14 17:00:36 +00:00
WORKDIR /scripts
RUN chmod +x *.sh
2020-02-20 18:08:49 +00:00
2014-05-04 19:14:46 +00:00
# Run any additional tasks here that are too tedious to put in
# this dockerfile directly.
2020-02-20 18:08:49 +00:00
RUN set -eux \
2024-09-26 12:01:05 +00:00
&& /scripts/setup.sh; rm /scripts/.pass_*
2021-06-10 16:27:48 +00:00
RUN echo 'figlet -t "Kartoza Docker PostGIS"' >> ~/.bashrc
2018-03-21 20:53:39 +00:00
2024-09-26 12:01:05 +00:00
2025-08-08 08:00:02 +00:00
ENTRYPOINT [ "/bin/bash" , "-c" , "/scripts/docker-entrypoint.sh" ]
2025-08-08 06:39:07 +00:00
2021-01-27 18:06:59 +00:00
2024-09-26 12:01:05 +00:00
2021-01-27 18:06:59 +00:00
##############################################################################
# Testing Stage #
##############################################################################
FROM postgis-prod AS postgis-test
2021-11-28 05:38:57 +00:00
COPY ./scenario_tests/utils/requirements.txt /lib/utils/requirements.txt
2021-01-27 18:06:59 +00:00
RUN set -eux \
&& export DEBIAN_FRONTEND = noninteractive \
&& apt-get update \
2023-02-01 04:42:32 +00:00
&& apt-get -y --no-install-recommends install python3-pip procps \
2021-01-27 18:06:59 +00:00
&& apt-get -y --purge autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
2023-08-06 03:02:07 +00:00
RUN pip3 install -r /lib/utils/requirements.txt --break-system-packages