From 62f90ad46164093566ca08690c119cad96e0485b Mon Sep 17 00:00:00 2001
From: Mats Hansen <mats.hansen@zenner-iot.com>
Date: Thu, 18 Jul 2019 12:57:52 +0200
Subject: [PATCH] added docker multistage build

---
 README.md             | 27 +++++++++++++++++++++++++++
 docker/Dockerfile     | 43 +++++++++++++++++++++++++++++++++++++++++++
 docker/initdb-unit.sh | 20 ++++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 docker/Dockerfile
 create mode 100644 docker/initdb-unit.sh

diff --git a/README.md b/README.md
index 5c6d229..cc2eed5 100644
--- a/README.md
+++ b/README.md
@@ -114,6 +114,33 @@ make PG_CONFIG=/usr/lib/postgresql/10/bin/pg_config
 sudo make install PG_CONFIG=/usr/lib/postgresql/10/bin/pg_config
 ```
 
+Docker
+------
+
+To try out this extension, there is an easy way with docker.
+
+This is a [multi-stage build](https://docs.docker.com/develop/develop-images/multistage-build/) ontop of the [official postgres docker image](https://hub.docker.com/_/postgres).
+
+You can easily switch the PostgreSQL Version in the [Dockerfile](docker/Dockerfile).
+
+```
+docker build -t postgresql-unit docker/
+
+docker run --name postgresql-unit -p 127.0.0.1:5432:5432 -d postgresql-unit
+
+# wait a few seconds for the database to setup...
+docker exec -itu postgres postgresql-unit psql
+psql (11.4 (Debian 11.4-1.pgdg90+1))
+Type "help" for help.
+
+postgres=# SELECT '2 m/s'::unit@'km/h';
+ ?column?
+----------
+ 7.2 km/h
+(1 row)
+```
+or connect with your favorite database client to `127.0.0.1:5432` (user: `postgres` pw: `postgres`).
+
 Config
 ------
 
diff --git a/docker/Dockerfile b/docker/Dockerfile
new file mode 100644
index 0000000..684ec50
--- /dev/null
+++ b/docker/Dockerfile
@@ -0,0 +1,43 @@
+FROM ubuntu:18.04
+
+LABEL MAINTAINER mh@zenner-iot.com
+
+ARG PG_VERSION=11
+ARG PG_UNIT_VERSION=7.1
+ARG PG_CONFIG=/usr/lib/postgresql/$PG_VERSION/bin/pg_config
+
+RUN apt-get update && \
+    apt-get install -y \
+      build-essential \
+      lsb-release \
+      flex \
+      bison \
+      curl && \
+    rm -rf /var/lib/apt/lists/*
+
+RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
+    curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
+
+RUN apt-get update && \
+    apt-get install -y \
+      postgresql-server-dev-$PG_VERSION && \
+    rm -rf /var/lib/apt/lists/*
+
+WORKDIR /opt/postgresql-unit
+
+RUN curl -fsSL https://github.com/df7cb/postgresql-unit/archive/$PG_UNIT_VERSION.tar.gz | \
+    tar --strip-components 1 -xzC /opt/postgresql-unit/
+
+RUN make
+RUN make install
+
+FROM postgres:11
+
+COPY --from=0 /opt/postgresql-unit/unit.so /usr/lib/postgresql/$PG_MAJOR/lib/
+COPY --from=0 /opt/postgresql-unit/unit_prefixes.data \
+              /opt/postgresql-unit/unit_units.data \
+              /opt/postgresql-unit/unit.control \
+              /opt/postgresql-unit/unit--*.sql \
+              /usr/share/postgresql/$PG_MAJOR/extension/
+
+COPY  ./initdb-unit.sh /docker-entrypoint-initdb.d/
diff --git a/docker/initdb-unit.sh b/docker/initdb-unit.sh
new file mode 100644
index 0000000..460e76d
--- /dev/null
+++ b/docker/initdb-unit.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+set -e
+
+# Perform all actions as $POSTGRES_USER
+export PGUSER="$POSTGRES_USER"
+
+# Create the 'template_unit' template db
+"${psql[@]}" <<- 'EOSQL'
+CREATE DATABASE template_unit;
+UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_unit';
+EOSQL
+
+# Load Unit into both template_database and $POSTGRES_DB
+for DB in template_unit "$POSTGRES_DB"; do
+        echo "Loading Unit extensions into $DB"
+        "${psql[@]}" --dbname="$DB" <<-'EOSQL'
+                CREATE EXTENSION IF NOT EXISTS unit;
+EOSQL
+done