Skip to content

Instantly share code, notes, and snippets.

@dtjm
Last active April 18, 2016 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtjm/d267c3e7766f8a4a39ff61ecc40605c6 to your computer and use it in GitHub Desktop.
Save dtjm/d267c3e7766f8a4a39ff61ecc40605c6 to your computer and use it in GitHub Desktop.
Helper Bash functions for Docker
# Log into a Docker Compose service container, e.g.
#
# dclogin go_ismtpd
# dclogin go_i # also works
#
function dclogin
{
container=$(docker-compose ps | grep _$1 | head -1 | awk '{print $1}')
if [ -z "$container" ]; then
echo "no container found matching $1" > /dev/stderr
return
fi
docker exec -it $container bash
}
# Get the port for the given docker-compose service and port, e.g.
#
# dcport go_ismtpd 50350
#
function dcport
{
active=$(docker-machine active)
activeip=$(docker-machine ip $active)
docker-compose port $1 $2 | sed -e "s/0.0.0.0/$activeip/"
}
# Loads the Docker Machine environment for the active machine
function dmenv {
active=$(docker-machine active)
if [ -n "$active" ]; then
echo "Activating docker machine '$active'"
eval "$(docker-machine env $active)"
else
echo "No active docker machine running. You should start one."
fi
}
# Automatically load the Docker Machine environment if there is a Docker-related file in the local directory
if [[ -e "$PWD/docker-compose.yml" || -e "$PWD/Dockerfile" ]]; then dmenv; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment