From 3c1c8f9e802db2303c64ff8388631d484e536331 Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Mon, 26 Oct 2015 21:05:53 -0700 Subject: [PATCH 1/6] Pass environment variables to docker container This change allows arbitrary environment variables to be passed to the underlying docker container. Useful when using postgres docker containers which allow for additional configuration on top of the official postgres image. See https://github.com/mcolyer/docker-postgres-wale for an example. --- README.md | 2 +- commands | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 30f0411..2efc1c4 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres ``` postgres:clone Create container then copy data from into postgres:connect Connect via psql to a postgres service -postgres:create Create a postgres service +postgres:create Create a postgres service with environment variables postgres:destroy Delete the service and stop its container if there are no links left postgres:export Export a dump of the postgres service database postgres:expose [port] Expose a postgres service on custom port if provided (random port otherwise) diff --git a/commands b/commands index 46dcadb..e9f6f07 100755 --- a/commands +++ b/commands @@ -31,8 +31,12 @@ case "$1" in touch "$LINKS_FILE" dokku_log_info1 "Starting container" + ENVIRONMENT="" + for arg in "${@:2}"; do + ENVIRONMENT="$ENVIRONMENT -e $arg" + done SERVICE_NAME=$(get_service_name "$SERVICE") - ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_ROOT/data:/var/lib/postgresql/data" -e "POSTGRES_PASSWORD=$password" -d --restart always --label dokku=service --label dokku.service=postgres "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION") + ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_ROOT/data:/var/lib/postgresql/data" $ENVIRONMENT -e "POSTGRES_PASSWORD=$password" -d --restart always --label dokku=service --label dokku.service=postgres "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION") echo "$ID" > "$SERVICE_ROOT/ID" dokku_log_verbose_quiet "Waiting for container to be ready" From 0a66e18ff35d1ff116ba194b024ed8dc9dcb496b Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Mon, 26 Oct 2015 21:07:35 -0700 Subject: [PATCH 2/6] Proceed on create if the database exists This change is useful if the container image allows for you to restore from a backup when starting. --- commands | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands b/commands index e9f6f07..fb0beac 100755 --- a/commands +++ b/commands @@ -44,7 +44,7 @@ case "$1" in dokku_log_verbose_quiet "Creating container database" DATABASE_NAME="$(get_database_name "$SERVICE")" - docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME" + docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME 2> /dev/null || echo 'Already exists'" dokku_log_info2 "$PLUGIN_SERVICE container created: $SERVICE" dokku "$PLUGIN_COMMAND_PREFIX:info" "$SERVICE" From a71847cc1eb0e14782b290593cbaa40bc322dabe Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Tue, 27 Oct 2015 07:09:47 -0700 Subject: [PATCH 3/6] Move double quote --- commands | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands b/commands index fb0beac..5cd51ac 100755 --- a/commands +++ b/commands @@ -44,7 +44,7 @@ case "$1" in dokku_log_verbose_quiet "Creating container database" DATABASE_NAME="$(get_database_name "$SERVICE")" - docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME 2> /dev/null || echo 'Already exists'" + docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME" 2> /dev/null || echo 'Already exists' dokku_log_info2 "$PLUGIN_SERVICE container created: $SERVICE" dokku "$PLUGIN_COMMAND_PREFIX:info" "$SERVICE" From 50f980ce03b54cc4c9af8d06b6599e8f1260cccd Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Mon, 2 Nov 2015 21:03:38 -0800 Subject: [PATCH 4/6] Switch to POSTGRES_DOCKER_ARGS variable Previously extra command line arguments were passed after the command, based on the discussion in https://github.com/dokku/dokku-postgres/pull/40, it was agreed that using a single environment variable was more clear. --- README.md | 3 ++- commands | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2efc1c4..014f503 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres ``` postgres:clone Create container then copy data from into postgres:connect Connect via psql to a postgres service -postgres:create Create a postgres service with environment variables +postgres:create Create a postgres service with environment variables postgres:destroy Delete the service and stop its container if there are no links left postgres:export Export a dump of the postgres service database postgres:expose [port] Expose a postgres service on custom port if provided (random port otherwise) @@ -52,6 +52,7 @@ dokku postgres:create lolipop # official postgres image export POSTGRES_IMAGE="postgres" export POSTGRES_IMAGE_VERSION="9.4.4" +export POSTGRES_DOCKER_ARGS="USER=alpha;HOST=beta" dokku postgres:create lolipop # get connection information as follows diff --git a/commands b/commands index 5cd51ac..230d7bd 100755 --- a/commands +++ b/commands @@ -31,12 +31,13 @@ case "$1" in touch "$LINKS_FILE" dokku_log_info1 "Starting container" - ENVIRONMENT="" - for arg in "${@:2}"; do - ENVIRONMENT="$ENVIRONMENT -e $arg" - done + if [ -n $POSTGRES_DOCKER_ARGS ]; then + echo "$(echo $POSTGRES_DOCKER_ARGS | tr ',' "\n")" > "$SERVICE_ROOT/ENVIRONMENT" + else + echo "" > "$SERVICE_ROOT/ENVIRONMENT" + fi SERVICE_NAME=$(get_service_name "$SERVICE") - ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_ROOT/data:/var/lib/postgresql/data" $ENVIRONMENT -e "POSTGRES_PASSWORD=$password" -d --restart always --label dokku=service --label dokku.service=postgres "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION") + ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_ROOT/data:/var/lib/postgresql/data" --env-file="$SERVICE_ROOT/ENVIRONMENT" -e "POSTGRES_PASSWORD=$password" -d --restart always --label dokku=service --label dokku.service=postgres "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION") echo "$ID" > "$SERVICE_ROOT/ID" dokku_log_verbose_quiet "Waiting for container to be ready" From c68ac3be25b99dc2081a4ac218b8c0722ba6ae7d Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Mon, 2 Nov 2015 21:23:58 -0800 Subject: [PATCH 5/6] Fix lint failures Also correct ; to be the proper delimiter. --- commands | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands b/commands index 230d7bd..fb0a722 100755 --- a/commands +++ b/commands @@ -31,8 +31,8 @@ case "$1" in touch "$LINKS_FILE" dokku_log_info1 "Starting container" - if [ -n $POSTGRES_DOCKER_ARGS ]; then - echo "$(echo $POSTGRES_DOCKER_ARGS | tr ',' "\n")" > "$SERVICE_ROOT/ENVIRONMENT" + if [[ -n $POSTGRES_DOCKER_ARGS ]]; then + echo "$POSTGRES_DOCKER_ARGS" | tr ';' "\n" > "$SERVICE_ROOT/ENVIRONMENT" else echo "" > "$SERVICE_ROOT/ENVIRONMENT" fi From 6c6b040f4aa4e972a261c494445d4fe99ef024d1 Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Wed, 4 Nov 2015 17:38:49 -0800 Subject: [PATCH 6/6] Rename to DOCKER_CUSTOM_ENV --- README.md | 2 +- commands | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 014f503..77cbe54 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ dokku postgres:create lolipop # official postgres image export POSTGRES_IMAGE="postgres" export POSTGRES_IMAGE_VERSION="9.4.4" -export POSTGRES_DOCKER_ARGS="USER=alpha;HOST=beta" +export POSTGRES_CUSTOM_ENV="USER=alpha;HOST=beta" dokku postgres:create lolipop # get connection information as follows diff --git a/commands b/commands index fb0a722..12816eb 100755 --- a/commands +++ b/commands @@ -31,8 +31,8 @@ case "$1" in touch "$LINKS_FILE" dokku_log_info1 "Starting container" - if [[ -n $POSTGRES_DOCKER_ARGS ]]; then - echo "$POSTGRES_DOCKER_ARGS" | tr ';' "\n" > "$SERVICE_ROOT/ENVIRONMENT" + if [[ -n $POSTGRES_CUSTOM_ENV ]]; then + echo "$POSTGRES_CUSTOM_ENV" | tr ';' "\n" > "$SERVICE_ROOT/ENVIRONMENT" else echo "" > "$SERVICE_ROOT/ENVIRONMENT" fi