diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fb8c312 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +db.pgdump* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6963719 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM postgres:8.4 +ADD init-db.sh /docker-entrypoint-initdb.d/ +ADD db.pgdump /tmp/db.pgdump +ADD db.pgdump.globals /tmp/db.pgdump.globals diff --git a/README.md b/README.md new file mode 100644 index 0000000..b8cdd20 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# SPB Database Container + +This project allows the creation of a docker container running on Postgresql +8.4 and populated with the provided SPB dump. This container already exposes +the port 5432 so you may link it with other docker containers. If you wish to +access the database from your host machine you still need to use the -p +parameter to forward the port. + +## Building the image + +To build the image you need to provide 2 sql files: +* db.pgdump: the plain sql from spb database which you can generate with a `pg_dump -t table1 -t table2 ... > db.pgdump` +* db.pgdump.globals: the globals from the database which you can generate with a `pg_dumpall --globals-only > db.pgdump.globals` + +Then run: +```shell +# docker build . -t +``` + +## Running the image + +To run the image you just need to execute: +```shell +# docker run -d +``` + +The -d option will run the container as a daemon + +## Running the image to access it from the host machine + +```shell +# docker run -d -p : +``` + +Now you can access the database from your host machine on . + +## Linking the container with other containers + +To link the container with other containers, run: +```shell +# docker run -d --name +``` + +Now to connect this container with other container you just need to run: +```shell +# docker run -d --name +# docker run --link : +``` + +After this, your second container will have access to the database through host +defined as the environment variable `_PORT_5432_TCP_ADDR` and port +`_PORT_5432_TCP_PORT`. + +E.g: if I use the alias `db` for my database container, on the other container +I would provided with `DB_PORT_5432_TCP_ADDR` and `DB_PORT_5432_TCP_PORT` +environment variables. With this information I could access the database with a simple: +```shell +$ psql -h "$DB_PORT_5432_TCP_ADDR" -p "$DB_PORT_5432_TCP_PORT" -U postgres -d spb +``` diff --git a/init-db.sh b/init-db.sh new file mode 100644 index 0000000..78e5ab4 --- /dev/null +++ b/init-db.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +: ${DB_USER:=service0} +: ${DB_NAME:=spb} +: ${DB_ENCODING:=UTF-8} +: ${DB_PG_DUMP_FILE:=/tmp/db.pgdump} +: ${DB_PG_DUMP_GLOBALS_FILE:=/tmp/db.pgdump.globals} + +{ gosu postgres postgres --single -jE <<-EOSQL + CREATE USER "$DB_USER"; +EOSQL +} && { gosu postgres postgres --single -jE <<-EOSQL + CREATE DATABASE "$DB_NAME" WITH OWNER="$DB_USER" TEMPLATE=template0 ENCODING='$DB_ENCODING'; +EOSQL +} && { gosu postgres pg_ctl start -w && gosu postgres psql "$DB_NAME" -f "$DB_PG_DUMP_GLOBALS_FILE" && gosu postgres psql "$DB_NAME" < "$DB_PG_DUMP_FILE" && gosu postgres pg_ctl stop -w +} && /bin/rm -f ${DB_PG_DUMP_FILE} -- libgit2 0.21.2