diff --git a/.example.env b/.example.env new file mode 100644 index 0000000..14f0d9c --- /dev/null +++ b/.example.env @@ -0,0 +1,16 @@ +## Example environment file for docker-compose and builders + +COMPOSE_PROJECT_NAME=postgis + +## For build arguments +DISTRO=debian +IMAGE_VERSION=bullseye +IMAGE_VARIANT=slim +# Set GENERATE_ALL_LOCALE to empty value or 0 to build just default LOCALE: en_US.UTF-8 +GENERATE_ALL_LOCALE=1 +# Set the language if you need to specify LANG locale at build time +LANG=en_US.UTF-8 + +POSTGRES_MAJOR_VERSION=13 +POSTGIS_MAJOR_VERSION=3 +POSTGIS_MINOR_RELEASE=1 diff --git a/.github/workflows/build-latest.yaml b/.github/workflows/build-latest.yaml index 92759ca..fc76b3b 100644 --- a/.github/workflows/build-latest.yaml +++ b/.github/workflows/build-latest.yaml @@ -56,20 +56,21 @@ jobs: id: docker_build_base uses: docker/build-push-action@v2 with: - context: base_build - file: base_build/Dockerfile + context: . + file: Dockerfile push: false - load: true tags: ${{ secrets.DOCKERHUB_REPO }}/postgis:base-${{ matrix.distro }}-${{ matrix.imageVersion }}-${{ matrix.imageVariant }} build-args: | - GENERATE_ALL_LOCALE=0 DISTRO=${{ matrix.distro }} IMAGE_VERSION=${{ matrix.imageVersion }} IMAGE_VARIANT=${{ matrix.imageVariant }} + GENERATE_ALL_LOCALE=0 + POSTGRES_MAJOR_VERSION=${{ matrix.postgresMajorVersion }} + POSTGIS_MAJOR_VERSION=${{ matrix.postgisMajorVersion }} + POSTGIS_MINOR_VERSION=${{ matrix.postgresMinorVersion }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache - - - run: docker images -a + target: postgis-base - name: Build prod image id: docker_build_prod @@ -82,14 +83,15 @@ jobs: tags: ${{ secrets.DOCKERHUB_REPO }}/postgis:${{ matrix.postgresMajorVersion }}-${{ matrix.postgisMajorVersion }}.${{ matrix.postgisMinorRelease }} build-args: | DISTRO=${{ matrix.distro }} - IMAGE_OWNER=${{ secrets.DOCKERHUB_REPO }} IMAGE_VERSION=${{ matrix.imageVersion }} IMAGE_VARIANT=${{ matrix.imageVariant }} + GENERATE_ALL_LOCALE=0 POSTGRES_MAJOR_VERSION=${{ matrix.postgresMajorVersion }} POSTGIS_MAJOR_VERSION=${{ matrix.postgisMajorVersion }} POSTGIS_MINOR_VERSION=${{ matrix.postgresMinorVersion }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache + target: postgis-prod run-scenario-tests: runs-on: ubuntu-latest @@ -126,8 +128,8 @@ jobs: restore-keys: | buildx-${{ matrix.distro }}-${{ matrix.imageVersion }}-${{ matrix.imageVariant }}- - - name: Build base image for testing - id: docker_build_base + - name: Build image for testing + id: docker_build_testing_image uses: docker/build-push-action@v2 with: context: . @@ -137,23 +139,14 @@ jobs: tags: kartoza/postgis:manual-build build-args: | DISTRO=${{ matrix.distro }} - IMAGE_OWNER=kartoza IMAGE_VERSION=${{ matrix.imageVersion }} IMAGE_VARIANT=${{ matrix.imageVariant }} + GENERATE_ALL_LOCALE=0 POSTGRES_MAJOR_VERSION=${{ matrix.postgresMajorVersion }} POSTGIS_MAJOR_VERSION=${{ matrix.postgisMajorVersion }} POSTGIS_MINOR_VERSION=${{ matrix.postgresMinorVersion }} - cache-from: type=local,src=/tmp/.buildx-cache + target: postgis-test - - name: Build image for testing - id: docker_build_testing_image - uses: docker/build-push-action@v2 - with: - context: . - file: Dockerfile.test - push: false - tags: kartoza/postgis:manual-build - load: true - name: Run scenario test run: | pushd scenario_tests/${{ matrix.scenario }} diff --git a/.github/workflows/deploy-image.yaml b/.github/workflows/deploy-image.yaml index 3335047..5f5d83d 100644 --- a/.github/workflows/deploy-image.yaml +++ b/.github/workflows/deploy-image.yaml @@ -61,17 +61,21 @@ jobs: id: docker_build_base uses: docker/build-push-action@v2 with: - context: base_build - file: base_build/Dockerfile + context: . + file: Dockerfile push: true - load: true tags: ${{ secrets.DOCKERHUB_REPO }}/postgis:base-${{ matrix.distro }}-${{ matrix.imageVersion }}-${{ matrix.imageVariant }} build-args: | DISTRO=${{ matrix.distro }} IMAGE_VERSION=${{ matrix.imageVersion }} IMAGE_VARIANT=${{ matrix.imageVariant }} + GENERATE_ALL_LOCALE=1 + POSTGRES_MAJOR_VERSION=${{ matrix.postgresMajorVersion }} + POSTGIS_MAJOR_VERSION=${{ matrix.postgisMajorVersion }} + POSTGIS_MINOR_VERSION=${{ matrix.postgresMinorVersion }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache + target: postgis-base - name: Build prod image id: docker_build_prod @@ -84,11 +88,12 @@ jobs: tags: ${{ secrets.DOCKERHUB_REPO }}/postgis:${{ matrix.postgresMajorVersion }}-${{ matrix.postgisMajorVersion }}.${{ matrix.postgisMinorRelease }} build-args: | DISTRO=${{ matrix.distro }} - IMAGE_OWNER=${{ secrets.DOCKERHUB_REPO }} IMAGE_VERSION=${{ matrix.imageVersion }} IMAGE_VARIANT=${{ matrix.imageVariant }} + GENERATE_ALL_LOCALE=1 POSTGRES_MAJOR_VERSION=${{ matrix.postgresMajorVersion }} POSTGIS_MAJOR_VERSION=${{ matrix.postgisMajorVersion }} POSTGIS_MINOR_VERSION=${{ matrix.postgresMinorVersion }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache + target: postgis-prod diff --git a/.gitignore b/.gitignore index 05e944f..bcf9e43 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ .python-version venv __pycache__ + +.env diff --git a/Dockerfile b/Dockerfile index 54416ac..8a01f5f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,75 @@ -#--------- Generic stuff all our Dockerfiles should start with so we get caching ------------ +############################################################################## +# Base stage # +############################################################################## ARG DISTRO=debian -ARG IMAGE_OWNER=kartoza ARG IMAGE_VERSION=bullseye ARG IMAGE_VARIANT=slim -FROM $IMAGE_OWNER/postgis:base-$DISTRO-$IMAGE_VERSION-$IMAGE_VARIANT +FROM $DISTRO:$IMAGE_VERSION-$IMAGE_VARIANT AS postgis-base +MAINTAINER Tim Sutton + +# 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 \ + && apt-get -y --no-install-recommends install \ + locales gnupg2 wget ca-certificates rpl pwgen software-properties-common iputils-ping \ + apt-transport-https curl \ + && dpkg-divert --local --rename --add /sbin/initctl + + + +RUN apt-get -y update; apt-get -y install build-essential autoconf libxml2-dev zlib1g-dev netcat gdal-bin + + + +# Temporary - PostgreSQL requires this which is not available in bullseye +ADD base_build/gdal_install.sh /gdal_install.sh +RUN chmod 0755 /gdal_install.sh;/bin/bash /gdal_install.sh + +# Generating locales takes a long time. Utilize caching by runnig it by itself +# 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 +ARG LANG=en_US.UTF-8 +ENV LANG=en_US.UTF-8 \ + LANGUAGE=en_US:en \ + LC_ALL=en_US.UTF-8 + +COPY base_build/scripts/locale.gen /etc/all.locale.gen +RUN if [ -z "${GENERATE_ALL_LOCALE}" ] || [ $GENERATE_ALL_LOCALE -eq 0 ]; \ + then \ + cat /etc/all.locale.gen | grep "${LANG}" > /etc/locale.gen; \ + else \ + cp -f /etc/all.locale.gen /etc/locale.gen; \ + fi; \ + set -eux \ + && /usr/sbin/locale-gen + +RUN update-locale ${LANG} + +# Cleanup resources +RUN apt-get -y --purge autoremove \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + + +############################################################################## +# Production Stage # +############################################################################## +FROM postgis-base AS postgis-prod MAINTAINER Tim Sutton # Reset ARG for version ARG IMAGE_VERSION ARG POSTGRES_MAJOR_VERSION=13 -ARG POSTGIS_MAJOR=3 +ARG POSTGIS_MAJOR_VERSION=3 ARG POSTGIS_MINOR_RELEASE=1 @@ -36,15 +95,15 @@ RUN set -eux \ && apt-get update \ && apt-get -y --no-install-recommends install postgresql-client-${POSTGRES_MAJOR_VERSION} \ postgresql-common postgresql-${POSTGRES_MAJOR_VERSION} \ - postgresql-${POSTGRES_MAJOR_VERSION}-postgis-${POSTGIS_MAJOR} \ + postgresql-${POSTGRES_MAJOR_VERSION}-postgis-${POSTGIS_MAJOR_VERSION} \ netcat postgresql-${POSTGRES_MAJOR_VERSION}-ogr-fdw \ - postgresql-${POSTGRES_MAJOR_VERSION}-postgis-${POSTGIS_MAJOR}-scripts \ + postgresql-${POSTGRES_MAJOR_VERSION}-postgis-${POSTGIS_MAJOR_VERSION}-scripts \ postgresql-plpython3-${POSTGRES_MAJOR_VERSION} postgresql-${POSTGRES_MAJOR_VERSION}-pgrouting \ postgresql-server-dev-${POSTGRES_MAJOR_VERSION} postgresql-${POSTGRES_MAJOR_VERSION}-cron RUN echo $POSTGRES_MAJOR_VERSION >/tmp/pg_version.txt -RUN echo $POSTGIS_MAJOR >/tmp/pg_major_version.txt +RUN echo $POSTGIS_MAJOR_VERSION >/tmp/pg_major_version.txt RUN echo $POSTGIS_MINOR_RELEASE >/tmp/pg_minor_version.txt ENV \ PATH="$PATH:/usr/lib/postgresql/${POSTGRES_MAJOR_VERSION}/bin" @@ -76,3 +135,21 @@ RUN set -eux \ VOLUME /var/lib/postgresql ENTRYPOINT /scripts/docker-entrypoint.sh + + +############################################################################## +# Testing Stage # +############################################################################## +FROM postgis-prod AS postgis-test + +COPY scenario_tests/utils/requirements.txt /lib/utils/requirements.txt + +RUN set -eux \ + && export DEBIAN_FRONTEND=noninteractive \ + && apt-get update \ + && apt-get -y --no-install-recommends install python3-pip \ + && apt-get -y --purge autoremove \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +RUN pip3 install -r /lib/utils/requirements.txt diff --git a/Dockerfile.test b/Dockerfile.test deleted file mode 100644 index c897705..0000000 --- a/Dockerfile.test +++ /dev/null @@ -1,16 +0,0 @@ -#--------- Generic stuff all our Dockerfiles should start with so we get caching ------------ -FROM kartoza/postgis:manual-build - -# For testing - -COPY scenario_tests/utils/requirements.txt /lib/utils/requirements.txt - -RUN set -eux \ - && export DEBIAN_FRONTEND=noninteractive \ - && apt-get update \ - && apt-get -y --no-install-recommends install python3-pip \ - && apt-get -y --purge autoremove \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* - -RUN pip3 install -r /lib/utils/requirements.txt diff --git a/base_build/Dockerfile b/base_build/Dockerfile deleted file mode 100644 index fcbb2d3..0000000 --- a/base_build/Dockerfile +++ /dev/null @@ -1,57 +0,0 @@ -#--------- Generic stuff all our Dockerfiles should start with so we get caching ------------ -ARG DISTRO=debian -ARG IMAGE_VERSION=bullseye -ARG IMAGE_VARIANT=slim -FROM $DISTRO:$IMAGE_VERSION-$IMAGE_VARIANT -MAINTAINER Tim Sutton - -# 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 \ - && apt-get -y --no-install-recommends install \ - locales gnupg2 wget ca-certificates rpl pwgen software-properties-common iputils-ping \ - apt-transport-https curl \ - && dpkg-divert --local --rename --add /sbin/initctl - - - -RUN apt-get -y update; apt-get -y install build-essential autoconf libxml2-dev zlib1g-dev netcat gdal-bin - - - -# Temporary - PostgreSQL requires this which is not available in bullseye -ADD gdal_install.sh /gdal_install.sh -RUN chmod 0755 /gdal_install.sh;/bin/bash /gdal_install.sh - -# Generating locales takes a long time. Utilize caching by runnig it by itself -# 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 -ENV LANG=en_US.UTF-8 \ - LANGUAGE=en_US:en \ - LC_ALL=en_US.UTF-8 - -COPY scripts/locale.gen /etc/all.locale.gen -RUN if [ -z "${GENERATE_ALL_LOCALE}" ] || [ $GENERATE_ALL_LOCALE -eq 0 ]; \ - then \ - cat /etc/all.locale.gen | grep "${LANG}" > /etc/locale.gen; \ - else \ - cp -f /etc/all.locale.gen /etc/locale.gen; \ - fi; \ - set -eux \ - && /usr/sbin/locale-gen - -RUN update-locale ${LANG} - -# Cleanup resources -RUN apt-get -y --purge autoremove \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* - diff --git a/base_build/build.sh b/base_build/build.sh deleted file mode 100755 index 53c3c7f..0000000 --- a/base_build/build.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash -# Building an debian base image - -docker build --build-arg DISTRO=debian --build-arg IMAGE_VERSION=bullseye --build-arg IMAGE_VARIANT=slim -t kartoza/postgis:base-debian-bullseye-slim . diff --git a/build-base.sh b/build-base.sh new file mode 100755 index 0000000..670b3cb --- /dev/null +++ b/build-base.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# Building an debian base image + +docker-compose -f docker-compose.build.yml build postgis-base + diff --git a/build-test.sh b/build-test.sh index f01d44c..cba9093 100755 --- a/build-test.sh +++ b/build-test.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash +# For scenario testing purposes -./build.sh - -docker build -t kartoza/postgis:manual-build -f Dockerfile.test . +docker-compose -f docker-compose.build.yml build postgis-test diff --git a/build.sh b/build.sh index 1cf2ea1..da239a6 100755 --- a/build.sh +++ b/build.sh @@ -1,9 +1,3 @@ #!/usr/bin/env bash -POSTGRES_MAJOR_VERSION=13 -POSTGIS_MINOR_RELEASE=1 -pushd base_build -./build.sh -popd -docker build -t kartoza/postgis:manual-build . -docker build --build-arg DISTRO=debian --build-arg IMAGE_VERSION=bullseye --build-arg IMAGE_VARIANT=slim -t kartoza/postgis:${POSTGRES_MAJOR_VERSION}.${POSTGIS_MINOR_RELEASE} . +docker-compose -f docker-compose.build.yml build postgis-prod diff --git a/docker-compose.build.yml b/docker-compose.build.yml new file mode 100644 index 0000000..5bbea86 --- /dev/null +++ b/docker-compose.build.yml @@ -0,0 +1,33 @@ +# Used solely for docker-compose build +version: '3' +services: + postgis-base: + image: kartoza/postgis:base-${DISTRO}-${IMAGE_VERSION}-${IMAGE_VARIANT} + build: + context: . + # Use yaml anchor for reusability + args: &build-args-anchor + DISTRO: ${DISTRO} + IMAGE_VERSION: ${IMAGE_VERSION} + IMAGE_VARIANT: ${IMAGE_VARIANT} + GENERATE_ALL_LOCALE: ${GENERATE_ALL_LOCALE} + POSTGRES_MAJOR_VERSION: ${POSTGRES_MAJOR_VERSION} + POSTGIS_MAJOR_VERSION: ${POSTGIS_MAJOR_VERSION} + POSTGIS_MINOR_RELEASE: ${POSTGIS_MINOR_RELEASE} + target: postgis-base + + postgis-prod: + image: kartoza/postgis:${POSTGRES_MAJOR_VERSION}-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_RELEASE} + build: + context: . + args: + <<: *build-args-anchor + target: postgis-prod + + postgis-test: + image: kartoza/postgis:manual-build + build: + context: . + args: + <<: *build-args-anchor + target: postgis-test