Skip to content

Instantly share code, notes, and snippets.

@akatrevorjay
Last active November 5, 2015 23:56
Show Gist options
  • Save akatrevorjay/b697c297f987f7816adf to your computer and use it in GitHub Desktop.
Save akatrevorjay/b697c297f987f7816adf to your computer and use it in GitHub Desktop.
Wrapper for docker and docker-compose commands to use a specific env if one is not in env
#!/bin/bash -eo pipefail
# ______________________
# docker-machine-wrapper: Wraps a command with your docker-machine dev environment
#
# * Put a directory in the beginning of $PATH somewhere in your shell init,
# ala ~/.bashrc, with a line such as the following at the end of the file:
#
# export PATH="$HOME/.bin:$PATH"
#
# Then save this file as "$HOME/.bin/docker". You can symlink it to the same
# basename as the anything you want to give a default machine env to, say:
#
# cd ~/.bin; ln -s docker docker-compose
#
# Change this to what you want your default docker machine name to be
: ${MACHINE_NAME:="dev"}
# If we need to eval docker-machine env, do so
if [[ -z "$DOCKER_MACHINE_NAME" ]]; then
eval $(docker-machine env "$MACHINE_NAME")
fi
# Find wrapped executable in PATH
self=$(basename "$0")
abs_self=$(realpath -es "$0")
# Get our target
for path in $(type -pa "$self"); do
# Verify absolute paths don't match ourselves (fruity loops detection)
abs_path=$(realpath -es "$path")
[[ "$abs_path" != "$abs_self" ]] || continue
# We have a winner
target="$path"
break
done
# Assert target
[[ -n "$target" ]] \
|| (echo "Could not find wrapped target \"$self\" in PATH." >&2; exit 1)
exec "$target" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment