Skip to content

Instantly share code, notes, and snippets.

@iansinnott
Last active March 20, 2016 12:29
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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 "$@"
}
@iansinnott
Copy link
Author

Automatically set up boot2docker on a Mac

I got annoyed manually exporting the boot2docker IP address every time I wanted to use Docker on my mac, so I wrote this short script to wrap the docker command and set the necessary environment variables if they are undefined.

If DOCKER_HOST is already defined all arguments will be automatically passed to docker.

This script also exports an extra environment variable: DOCKER_IP. This comes in handy when you want to connect to any port exposed by a dockerized app. For instance, if you are running a web app in your container that is exposed on port 8080 you can confirm that it is running by executing:

$ curl $DOCKER_IP:8080

Of course you cannot access environment variables from your browser, so to view it their you will have to copy the IP from the command line. But having the variable set still makes this simple:

$ echo $DOCKER_IP:8080 | pbcopy

Then simply past into your browser.

@MrMMorris
Copy link

you are amazing

@MrMMorris
Copy link

One suggestion, and let me know if this doesn't make sense:

You have the DOCKER_HOST port hardcoded and I noticed right away that sometimes b2d will start with 2376 instead. This means the script will result in:

FATA[0000] Cannot connect to the Docker daemon. Is 'docker -d' running on this host?

I think the better option would be to replace the exporting of $DOCKER_HOST with $(boot2docker shellinit) as this sets them properly

@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