From c30dee363f389998e533821a314a89a35c54b74c Mon Sep 17 00:00:00 2001 From: Tim Sutton Date: Thu, 2 Oct 2014 16:45:01 +0200 Subject: [PATCH 1/2] Fixes for specifying custom user / pass / volume options at start --- run-postgis-docker.sh | 92 +++++++++++++++++++++++++++++++------------ start-postgis.sh | 9 +++-- 2 files changed, 71 insertions(+), 30 deletions(-) mode change 100644 => 100755 start-postgis.sh diff --git a/run-postgis-docker.sh b/run-postgis-docker.sh index 45abbcb..d791257 100755 --- a/run-postgis-docker.sh +++ b/run-postgis-docker.sh @@ -1,43 +1,83 @@ #!/bin/bash # Commit and redeploy the user map container -# Note this script hosts the postgis cluster on the host filesystem -# If you want to use the container with the cluster embedded -# In the container, run it like this: +usage() +{ +cat << EOF +usage: $0 options + +This script runs a new docker postgis instance for you. + +OPTIONS: + -h Show this message + -n Container name + -v Volume to mount the Postgres cluster into + -u Postgres user name (defaults to 'docker') + -p Postgres password (defaults to 'docker') +EOF +} + +while getopts ":h:n:v:u:p:" OPTION +do + case $OPTION in + n) + CONTAINER_NAME=${OPTARG} + ;; + v) + VOLUME=${OPTARG} + ;; + u) + PGUSER=${OPTARG} + ;; + p) + PGPASSWORD=${OPTARG} + ;; + *) + usage + exit 1 + ;; + esac +done -# -if [ $# -ne 1 ]; then - echo "Deploy the postgis container." - echo "Usage:" - echo "$0 " - echo "e.g.:" - echo "$0 2.1" - echo "Will run the container using tag version 2.1" - echo "Once it is running see the commit-and-deploy.sh script if you" - echo "wish to save new snapshots." - exit 1 +if [[ -z $VOLUME ]] || [[ -z $CONTAINER_NAME ]] || [[ -z $PGUSER ]] || [[ -z $PGPASSWORD ]] +then + usage + exit 1 fi -VERSION=$1 -HOST_DATA_DIR=/var/docker-data/postgres-dat -PGUSER=qgis -PGPASS=qgis -IDFILE=/home/timlinux/postgis-current-container.id +if [[ ! -z $VOLUME ]] +then + VOLUME_OPTION="-v ${VOLUME}:/var/lib/postgresql" +else + VOLUME_OPTION="" +fi if [ ! -d $HOST_DATA_DIR ] then mkdir $HOST_DATA_DIR fi -CMD="docker run -cidfile="$IDFILE" -name="postgis" -e USERNAME=$PGUSER -e PASS=$PGPASS -d -v $HOST_DATA_DIR:/var/lib/postgresql -t qgis/postgis:$VERSION /start.sh" -echo 'Running:' +chmod a+w $HOST_DATA_DIR + +docker kill ${CONTAINER_NAME} +docker rm ${CONTAINER_NAME} + +CMD="docker run --name="${CONTAINER_NAME}" \ + --hostname="${CONTAINER_NAME}" \ + --restart=always \ + -e USERNAME=${PGUSER} \ + -e PASS=${PGPASSWORD} \ + -d -t \ + ${VOLUME_OPTION} \ + kartoza/postgis /start-postgis.sh" + +echo 'Running\n' echo $CMD eval $CMD -NEWID=`cat $IDFILE` -echo "Postgis has been deployed as $NEWID" -docker ps -a | grep $NEWID -echo "If there was no pre-existing database, you can access this using" -IPADDRESS=`docker inspect postgis | grep IPAddress | grep -o '[0-9\.]*'` + +docker ps | grep ${CONTAINER_NAME} + +echo "Connect using:" echo "psql -l -p 5432 -h $IPADDRESS -U $PGUSER" echo "and password $PGPASS" echo diff --git a/start-postgis.sh b/start-postgis.sh old mode 100644 new mode 100755 index 294c36a..8cf9aa0 --- a/start-postgis.sh +++ b/start-postgis.sh @@ -2,6 +2,8 @@ # This script will run as the postgres user due to the Dockerfile USER directive +set -x + DATADIR="/var/lib/postgresql/9.3/main" CONF="/etc/postgresql/9.3/main/postgresql.conf" POSTGRES="/usr/lib/postgresql/9.3/bin/postgres" @@ -35,18 +37,17 @@ fi # Make sure we have a user set up if [ -z "$USERNAME" ]; then - USERNAME=postgis + USERNAME=docker fi if [ -z "$PASS" ]; then - PASS=postgis - #PASS=`pwgen -c -n -1 12` + PASS=docker fi # redirect user/pass into a file so we can echo it into # docker logs when container starts # so that we can tell user their password echo "postgresql user: $USERNAME" > /tmp/PGPASSWORD.txt echo "postgresql password: $PASS" >> /tmp/PGPASSWORD.txt -$POSTGRES --single -D $DATADIR -c config_file=$CONF" <<< "CREATE USER $USERNAME WITH SUPERUSER ENCRYPTED PASSWORD '$PASS'; +$POSTGRES --single -D $DATADIR -c config_file=$CONF <<< "CREATE USER $USERNAME WITH SUPERUSER ENCRYPTED PASSWORD '$PASS';" trap "echo \"Sending SIGTERM to postgres\"; killall -s SIGTERM postgres" SIGTERM From 0348c41a815d909357e212acfae283eaadf6ade8 Mon Sep 17 00:00:00 2001 From: Tim Sutton Date: Thu, 2 Oct 2014 16:52:45 +0200 Subject: [PATCH 2/2] Updated readme --- README.md | 32 ++++++++++++++++++++++++++++++++ start-postgis.sh | 2 -- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e870add..7052ca1 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,38 @@ To create a running container do: sudo docker run --name "postgis" -p 25432:5432 -d -t kartoza/postgis ``` +You can also use the following environment variables to pass a +user name and password. + +* -e USERNAME= +* -e PASS= + +These will be used to create a new superuser with +your preferred credentials. If these are not specified then the postgresql +user is set to 'docker' with password 'docker'. + +## Convenience run script + +For convenience we have provided a bash script for running this container +that lets you specify a volume mount point and a username / password +for the new instance superuser. It takes these options: + +``` +OPTIONS: + -h Show this message + -n Container name + -v Volume to mount the Postgres cluster into + -u Postgres user name (defaults to 'docker') + -p Postgres password (defaults to 'docker') +``` + +Example usage: + +``` +./run-postgis-docker.sh -v /tmp/foo/ -n postgis -u foo -p bar + +``` + ## Connect via psql Connect with psql (make sure you first install postgresql client tools on your diff --git a/start-postgis.sh b/start-postgis.sh index 8cf9aa0..82e3c0a 100755 --- a/start-postgis.sh +++ b/start-postgis.sh @@ -2,8 +2,6 @@ # This script will run as the postgres user due to the Dockerfile USER directive -set -x - DATADIR="/var/lib/postgresql/9.3/main" CONF="/etc/postgresql/9.3/main/postgresql.conf" POSTGRES="/usr/lib/postgresql/9.3/bin/postgres"