Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My Docker Command

My Docker Command

When working with docker toolbox, sometimes I type get tripped up trying to type docker-machine env default and accidentally type docker machine env default instead. This bash script is a helper to allow you to do just that.

Put it in your $PATH and chmod +x my-docker. Then create an alias in your profile with alias docker="my-docker"

Now when you type docker machine env this will automatically proxy anything after machine to docker-machine.

Same goes for docker compose up proxies to docker-compose up.

#!/usr/bin/env bash
machine () {
shift
local args=("${@:1}")
docker-machine "${args[@]}"
}
compose () {
shift
local args=("${@:1}")
docker-compose "${args[@]}"
}
cleanup () {
case "$2" in
images)
docker rmi $(docker images -f -q "dangling=true")
;;
containers)
docker rm -f $(docker ps -aq -f "status=exited")
;;
*)
echo "Please provide an extra argument, either \`images\` or \`containers\`."
;;
esac
}
main () {
local args=("$@")
case "$1" in
machine) machine "${args[@]}" ;;
compose) compose "${args[@]}" ;;
cleanup) cleanup "${args[@]}" ;;
*) docker "${args[@]}" ;;
esac
}
main "$@"
@joshmanders

This comment has been minimized.

Copy link
Owner Author

joshmanders commented Feb 26, 2016

Added support for cleaning up containers and images, by just issuing docker cleanup images or docker cleanup containers.

@RRAlex

This comment has been minimized.

Copy link

RRAlex commented Feb 26, 2016

To be safe (and shorten the code), you should consider using something like this on line 25:
docker ps -aq -f "status=exited"

It doesn't depend on the possibly changing docker output and will also not catch container named Exited (strange but possible! :)

@joshmanders

This comment has been minimized.

Copy link
Owner Author

joshmanders commented Feb 27, 2016

Good suggestion @RRAlex. Updated. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.