From 3e31f8097742d8d4d5c6a1dea43d86d17e01c83b Mon Sep 17 00:00:00 2001 From: EricsonMacedo Date: Wed, 2 Dec 2020 16:49:54 -0300 Subject: [PATCH] Setup environment variables for compose. (#7490) * Setup environment variables for compose. - Setup environment variables to work as expected for compose config and context in container mode. - Setup volume mounts based on -f, --file argument for compose config and context. Signed-off-by: Ericson Macedo * Improve parsing of specified compose file - Update parsing of multiple -f, --file parameters. - Remove usage of eval command. Signed-off-by: Ericson Macedo --- script/run/run.sh | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/script/run/run.sh b/script/run/run.sh index bacf39d88..658cf47ac 100755 --- a/script/run/run.sh +++ b/script/run/run.sh @@ -44,13 +44,34 @@ fi if [ -n "$COMPOSE_PROJECT_NAME" ]; then COMPOSE_OPTIONS="-e COMPOSE_PROJECT_NAME $COMPOSE_OPTIONS" fi -# TODO: also check --file argument if [ -n "$compose_dir" ]; then VOLUMES="$VOLUMES -v $compose_dir:$compose_dir" fi if [ -n "$HOME" ]; then VOLUMES="$VOLUMES -v $HOME:$HOME -e HOME" # Pass in HOME to share docker.config and allow ~/-relative paths to work. fi +i=$# +while [ $i -gt 0 ]; do + arg=$1 + i=$((i - 1)) + shift + + case "$arg" in + -f|--file) + value=$1 + i=$((i - 1)) + shift + set -- "$@" "$arg" "$value" + + file_dir=$(realpath "$(dirname "$value")") + VOLUMES="$VOLUMES -v $file_dir:$file_dir" + ;; + *) set -- "$@" "$arg" ;; + esac +done + +# Setup environment variables for compose config and context +ENV_OPTIONS=$(printenv | sed -E "/^PATH=.*/d; s/^/-e /g; s/=.*//g; s/\n/ /g") # Only allocate tty if we detect one if [ -t 0 ] && [ -t 1 ]; then @@ -67,4 +88,4 @@ if docker info --format '{{json .SecurityOptions}}' 2>/dev/null | grep -q 'name= fi # shellcheck disable=SC2086 -exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@" +exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $ENV_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"