Public Repository

Last pushed: a year ago
Short Description
Short description is empty for this repo.
Full Description

Dockerfile:

 FROM phusion/baseimage:0.9.11

MAINTAINER davidstacy

ENV DEBIAN_FRONTEND noninteractive
ENV TERM linux

RUN export LANGUAGE=en_US.UTF-8 \
  && export LANG=en_US.UTF-8 \
  && locale-gen --no-purge en_US.UTF-8 \
  && update-locale LANG=en_US.UTF-8 \
  && export LC_ALL=en_US.UTF-8

RUN sudo apt-get update -y \
 && sudo apt-get install -y ca-certificates wget pwgen inotify-tools \
 && sudo wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - \
 && sudo echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
 && sudo apt-get update -y

RUN sudo apt-get update -y && \
  sudo apt-get install -y \ 
  libgdal-dev \
  libspatialite-dev \
  libgeos-dev \
  liblwgeom-dev \
 && sudo apt-get install -y \ 
  build-essential \
  postgresql-9.2 \
  postgresql-server-dev-9.2 \ 
  postgresql-contrib-9.2 \
  libxml2-dev \ 
  libproj-dev \ 
  libjson0-dev \ 
  xsltproc \ 
  docbook-xsl \ 
  docbook-mathml \
  && sudo /etc/init.d/postgresql stop


# you must install postgis 2.0 manually because of a warning that pops up and breaks the run script
RUN wget http://download.osgeo.org/postgis/source/postgis-2.0.6.tar.gz \
 && tar xfz postgis-2.0.6.tar.gz
RUN cd postgis-2.0.6 \
  && ./configure \
  && make \
  && sudo make install \
  && sudo ldconfig \
  && sudo make comments-install \
  && rm /postgis-2.0.6.tar.gz

#Lastly, enable the command-line tools to work from your shell:
RUN sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/shp2pgsql \
  && sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/pgsql2shp \
  && sudo ln -sf /usr/share/postgresql-common/pg_wrapper /usr/local/bin/raster2pgsql

RUN sudo apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# setup data directory
RUN sed -i -e"s/data_directory =.*$/data_directory = '\/db'/" /etc/postgresql/9.2/main/postgresql.conf

# allow connections from anywhere
RUN echo "host    all             all             0.0.0.0/0               md5" >> /etc/postgresql/9.2/main/pg_hba.conf
RUN echo "listen_addresses = '*'" >> /etc/postgresql/9.2/main/postgresql.conf
RUN echo "port = 5432" >> /etc/postgresql/9.2/main/postgresql.conf

EXPOSE 5432
ADD scripts /scripts
RUN chmod +x /scripts/start.sh
RUN touch /firstrun

RUN mkdir /etc/service/postgresql
RUN ln -s /scripts/start.sh /etc/service/postgresql/run

VOLUME ["/db", "/var/log/postgresql", "/etc/postgresql"]

CMD ["/sbin/my_init"]

first_run.sh

#!/bin/bash

USER=${USER:-deploy}
PASS=${PASS:-$(pwgen -s -1 16)}
DB=${DB:-roadtrippers}
DATA_DIR=${DATA_DIR:-/db}


pre_start_action() {
  # Echo out info to later obtain by running `docker logs container_name`
  echo "POSTGRES_USER=$USER"
  echo "POSTGRES_PASS=$PASS"
  echo "POSTGRES_DATA_DIR=$DATA_DIR"
  if [ ! -z $DB ];then echo "POSTGRES_DB=$DB";fi

  # test if DATA_DIR has content
  if [[ ! "$(ls -A $DATA_DIR)" ]]; then
      echo "Initializing PostgreSQL at $DATA_DIR"

      # Copy the data that we generated within the container to the empty DATA_DIR.
      cp -R /var/lib/postgresql/9.2/main/* $DATA_DIR
  fi

  # Ensure postgres owns the DATA_DIR
  chown -R postgres $DATA_DIR
  # Ensure we have the right permissions set on the DATA_DIR
  chmod -R 700 $DATA_DIR
}

post_start_action() {
  echo "Creating the superuser: $USER"
  setuser postgres psql -q <<-EOF
    DROP ROLE IF EXISTS $USER;
    CREATE ROLE $USER WITH ENCRYPTED PASSWORD '$PASS';
    ALTER USER $USER WITH ENCRYPTED PASSWORD '$PASS';
    ALTER ROLE $USER WITH SUPERUSER;
    ALTER ROLE $USER WITH LOGIN;
EOF

  # create database if requested
  echo "Creating the database: $DB"
  if [ ! -z "$DB" ]; then
    for db in $DB; do
      echo "Creating database: $db"
      setuser postgres psql -q <<-EOF
      CREATE DATABASE $db WITH OWNER=$USER ENCODING='UTF8';
      GRANT ALL ON DATABASE $db TO $USER
EOF
    done
  else
    echo "DB not created: $DB"
  fi

  for db in $DB; do
    echo "Installing extension for $db: fuzzystrmatch and postgis"
    # enable the extension for the user's database
    setuser postgres psql $db <<-EOF
    CREATE EXTENSION postgis;
    CREATE EXTENSION fuzzystrmatch;
EOF
  done

  rm /firstrun
}

normal_run.sh:

pre_start_action() {
  : # No-op
}

post_start_action() {
  : # No-op
}

start.sh:

#!/bin/bash
# Starts up postgresql within the container.

# Stop on error
set -e

DATA_DIR=/db

if [[ -e /firstrun ]]; then
  source /scripts/first_run.sh
else
  source /scripts/normal_run.sh
fi

wait_for_postgres_and_run_post_start_action() {
  # Wait for postgres to finish starting up first.
  while [[ ! -e /run/postgresql/9.2-main.pid ]] ; do
      inotifywait -q -e create /run/postgresql/ >> /dev/null
  done

  post_start_action
}

pre_start_action

wait_for_postgres_and_run_post_start_action &

# Start PostgreSQL
echo "Starting PostgreSQL..."
setuser postgres /usr/lib/postgresql/9.2/bin/postgres -D /etc/postgresql/9.2/main
Docker Pull Command
Owner
roadtrippers