diff --git a/docker-clean b/docker-clean index 06f5987..0268dd4 100755 --- a/docker-clean +++ b/docker-clean @@ -78,9 +78,21 @@ declare -a REMAINING_CONTAINERS # Currently in use declare -a EMPTY_NETWORKS # Currently in use #declare -a VOLUMES_TO_DELETE # Not in use +# @info: Docker host to use (default to empty or localhost). +declare DOCKER_HOST #FUNCTIONS +# @info: Overrides the system docker daemon so we can pass in a host +# @args: Global Arguments $@ +function docker(){ + if [ -z "$DOCKER_HOST" ]; then + command "docker" "$@" + else + command "docker" -H "$DOCKER_HOST" "$@" + fi +} + # @info: Parses and validates the CLI arguments # @args: Global Arguments $@ # TODO: handle use case where just -n or just -l flag is given @@ -101,11 +113,13 @@ function parseCli(){ esac while [[ "$#" -gt 0 ]]; do key="$1" + val="$2" case $key in stop ) STOP_CONTAINERS=true; CLEAN_CONTAINERS=true; CLEAN_IMAGES=true; CLEAN_VOLUMES=true; CLEAN_NETWORKS=true ;; images ) DELETE_TAGGED=true; CLEAN_CONTAINERS=true; CLEAN_IMAGES=true; CLEAN_VOLUMES=true; CLEAN_NETWORKS=true ;; run ) CLEAN_CONTAINERS=true; CLEAN_IMAGES=true; CLEAN_VOLUMES=true; CLEAN_NETWORKS=true ;; all ) STOP_CONTAINERS=true; DELETE_TAGGED=true; CLEAN_CONTAINERS=true; CLEAN_IMAGES=true; CLEAN_VOLUMES=true; CLEAN_NETWORKS=true ;; + -H | --host) DOCKER_HOST=$val; shift;; -s | --stop) STOP_CONTAINERS=true ;; -n | --dry-run) DRY_RUN=true ;; -l | --log) VERBOSE=true ;; @@ -155,6 +169,10 @@ function usage { echo echo "Additional Flag options:" echo + echo "-H or --host Specifies the docker host to run against" + echo " Useful for docker swarm maintenance" + echo " ie: -H 127.0.0.1:4000" + echo echo "-n or --dry-run Adding this additional flag will list items to be" echo " removed without executing any stopping or removing commands" echo @@ -549,7 +567,7 @@ function restartMachine { # Upstart covers SysV and OpenRC as well. if [[ $init_system =~ upstart ]]; then if [[ $DRY_RUN == false ]]; then - sudo service docker restart + sudo service "docker" restart else echo "Restart command that would be run: sudo service docker restart" fi @@ -561,7 +579,7 @@ function restartMachine { fi elif [[ $init_system =~ rc ]]; then if [[ $DRY_RUN == false ]]; then - sudo launchctl restart docker + sudo launchctl restart "docker" else echo "Restart command that would be run: sudo launchctl restart docker" fi