Skip to content

Instantly share code, notes, and snippets.

@iansinnott
Last active March 20, 2016 12:29
Show Gist options
  • Save iansinnott/0a0c212260386bdbfafb to your computer and use it in GitHub Desktop.
Save iansinnott/0a0c212260386bdbfafb to your computer and use it in GitHub Desktop.
Automatically set up boot2docker on a Mac whenever `docker` is called
#!/bin/bash
# A wrapper for the docker binary. Checks to make sure the docker host is
# set before executing docker commands.
docker() {
# Start the daemon if it's not running
if [ $(boot2docker status) != 'running' ]; then
echo 'Starting the Docker daemon.'
boot2docker start
fi
if [ -z $DOCKER_HOST ] || [ -z $DOCKER_IP ]; then
# Store the docker binary path
DOCKER=$(which docker)
# All 'echo' commands are unecessary, but it lets you know
# if this block of code was run or not.
echo 'Setting Docker host...'
# Grab the ip address from boot2socker. DOCKER_IP is not
# necessary to run docker, but it comes in handy (see readme).
export DOCKER_IP=$(boot2docker ip 2>/dev/null)
export DOCKER_HOST="tcp://$DOCKER_IP:2375"
# Confirm that variables were exported via the command line.
echo " DOCKER_IP=$DOCKER_IP"
echo " DOCKER_HOST=$DOCKER_HOST"; echo
fi
# Execute docker with all arguments.
DOCKER "$@"
}
@colbywhite
Copy link

What @MrMMorris said. Here's what's in my .zshrc file now.

boot2docker up
eval `boot2docker shellinit`

Edit: You could probably put an if statement around that boot2docker up to not run it when it's already up.

@ctoa
Copy link

ctoa commented May 20, 2015

Pipe stderr to /dev/null if you don't want the "Writing ..." messages to appear.

eval $(boot2docker shellinit 2>/dev/null)

@itoche
Copy link

itoche commented Jul 5, 2015

Using this script didn't work immediately for me as I got:

malformed HTTP response "\x15\x03\x01\x00\x02\x02". Are you trying to connect to a TLS-enabled daemon without TLS?

Maybe because Docker (at least in version 1.7?) uses port 2376 by default (and not 2375)? I added the following to make things work.

export DOCKER_CERT_PATH=$HOME/.boot2docker/certs/boot2docker-vm/
export DOCKER_TLS_VERIFY=1

https://gist.github.com/itoche/3bc0ee84f2ae9fee954d

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