Automatically import boot2docker environment variables

boot2docker is a lightweight Linux distribution specifically made to run Docker containers. It's also the fastest and easiest way to run Docker on OSX. Similarly to Vagrant it uses VirtualBox under the hood and has a very similar CLI commands.

One annoying part of boot2docker is the fact that Docker server environment variables (DOCKER_HOST, DOCKER_TLS_VERIFY and DOCKER_CERT_PATH) have to be set in order to use Docker CLI locally. So one way is to manually run init script:

$ boot2docker shellinit
Writing /Users/sosedoff/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/sosedoff/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/sosedoff/.boot2docker/certs/boot2docker-vm/key.pem
    export DOCKER_CERT_PATH=/Users/sosedoff/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1
    export DOCKER_HOST=tcp://

To automate variables import we just need to add the following line into .bash_profile:

$(boot2docker shellinit)

That will do the job, but it prints extra stuff in the console. Here's the cleaner solution that will first check if boot2docker is installed and running, and only then import envionment variables.

if [ $(hash boot2docker 2>/dev/null ; echo $?) -eq 0 ]
  if [ $(ps aux | grep [b]oot2docker-vm -c) -eq 1 ]
    echo "Boot2Docker is running. Loading shell init scripts automatically."
    $(boot2docker shellinit 2>/dev/null)