From e3e57922ec08a32b5fb26ebb4271569bf050ff28 Mon Sep 17 00:00:00 2001 From: Rajshri Mohan K S Date: Thu, 26 Oct 2017 18:35:01 +0530 Subject: [PATCH 1/2] Updated for docker-compose. Made it simpler to fire up databases. --- Dockerfile | 2 - README.md | 45 ++++++------------- docker-compose.yml | 16 +++++++ .../create-multiple-postgresql-databases.sh | 2 +- 4 files changed, 30 insertions(+), 35 deletions(-) delete mode 100644 Dockerfile create mode 100644 docker-compose.yml rename create-multiple-postgresql-databases.sh => docker-entrypoint-initdb.d/create-multiple-postgresql-databases.sh (99%) mode change 100755 => 100644 diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index ee9c3d0..0000000 --- a/Dockerfile +++ /dev/null @@ -1,2 +0,0 @@ -FROM postgres:9.6 -COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ diff --git a/README.md b/README.md index 666b26f..90b60b3 100644 --- a/README.md +++ b/README.md @@ -3,48 +3,29 @@ The [official recommendation](https://hub.docker.com/_/postgres/) for creating multiple databases is as follows: -*If you would like to do additional initialization in an image derived from -this one, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts under -`/docker-entrypoint-initdb.d` (creating the directory if necessary). After the -entrypoint calls `initdb` to create the default `postgres` user and database, -it will run any `*.sql` files and source any `*.sh` scripts found in that -directory to do further initialization before starting the service.* +_If you would like to do additional initialization in an image derived from +this one, add one or more \`_.sql`,`_.sql.gz`, or`_.sh`scripts under`/docker-entrypoint-initdb.d`(creating the directory if necessary). After the +entrypoint calls`initdb`to create the default`postgres`user and database, +it will run any`_.sql`files and source any`_.sh\` scripts found in that +directory to do further initialization before starting the service.\* This directory contains a script to create multiple databases using that mechanism. ## Usage -### By mounting a volume +Open `docker-compose.yml` file and edit the `POSTGRES_MULTIPLE_DATABASES` environment variable with a list of db names you'd like to be created in the postgres container. The db names should be comma(`,`) separated. -Clone the repository, mount its directory as a volume into -`/docker-entrypoint-initdb.d` and declare database names separated by commas in -`POSTGRES_MULTIPLE_DATABASES` environment variable as follows -(`docker-compose` syntax): +Save the file and simpy run: - myapp-postgresql: - image: postgres:9.6.2 - volumes: - - ../docker-posgresql-multiple-databases:/docker-entrypoint-initdb.d - environment: - - POSTGRES_MULTIPLE_DATABASES=db1,db2 - - POSTGRES_USER=myapp - - POSTGRES_PASSWORD= + docker-compose up -### By building a custom image +to get the container up and running. Your databases would automatically be created and will be exposed by default on port `5432`. -Clone the repository, build and push the image to your Docker repository, -for example for Google Private Repository do the following: +# Credits - docker build --tag=eu.gcr.io/your-project/postgres-multi-db . - gcloud docker -- push eu.gcr.io/your-project/postgres-multi-db +Thanks to [@mrts][original-author-link] for the inspiration and [original script][original-repo-url]. -You still need to pass the `POSTGRES_MULTIPLE_DATABASES` environment variable -to the container: +[original-author-link]: https://github.com/mrts - myapp-postgresql: - image: eu.gcr.io/your-project/postgres-multi-db - environment: - - POSTGRES_MULTIPLE_DATABASES=db1,db2 - - POSTGRES_USER=myapp - - POSTGRES_PASSWORD= +[original-repo-url]: https://github.com/mrts/docker-postgresql-multiple-databases diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..389a291 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3.3' + +services: + db: + image: postgres:latest + restart: always + volumes: + - type: volume + source: ./docker-entrypoint-initdb.d + target: /docker-entrypoint-initdb.d + environment: + POSTGRES_PASSWORD: root + POSTGRES_USER: postgres + POSTGRES_MULTIPLE_DATABASES: iqz_db1,iqz_db2 + ports: + - 5432:5432 \ No newline at end of file diff --git a/create-multiple-postgresql-databases.sh b/docker-entrypoint-initdb.d/create-multiple-postgresql-databases.sh old mode 100755 new mode 100644 similarity index 99% rename from create-multiple-postgresql-databases.sh rename to docker-entrypoint-initdb.d/create-multiple-postgresql-databases.sh index e883f80..e609a18 --- a/create-multiple-postgresql-databases.sh +++ b/docker-entrypoint-initdb.d/create-multiple-postgresql-databases.sh @@ -19,4 +19,4 @@ if [ $POSTGRES_MULTIPLE_DATABASES ]; then create_user_and_database $db done echo "Multiple databases created" -fi +fi \ No newline at end of file From 8dd816a7dc4f1d51fad705b2709c9c47242dcb36 Mon Sep 17 00:00:00 2001 From: Rajshri Mohan K S Date: Thu, 25 Jan 2018 10:27:00 +0530 Subject: [PATCH 2/2] Fixed "Named volume is used in service but no declaration was found in the volumes section." error in Compose version 1.18.0. --- docker-compose.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 389a291..c6e5756 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,12 +5,10 @@ services: image: postgres:latest restart: always volumes: - - type: volume - source: ./docker-entrypoint-initdb.d - target: /docker-entrypoint-initdb.d + - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d environment: POSTGRES_PASSWORD: root POSTGRES_USER: postgres POSTGRES_MULTIPLE_DATABASES: iqz_db1,iqz_db2 ports: - - 5432:5432 \ No newline at end of file + - 5432:5432