Skip to content

Instantly share code, notes, and snippets.

@joshmanders
Last active November 9, 2020 13:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmanders/4dcad14cf6d4ff318d86 to your computer and use it in GitHub Desktop.
Save joshmanders/4dcad14cf6d4ff318d86 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@RRAlex
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
Copy link
Author

Good suggestion @RRAlex. Updated. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment