Commit b4cd2add7ad2e320c6ec1ec53a5ab391045cfc60
0 parents
Exists in
master
First commit
Showing
4 changed files
with
80 additions
and
0 deletions
Show diff stats
| 1 | +++ a/README.md | |
| ... | ... | @@ -0,0 +1,59 @@ |
| 1 | +# SPB Database Container | |
| 2 | + | |
| 3 | +This project allows the creation of a docker container running on Postgresql | |
| 4 | +8.4 and populated with the provided SPB dump. This container already exposes | |
| 5 | +the port 5432 so you may link it with other docker containers. If you wish to | |
| 6 | +access the database from your host machine you still need to use the -p | |
| 7 | +parameter to forward the port. | |
| 8 | + | |
| 9 | +## Building the image | |
| 10 | + | |
| 11 | +To build the image you need to provide 2 sql files: | |
| 12 | +* db.pgdump: the plain sql from spb database which you can generate with a `pg_dump -t table1 -t table2 ... > db.pgdump` | |
| 13 | +* db.pgdump.globals: the globals from the database which you can generate with a `pg_dumpall --globals-only > db.pgdump.globals` | |
| 14 | + | |
| 15 | +Then run: | |
| 16 | +```shell | |
| 17 | +# docker build . -t <image-name> | |
| 18 | +``` | |
| 19 | + | |
| 20 | +## Running the image | |
| 21 | + | |
| 22 | +To run the image you just need to execute: | |
| 23 | +```shell | |
| 24 | +# docker run -d <image-name> | |
| 25 | +``` | |
| 26 | + | |
| 27 | +The -d option will run the container as a daemon | |
| 28 | + | |
| 29 | +## Running the image to access it from the host machine | |
| 30 | + | |
| 31 | +```shell | |
| 32 | +# docker run -d -p <container-port>:<host-port> <image-name> | |
| 33 | +``` | |
| 34 | + | |
| 35 | +Now you can access the database from your host machine on <host-port>. | |
| 36 | + | |
| 37 | +## Linking the container with other containers | |
| 38 | + | |
| 39 | +To link the container with other containers, run: | |
| 40 | +```shell | |
| 41 | +# docker run -d --name <alias> <image-name> | |
| 42 | +``` | |
| 43 | + | |
| 44 | +Now to connect this container with other container you just need to run: | |
| 45 | +```shell | |
| 46 | +# docker run -d --name <alias> <image-name> | |
| 47 | +# docker run --link <alias>:<alias> <another-image> | |
| 48 | +``` | |
| 49 | + | |
| 50 | +After this, your second container will have access to the database through host | |
| 51 | +defined as the environment variable `<ALIAS>_PORT_5432_TCP_ADDR` and port | |
| 52 | +`<ALIAS>_PORT_5432_TCP_PORT`. | |
| 53 | + | |
| 54 | +E.g: if I use the alias `db` for my database container, on the other container | |
| 55 | +I would provided with `DB_PORT_5432_TCP_ADDR` and `DB_PORT_5432_TCP_PORT` | |
| 56 | +environment variables. With this information I could access the database with a simple: | |
| 57 | +```shell | |
| 58 | +$ psql -h "$DB_PORT_5432_TCP_ADDR" -p "$DB_PORT_5432_TCP_PORT" -U postgres -d spb | |
| 59 | +``` | ... | ... |
| 1 | +++ a/init-db.sh | |
| ... | ... | @@ -0,0 +1,16 @@ |
| 1 | +#!/bin/bash | |
| 2 | + | |
| 3 | +: ${DB_USER:=service0} | |
| 4 | +: ${DB_NAME:=spb} | |
| 5 | +: ${DB_ENCODING:=UTF-8} | |
| 6 | +: ${DB_PG_DUMP_FILE:=/tmp/db.pgdump} | |
| 7 | +: ${DB_PG_DUMP_GLOBALS_FILE:=/tmp/db.pgdump.globals} | |
| 8 | + | |
| 9 | +{ gosu postgres postgres --single -jE <<-EOSQL | |
| 10 | + CREATE USER "$DB_USER"; | |
| 11 | +EOSQL | |
| 12 | +} && { gosu postgres postgres --single -jE <<-EOSQL | |
| 13 | + CREATE DATABASE "$DB_NAME" WITH OWNER="$DB_USER" TEMPLATE=template0 ENCODING='$DB_ENCODING'; | |
| 14 | +EOSQL | |
| 15 | +} && { 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 | |
| 16 | +} && /bin/rm -f ${DB_PG_DUMP_FILE} | ... | ... |