Docker-Machine Lazy Loading
31/Aug 2015
Like many, I use docker-machine
(installed via
Docker Toolbox) to develop Docker
images on OSX. I am terrible about remembering to launch the docker-machine
VM
ahead of time, and I don’t like having STDERR output or delays from launching
new shell tabs, so I wrote a quick lazy-load function inspired by
Spacemacs.
I use ZSH but this may work on Bash as well, and my VM is named dev
. This will
intercept the first command that begins with docker
during a given shell
session, and ensure that the VM has been booted and that the shell variables
DOCKER_HOST
and DOCKER_CERT_PATH
are set appropriately. It won’t try to boot
the machine or update the shell environment until the first time a docker
command is attempted.
docker() {
unset -f docker
# docker-machine create -d virtualbox dev
# VBoxManage list runningvms | grep -E "^\"dev\"" >/dev/null 2>/dev/null || docker-machine start dev
# docker-machine create -d vmwarefusion dev
vmrun list | grep -E ".docker\/machine\/machines\/dev\/dev.vmx$" 2>&1 >/dev/null || docker-machine start dev
eval "$(docker-machine env dev)"
docker "$@"
}
More Reading