boot2docker Wrapper Script

I put together this simple wrapper function for simplifying boot2docker interactions on my Macbook.

Note: I refer to .bashrc as the target as it's the more common shell. However, this has been tested and I personally use it within zsh.

Basic Usage

$ docker reload
# purge current boot2docker environment

$ docker up
# start boot2docker and export environment

$ docker reset|restart|reup
# stop and restart boot2docker

$ docker clean
# remove all orphaned image shards and all containers that aren't running - DANGEROUS

$ docker [etc]
# all other arguements are passed directly passed through to docker

The Functions

# file: ~/.bashrc

#############################################################
# Function -- Docker/Boot2Docker
#############################################################
function docker_shellinit {
  local _shellinit="$(boot2docker shellinit)"
  eval "$(echo ${_shellinit})"
  echo "${_shellinit}" > ~/.boot2dockerrc
}

function docker_reup {
  echo "+ running vpn fix"
  docker_down

  echo "+ resetting vbox route"

  local _iface="$(VBoxManage showvminfo boot2docker-vm --machinereadable | grep hostonlyadapter | cut -d '"' -f 2)"
  echo "++ sudo route -n add -net 192.168.59.0/24 -interface ${_iface}"

  sudo route -n add -net 192.168.59.0/24 -interface ${_iface} && \
    docker_up
}

function docker_reset {
  echo "+ clearing docker variables"
  unset DOCKER_HOST
  unset DOCKER_CERT_PATH
  unset DOCKER_TLS_VERIFY
  docker_shellinit
}

function docker_up {
  echo "+ starting boot2docker"
  boot2docker up
  b2dSTATUS=$?
  docker_reset
  return $b2dSTATUS
}

function docker_down {
  echo "+ stopping boot2docker"
  boot2docker down
  return 0
}

function docker_clean {
  echo "+ clean containers"
  docker ps -a | grep 'Exited ' | awk '{ print $NF }' | xargs docker rm
  docker ps -a | grep -v 'Up ' | awk '{ print $NF }' | xargs docker rm

  echo "+ clean images"
  docker images | grep '^<none>' | awk '{ print $3 }' | xargs docker rmi
}

function b2d {
  case "$@" in
  reload)
    docker_reset
    return 0;;
  reset|fix|reup|fuck)
    docker_reup
    return $?;;
  up)
    docker_up
    return $?;;
  down)
    docker_down
    return $?;;
  clean)
    docker_clean
    return $?;;
  esac
  boot2docker $@
}

docker_exec="$(which docker)"
function docker {
  case "$@" in
  reload)
    docker_reset
    return 0;;
  reset|fix|reup|fuck)
    docker_reup
    return $?;;
  up)
    docker_up
    return $?;;
  down)
    docker_down
    return $?;;
  clean)
    docker_clean
    return $?;;
  esac
  $docker_exec $@
}

Installation

$ curl -s https://gist.githubusercontent.com/jmervine/6713d10ab05fecd6e1aa/raw/5c5f7020696e23dffa6f046816239574f42767ee/boot2dockerrc.sh >> ~/.bashrc