From 24fbd7205dff73f5f09838881b87b1ba775dc283 Mon Sep 17 00:00:00 2001 From: Jason Michalski Date: Tue, 15 May 2018 16:04:50 -0700 Subject: [PATCH] docker: Add postgres docker build with full text search. This is multi-stage build which first builds tsearch-extras with the current version of postgres and then configs postgres for zulip. The zulip config installs the hunspell dictionaries, stop words file, tsearch-extras, and creates the initial database. **Testing Plan:** 1) `docker-compose up` the existing config. 2) Build the new image 3) Edit docker-compose.yml to use the new image id 4) `docker-compose up` and verify full text search is still working. --- Dockerfile-postgresql | 37 ++++++++++++++++++++++++++++++++ scripts/setup/postgres-create-db | 21 ++++++++++++++++++ scripts/setup/postgres-init-db | 8 +------ 3 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 Dockerfile-postgresql create mode 100755 scripts/setup/postgres-create-db diff --git a/Dockerfile-postgresql b/Dockerfile-postgresql new file mode 100644 index 0000000000..08792eea79 --- /dev/null +++ b/Dockerfile-postgresql @@ -0,0 +1,37 @@ +# To build run `docker build -f Dockerfile-postgresql .` from the root of the +# zulip repo. + +# Install build tools and build tsearch_extras for the current postgres +# version. Currently the postgres images do not support automatic upgrading of +# the on-disk data in volumes. So the base image can not currently be upgraded +# without users needing a manual pgdump and restore. +FROM postgres:9.5 +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install -y \ + postgresql-server-dev-$PG_MAJOR \ + postgresql-server-dev-all \ + git \ + build-essential \ + fakeroot \ + devscripts +RUN git clone https://github.com/zulip/tsearch_extras.git \ + && cd tsearch_extras \ + && echo $PG_MAJOR > debian/pgversions \ + && pg_buildext updatecontrol \ + && debuild -b -uc -us + +# Install tsearch_extras, hunspell, zulip stop words, and run zulip database +# init. +FROM postgres:9.5 +ENV TSEARCH_EXTRAS_VERSION=0.4 +ENV TSEARCH_EXTRAS_DEB=postgresql-${PG_MAJOR}-tsearch-extras_${TSEARCH_EXTRAS_VERSION}_amd64.deb +COPY --from=0 /${TSEARCH_EXTRAS_DEB} /tmp +COPY puppet/zulip/files/postgresql/zulip_english.stop /usr/share/postgresql/$PG_MAJOR/tsearch_data/zulip_english.stop +COPY scripts/setup/postgres-create-db /docker-entrypoint-initdb.d/postgres-create-db.sh +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y hunspell-en-us \ + && DEBIAN_FRONTEND=noninteractive dpkg -i /tmp/${TSEARCH_EXTRAS_DEB} \ + && rm /tmp/${TSEARCH_EXTRAS_DEB} \ + && ln -sf /var/cache/postgresql/dicts/en_us.dict "/usr/share/postgresql/$PG_MAJOR/tsearch_data/en_us.dict" \ + && ln -sf /var/cache/postgresql/dicts/en_us.affix "/usr/share/postgresql/$PG_MAJOR/tsearch_data/en_us.affix" \ + && rm -rf /var/lib/apt/lists/* diff --git a/scripts/setup/postgres-create-db b/scripts/setup/postgres-create-db new file mode 100755 index 0000000000..c069c7c1aa --- /dev/null +++ b/scripts/setup/postgres-create-db @@ -0,0 +1,21 @@ +#!/bin/bash +set -e +set -x + +# Make sure the current working directory is readable +cd / + +DATABASE_CREATE=" +CREATE USER zulip; +ALTER ROLE zulip SET search_path TO zulip,public; +CREATE DATABASE zulip OWNER=zulip; +\\connect zulip +CREATE SCHEMA zulip AUTHORIZATION zulip; +CREATE EXTENSION tsearch_extras SCHEMA zulip; +" + +if [ -f /.dockerenv ]; then + echo "$DATABASE_CREATE" | psql +else + echo "$DATABASE_CREATE" | su postgres -c psql +fi diff --git a/scripts/setup/postgres-init-db b/scripts/setup/postgres-init-db index a874ac6ec2..104e2b76b1 100755 --- a/scripts/setup/postgres-init-db +++ b/scripts/setup/postgres-init-db @@ -39,16 +39,10 @@ source "$(dirname "$0")/terminate-psql-sessions" postgres zulip zulip_base cd / su "$POSTGRES_USER" -c psql <