From 4c7b293f068506ebc6f022e5883c88be7fe9b6d4 Mon Sep 17 00:00:00 2001 From: Tim Sutton Date: Sun, 4 May 2014 21:14:46 +0200 Subject: [PATCH] Tweaks to setup scripts --- 71-apt-cacher-ng | 1 + Dockerfile | 54 ++++++++++++++++++++++++++++++ README.md | 76 +++++++++++++++++++++++++++++++++++++++++-- build.sh | 1 + commit-and-deploy.sh | 39 ++++++++++++++++++++++ postgres.conf | 5 +++ run-postgis-docker.sh | 50 ++++++++++++++++++++++++++++ setup.sh | 1 + sshd.conf | 5 +++ start-postgis.sh | 56 +++++++++++++++++++++++++++++++ 10 files changed, 285 insertions(+), 3 deletions(-) create mode 100644 71-apt-cacher-ng create mode 100644 Dockerfile create mode 100755 build.sh create mode 100755 commit-and-deploy.sh create mode 100644 postgres.conf create mode 100755 run-postgis-docker.sh create mode 100644 setup.sh create mode 100644 sshd.conf create mode 100644 start-postgis.sh diff --git a/71-apt-cacher-ng b/71-apt-cacher-ng new file mode 100644 index 0000000..354c988 --- /dev/null +++ b/71-apt-cacher-ng @@ -0,0 +1 @@ +Acquire::http { Proxy "http://192.168.1.13:3142"; }; diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9d356e1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,54 @@ +#--------- Generic stuff all our Dockerfiles should start with so we get caching ------------ +FROM ubuntu:trusty +MAINTAINER Tim Sutton + +RUN export DEBIAN_FRONTEND=noninteractive +ENV DEBIAN_FRONTEND noninteractive +RUN dpkg-divert --local --rename --add /sbin/initctl +#RUN ln -s /bin/true /sbin/initctl + +# Use local cached debs from host (saves your bandwidth!) +# Change ip below to that of your apt-cacher-ng host +# Or comment this line out if you do not with to use caching +ADD 71-apt-cacher-ng /etc/apt/apt.conf.d/71-apt-cacher-ng + +RUN echo "deb http://archive.ubuntu.com/ubuntu trusty main universe" > /etc/apt/sources.list +RUN apt-get -y update +# socat can be used to proxy an external port and make it look like it is local +RUN apt-get -y install ca-certificates socat openssh-server supervisor +RUN mkdir /var/run/sshd +ADD sshd.conf /etc/supervisor/conf.d/sshd.conf + + +RUN echo 'root:postgis' | chpasswd + +#-------------Application Specific Stuff ---------------------------------------------------- + +# Next line a workaround for https://github.com/dotcloud/docker/issues/963 +RUN apt-get install -y postgresql-9.3-postgis-2.1 +RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf +# Note: I dont think this is relevant anymore as start.sh sets up a user +# Please test and verify before removing though +RUN service postgresql start && /bin/su postgres -c "createuser -d -s -r -l docker" && /bin/su postgres -c "psql postgres -c \"ALTER USER docker WITH ENCRYPTED PASSWORD 'docker'\"" && service postgresql stop +# Listen on all ip addresses +RUN echo "listen_addresses = '*'" >> /etc/postgresql/9.3/main/postgresql.conf +RUN echo "port = 5432" >> /etc/postgresql/9.3/main/postgresql.conf + +# Start with supervisor +ADD postgres.conf /etc/supervisor/conf.d/postgres.conf + +# Open port 5432 and 22 so linked containers can see them +EXPOSE 5432 +EXPOSE 22 + +# Run any additional tasks here that are too tedious to put in +# this dockerfile directly. +ADD setup.sh /setup.sh +RUN chmod 0755 /setup.sh +RUN /setup.sh + +# We will run any commands in this when the container starts +ADD start-postgis.sh /start-postgis.sh +RUN chmod 0755 /start-postgis.sh + +CMD supervisor -n diff --git a/README.md b/README.md index 75b864c..1f947e2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,74 @@ -docker-postgis -============== +# docker-postgis -Dockerfile for postgis +A simple docker container that runs PostGIS + + +## Build + +To build the image do: + +``` +docker build -t kartoza/postgis git://github.com/timlinux/docker-postgis +``` + +Run +--- + +To create a running container do: + +``` +sudo docker run --name "postgis" -p 2222:22 -p 25432:5432 -d -t kartoza/postgis:2.1 +``` + +## Connect via psql + +To log in to your container do: + +Connect with psql (make sure you first install postgresql client tools on your +host / client): + + +``` +psql -h localhost -U docker -p 25432 -l +``` + +You can then go on to use any normal postgresql commands against the container. + +Under ubuntu 14.04 the postgresql client can be installed like this: + +``` +sudo apt-get install postgresql-client-9.3 +``` + + +# Storing data on the host rather than the container. + + +Docker volumes can be used to persist your data. + +``` +mkdir -p ~/postgres_data +docker run -d -v $HOME/postgres_data:/var/lib/postgresql kartoza/postgis` +``` + +You need to ensure the ``postgres_data`` directory has sufficinet permissions +for the docker process to read / write it. + + + +Connect via ssh +--------------- + +To log into your container do: + +``` +ssh root@localhost -p 2222 +``` + +Default ssh password is 'postgis' + + +Credits +------- +Tim Sutton (tim@linfiniti.com) +May 2014 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..6897ab4 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +docker build -t kartoza/postgis:2.1 . diff --git a/commit-and-deploy.sh b/commit-and-deploy.sh new file mode 100755 index 0000000..1334013 --- /dev/null +++ b/commit-and-deploy.sh @@ -0,0 +1,39 @@ +#!/bin/bash +# Commit and redeploy the user map container +# +if [ $# -ne 1 ]; then + echo "Commit and then redeploy the user_map container." + echo "Usage:" + echo "$0 " + echo "e.g.:" + echo "$0 1.6" + echo "Will commit the current state of the container as version 1.6" + echo "and then redeploy it." + exit 1 +fi +VERSION=$1 +HOST_DATA_DIR=/var/docker-data/postgres-data +PGUSER=qgis +PGPASS=qgis + +IDFILE=/home/timlinux/postgis-current-container.id +ID=`cat $IDFILE` +docker commit $ID qgis/postgis:$VERSION -run='{"Cmd": ["/start.sh"], "PortSpecs": ["5432"], "Hostname": "postgis"}' -author="Tim Sutton " +docker kill $ID +docker rm $ID +rm $IDFILE +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:' +echo $CMD +eval $CMD +NEWID=`cat $IDFILE` +echo "Postgis has been committed as $1 and redeployed as $NEWID" +docker ps -a | grep $NEWID +echo "If thhere was no pre-existing database, you can access this using" +IPADDRESS=`docker inspect postgis | grep IPAddress | grep -o '[0-9\.]*'` +echo "psql -l -p 5432 -h $IPADDRESS -U $PGUSER" +echo "and password $PGPASS" diff --git a/postgres.conf b/postgres.conf new file mode 100644 index 0000000..dce7a0b --- /dev/null +++ b/postgres.conf @@ -0,0 +1,5 @@ +[program:postgres] +user=root +command=/start-postgis.sh +autorestart=true +stopsignal=INT diff --git a/run-postgis-docker.sh b/run-postgis-docker.sh new file mode 100755 index 0000000..45abbcb --- /dev/null +++ b/run-postgis-docker.sh @@ -0,0 +1,50 @@ +#!/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: + + +# +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 +fi +VERSION=$1 +HOST_DATA_DIR=/var/docker-data/postgres-dat +PGUSER=qgis +PGPASS=qgis + +IDFILE=/home/timlinux/postgis-current-container.id + +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:' +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\.]*'` +echo "psql -l -p 5432 -h $IPADDRESS -U $PGUSER" +echo "and password $PGPASS" +echo +echo "Alternatively link to this container from another to access it" +echo "e.g. docker run -link postgis:pg .....etc" +echo "Will make the connection details to the postgis server available" +echo "in your app container as $PG_PORT_5432_TCP_ADDR (for the ip address)" +echo "and $PG_PORT_5432_TCP_PORT (for the port number)." + + diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..58932f6 --- /dev/null +++ b/setup.sh @@ -0,0 +1 @@ +# Add any additional setup tasks here diff --git a/sshd.conf b/sshd.conf new file mode 100644 index 0000000..dc167f4 --- /dev/null +++ b/sshd.conf @@ -0,0 +1,5 @@ +[program:sshd] +user=root +command=/usr/sbin/sshd -D +autorestart=true +stopsignal=INT diff --git a/start-postgis.sh b/start-postgis.sh new file mode 100644 index 0000000..6ee5ea1 --- /dev/null +++ b/start-postgis.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +DATADIR="/var/lib/postgresql/9.3/main" +CONF="/etc/postgresql/9.3/main/postgresql.conf" +POSTGRES="/usr/lib/postgresql/9.3/bin/postgres" +INITDB="/usr/lib/postgresql/9.3/bin/initdb" +SQLDIR="/usr/share/postgresql/9.3/contrib/postgis-2.1/" + +# test if DATADIR is existent +if [ ! -d $DATADIR ]; then + echo "Creating Postgres data at $DATADIR" + mkdir -p $DATADIR +fi + +# Note that $USERNAME and $PASS below are passed via docker run e.g. +#docker run -cidfile=/home/timlinux/postgis-current-container.id -name=postgis -e USERNAME=qgis -e PASS=qgis -d -v /var/docker-data/postgres-dat:/var/lib/postgresql -t qgis/postgis:6 /start.sh + + +# test if DATADIR has content +if [ ! "$(ls -A $DATADIR)" ]; then + echo "Initializing Postgres Database at $DATADIR" + chown -R postgres $DATADIR + su postgres sh -c "$INITDB $DATADIR" + su postgres sh -c "$POSTGRES --single -D $DATADIR -c config_file=$CONF" <<< "CREATE USER $USERNAME WITH SUPERUSER PASSWORD '$PASS';" +fi + +trap "echo \"Sending SIGTERM to postgres\"; killall -s SIGTERM postgres" SIGTERM + +su postgres sh -c "$POSTGRES -D $DATADIR -c config_file=$CONF" & + +# Wait for the db to start up before trying to use it.... + +sleep 10 + +RESULT=`su postgres sh -c "psql -l" | grep postgis | wc -l` +if [[ $RESULT == '1' ]] +then + echo 'Postgis Already There' +else + echo "Postgis is missing, installing now" + # Note the dockerfile must have put the postgis.sql and spatialrefsys.sql scripts into /root/ + # We use template0 since we want t different encoding to template1 + su postgres sh -c "createdb template_postgis -E UTF8 -T template0" + su postgres sh -c "psql template0 -c 'UPDATE pg_database SET datistemplate = TRUE WHERE datname = \'template_postgis\';'" + su postgres sh -c "psql template_postgis -f $SQLDIR/postgis.sql" + su postgres sh -c "psql template_postgis -f $SQLDIR/spatial_ref_sys.sql" + # Needed when importing old dumps using e.g ndims for constraints + su postgres sh -c "psql template_postgis -f $SQLDIR/legacy_minimal.sql" + su postgres sh -c "psql template_postgis -c 'GRANT ALL ON geometry_columns TO PUBLIC;'" + su postgres sh -c "psql template_postgis -c 'GRANT ALL ON geography_columns TO PUBLIC;'" + su postgres sh -c "psql template_postgis -c 'GRANT ALL ON spatial_ref_sys TO PUBLIC;'" + # This should show up in docker logs afterwards +fi +su postgres sh -c "psql -l" + +wait $!