Skip to content

Instantly share code, notes, and snippets.

@shazow
Created July 3, 2014 20:30
Show Gist options
  • Save shazow/ad5009571eea96c51622 to your computer and use it in GitHub Desktop.
Save shazow/ad5009571eea96c51622 to your computer and use it in GitHub Desktop.
PostgreSQL Dockerfile
# DOCKER-VERSION 0.6.1
# Borrowed from https://github.com/vvlad/dockerfiles-postgresql-9.3
#
# First run:
# $ docker build -t postgresql -rm=true .
# $ ID=$(docker run -e "CREATE_DB=foo" -v "path/to/socks:/var/run/postgresql" -d postgresql)
# $ docker wait $ID
# $ docker logs $ID
FROM ubuntu:12.04
MAINTAINER Andrey Petrov "[email protected]"
ENV DEBIAN_FRONTEND noninteractive
RUN locale-gen en_US.UTF-8
ENV LC_ALL en_US.UTF-8
# Suspend services autostart
RUN echo "#!/bin/sh\nexit 101" > /usr/sbin/policy-rc.d; chmod +x /usr/sbin/policy-rc.d
RUN echo "deb http://us.archive.ubuntu.com/ubuntu/ precise universe" >> /etc/apt/sources.list
# Install required packages
RUN apt-get -y install wget
RUN wget --quiet -O - "http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc" | apt-key add -
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN apt-get -y update
RUN apt-get -y install postgresql-9.3
RUN pg_dropcluster 9.3 main --stop
ADD init.sh /init.sh
EXPOSE 5432
VOLUME ["/var/lib/postgresql", "/etc/postgresql", "/var/run/postgresql"]
CMD ["/init.sh"]
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
IP_ADDRESS="$(ip addr show eth0 | grep 'inet ' | awk '{ print $2 }' | cut -d '/' -f1)"
IP_SUBNET="$(echo $IP_ADDRESS | cut -d '.' -f1-3).0/16"
POSTGRES_BIN="/usr/lib/postgresql/9.3/bin/postgres"
POSTGRES_CONFIG="/etc/postgresql/9.3/main/postgresql.conf"
if [ ! -d "/var/lib/postgresql/9.3/main" ] ; then
pg_createcluster 9.3 main
cat > "/etc/postgresql/9.3/main/pg_hba.conf" << EOF
local all postgres trust
host all all $IP_SUBNET md5
EOF
pg_ctlcluster 9.3 main start
password=${MASTER_PASSWORD:-$(< /dev/urandom tr -dc A-Za-z0-9 | head -c 16)}
psql -U postgres -c "ALTER USER postgres WITH PASSWORD '$password';" &> /dev/null
psql -U postgres -c "CREATE DATABASE db;"
pg_ctlcluster 9.3 main stop
echo "* Credentials: url=\"postgresql://postgres:$password@$IP_ADDRESS/db\""
exit 0
fi
su postgres -c "$POSTGRES_BIN -c config_file=\"$POSTGRES_CONFIG\" -c listen_addresses='*'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment