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 <macedoericson@gmail.com>

* Improve parsing of specified compose file

- Update parsing of multiple -f, --file parameters.
- Remove usage of eval command.

Signed-off-by: Ericson Macedo <macedoericson@gmail.com>
This commit is contained in:
EricsonMacedo 2020-12-02 16:49:54 -03:00 committed by GitHub
parent 059fd29ec3
commit 3e31f80977
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 2 deletions

View File

@ -44,13 +44,34 @@ fi
if [ -n "$COMPOSE_PROJECT_NAME" ]; then if [ -n "$COMPOSE_PROJECT_NAME" ]; then
COMPOSE_OPTIONS="-e COMPOSE_PROJECT_NAME $COMPOSE_OPTIONS" COMPOSE_OPTIONS="-e COMPOSE_PROJECT_NAME $COMPOSE_OPTIONS"
fi fi
# TODO: also check --file argument
if [ -n "$compose_dir" ]; then if [ -n "$compose_dir" ]; then
VOLUMES="$VOLUMES -v $compose_dir:$compose_dir" VOLUMES="$VOLUMES -v $compose_dir:$compose_dir"
fi fi
if [ -n "$HOME" ]; then if [ -n "$HOME" ]; then
VOLUMES="$VOLUMES -v $HOME:$HOME -e HOME" # Pass in HOME to share docker.config and allow ~/-relative paths to work. VOLUMES="$VOLUMES -v $HOME:$HOME -e HOME" # Pass in HOME to share docker.config and allow ~/-relative paths to work.
fi 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 # Only allocate tty if we detect one
if [ -t 0 ] && [ -t 1 ]; then if [ -t 0 ] && [ -t 1 ]; then
@ -67,4 +88,4 @@ if docker info --format '{{json .SecurityOptions}}' 2>/dev/null | grep -q 'name=
fi fi
# shellcheck disable=SC2086 # 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 "$@"