Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/sh
set -e
echo "Packing data folder: " $PGDATA
cd /zdata/
tar -cf backup.tar -C $PGDATA ./
sync
rm -rf $PGDATA/*
echo "Pack & clean finished successfully."
#!/bin/sh
set -e
if [ ! -f "$PGDATA/PG_VERSION" ]; then
echo "Restoring $PGDATA ..."
tar -xf /zdata/backup.tar -C $PGDATA
#chown -R postgres:postgres "$PGDATA"
sync
echo "Done."
else
echo "$PGDATA was already there, skipping restore."
fi
echo "Launching command: $@ ..."
if [ "$1" = 'postgres' ]; then
gosu postgres "$@"
else
exec "$@"
fi
#!/bin/sh
set -e
if [ ! -d "$PGDATA" ]; then
mkdir -p ${PGDATA}
fi
chown -R postgres:postgres "$PGDATA"
gosu postgres initdb
sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf
: ${POSTGRES_USER:=$POSTGRES_USER}
: ${POSTGRES_DB:=$POSTGRES_DB}
if [ "$POSTGRES_PASSWORD" ]; then
pass="PASSWORD '$POSTGRES_PASSWORD'"
authMethod=md5
else
echo "==============================="
echo "!!! Use \$POSTGRES_PASSWORD env var to secure your database !!!"
echo "==============================="
pass=
authMethod=trust
fi
echo
createSql="CREATE DATABASE $POSTGRES_DB;"
echo $createSql | gosu postgres postgres --single -jE
echo
op=CREATE
userSql="$op USER $POSTGRES_USER WITH SUPERUSER $pass;"
echo $userSql | gosu postgres postgres --single -jE
echo
# internal start of server in order to allow set-up using psql-client
# does not listen on TCP/IP and waits until start finishes
gosu postgres pg_ctl -D "$PGDATA" \
-o "-c listen_addresses=''" \
-w start
echo
cd ./data-scripts.d/
for f in *; do
case "$f" in
*.sql) echo "$0: running $f"; psql --quiet --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --file="$f" &> /dev/null;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
cd ../
sync
gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA"/pg_hba.conf
sync
# source: https://github.com/kiasaki/docker-alpine-postgres
FROM alpine
RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories && \
apk update && \
apk add curl "libpq@edge<9.7" "postgresql-client@edge<9.7" "postgresql@edge<9.7" "postgresql-contrib@edge<9.7" && \
curl -o /usr/local/bin/gosu -sSL "https://github.com/tianon/gosu/releases/download/1.2/gosu-amd64" && \
chmod +x /usr/local/bin/gosu && \
apk del curl && \
rm -rf /var/cache/apk/*
ENV LANG en_US.utf8
ENV PGDATA /var/lib/postgresql/data/
ENV POSTGRES_DB datastore
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD letmein
RUN mkdir -p /opt/setup/data-scripts.d/
RUN mkdir -p /zdata/
COPY ./data-scripts.d/* /opt/setup/data-scripts.d/
WORKDIR /opt/setup/
COPY db-setup.sh /opt/setup/
COPY db-pack.sh /opt/setup/
COPY db-run.sh /opt/setup/
RUN ./db-setup.sh
RUN ./db-pack.sh
VOLUME $PGDATA
EXPOSE 5432
ENTRYPOINT [ "/opt/setup/db-run.sh" ]
CMD [ "postgres" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.