From e97c924f635042d3d0aa1948fca6866b23e12cd1 Mon Sep 17 00:00:00 2001 From: Li Lin Date: Thu, 29 Jan 2015 13:34:59 +0800 Subject: [PATCH] tested auto create database after container startup --- atl-postgres/Dockerfile | 3 +-- atl-postgres/atl-dbinit.sh | 14 -------------- atl-postgres/build.sh | 0 atl-postgres/dbinit.sh | 15 +++++++++++++++ run.sh | 19 ++++++++++++------- 5 files changed, 28 insertions(+), 23 deletions(-) delete mode 100755 atl-postgres/atl-dbinit.sh mode change 100644 => 100755 atl-postgres/build.sh create mode 100755 atl-postgres/dbinit.sh diff --git a/atl-postgres/Dockerfile b/atl-postgres/Dockerfile index d1b0108..5dcb454 100644 --- a/atl-postgres/Dockerfile +++ b/atl-postgres/Dockerfile @@ -1,7 +1,6 @@ FROM postgres:9.3 MAINTAINER Li Lin -RUN mkdir -p /docker-entrypoint-initdb.d -ADD atl-dbinit.sh /docker-entrypoint-initdb.d +ADD dbinit.sh /docker-entrypoint-initdb.d/ diff --git a/atl-postgres/atl-dbinit.sh b/atl-postgres/atl-dbinit.sh deleted file mode 100755 index 01f0ee2..0000000 --- a/atl-postgres/atl-dbinit.sh +++ /dev/null @@ -1,14 +0,0 @@ -DB_USER=$postgres - -create_db_if_not_exist() { - - ( psql -l -U $DB_USER | grep $1 ) || \ - echo " CREATE DATABASE $1 WITH ENCODING='UTF8' TEMPLATE=template0; " | psql -U $DB_USER -} - -create_db_if_not_exist jira -create_db_if_not_exist stash -create_db_if_not_exist fisheye -create_db_if_not_exist bamboo - - diff --git a/atl-postgres/build.sh b/atl-postgres/build.sh old mode 100644 new mode 100755 diff --git a/atl-postgres/dbinit.sh b/atl-postgres/dbinit.sh new file mode 100755 index 0000000..593e1f3 --- /dev/null +++ b/atl-postgres/dbinit.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +echo "******CREATING APPLICATIONS DATABASES ******" + +gosu postgres postgres --single <<- EOSQL + +CREATE DATABASE jira WITH ENCODING='UTF8' TEMPLATE=template0; +CREATE DATABASE stash WITH ENCODING='UTF8' TEMPLATE=template0; +CREATE DATABASE fisheye WITH ENCODING='UTF8' TEMPLATE=template0; +CREATE DATABASE bamboo WITH ENCODING='UTF8' TEMPLATE=template0; + +EOSQL + +echo "******APPLICATION DATABASES CREATED******" + diff --git a/run.sh b/run.sh index 3a08f72..2d8219f 100755 --- a/run.sh +++ b/run.sh @@ -36,7 +36,7 @@ echo " is_container_running() { - if [ "`docker inspect -f '{{ .State.Running }}' $1 `" = "true" ]; then + if [ "`docker inspect -f '{{ .State.Running }}' $1 2> /dev/null `" = "true" ]; then return 1 else return 0 @@ -46,8 +46,8 @@ is_container_running() { stop_and_rm_container() { - is_container_running $1 && docker stop $1 - (docker ps -a | grep $1) || docker rm $1 + is_container_running $1 || docker stop $1 + (docker ps -a | grep $1 > /dev/null) && docker rm $1 } @@ -59,8 +59,9 @@ start_app() { RUN_MODE="-d" fi - # start data container if it does not exist - (docker ps -a | grep atldata) && start_data + stop_and_rm_container $1 + + start_data docker run $RUN_MODE --name $1 --link postgres:db \ --volumes-from="atldata" -e BASE_URL=$BASE_URL \ @@ -71,17 +72,20 @@ start_app() { start_db() { stop_and_rm_container postgres + + start_data + docker run -d --name postgres -e POSTGRES_PASSWORD=password \ --volumes-from="atldata" ${DOCKER_HUB_USER}/atl-postgres } start_data() { - (docker ps -a | grep atldata) && \ + (docker ps -a | grep atldata > /dev/null) || \ docker run --name atldata \ -v /opt/atlassian-home \ -v /var/lib/postgresql/data \ - centos:7 echo "data container atldata started" + centos:7 echo atldata container } @@ -143,6 +147,7 @@ case "$ACTION" in stop_and_rm_container $1 start_app $1 $2 $3 $4 test -z "$2" && start_web + ;; web)