Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added docker multistage build #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------

Expand Down
43 changes: 43 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM ubuntu:18.04

LABEL MAINTAINER [email protected]

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 | \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
why isn't this using the files from the very git repository you want to have this merged into? Pulling a (versioned) tarball from github instead doesn't seem right.
Alternatively, couldn't you just "apt-get install postgresql-$PG_VERSION-unit" instead?
Christoph

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh! My mistake...
I didn't check the postgresql repos for your unit extension, just the ubuntu repos :(
Through the docker build, you get the flexibility to test different releases of your extension with different distros and psql versions, but the fact, that the extension is available in the psql apt repo ist making this pull request nearly obsolete...
Nevertheless a Docker Hub Image would be nice and is easily possible with this Dockerfile.

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 link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it run make install when it then proceeds to fetch the files from /opt?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make install is just for checking that the package installs right - indeed not necessary.
Should be commented out, I used it just for development purposes.

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/
20 changes: 20 additions & 0 deletions docker/initdb-unit.sh
Original file line number Diff line number Diff line change
@@ -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