From 4b34097657c934c3de69e5a7c096e85110c1450b Mon Sep 17 00:00:00 2001 From: Mayel de Borniol Date: Wed, 9 Mar 2022 15:36:58 +1300 Subject: [PATCH] entrypoint for env vars --- compose.yml | 16 ++++++++++++++++ entrypoint.sh.tmpl | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 entrypoint.sh.tmpl diff --git a/compose.yml b/compose.yml index b52ee3f..7c3602f 100644 --- a/compose.yml +++ b/compose.yml @@ -30,6 +30,11 @@ services: - internal ports: - "4000:4000" # make sure this is commented in production + entrypoint: "/docker-entrypoint.sh" + configs: + - source: app_entrypoint + target: /docker-entrypoint.sh + mode: 0555 deploy: restart_policy: condition: on-failure @@ -74,6 +79,11 @@ services: - "search-data:/data.ms" networks: - internal + entrypoint: ["/tini", "--", "/docker-entrypoint.sh"] + configs: + - source: app_entrypoint + target: /docker-entrypoint.sh + mode: 0555 volumes: db-data: @@ -85,6 +95,12 @@ networks: external: true internal: +configs: + app_entrypoint: + name: ${STACK_NAME}_app_entrypoint_${APP_ENTRYPOINT_VERSION} + file: entrypoint.sh.tmpl + template_driver: golang + secrets: postgres_password: external: true diff --git a/entrypoint.sh.tmpl b/entrypoint.sh.tmpl new file mode 100644 index 0000000..01fd22b --- /dev/null +++ b/entrypoint.sh.tmpl @@ -0,0 +1,27 @@ +#!/bin/bash + +set -e + +file_env() { + local var="$1" + local fileVar="${var}_FILE" + local def="${2:-}" + + if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then + echo >&2 "error: both $var and $fileVar are set (but are exclusive)" + exit 1 + fi + + local val="$def" + + if [ "${!var:-}" ]; then + val="${!var}" + elif [ "${!fileVar:-}" ]; then + val="$(< "${!fileVar}")" + fi + + export "$var"="$val" + unset "$fileVar" +} + +file_env "MEILI_MASTER_KEY"