Last active
March 20, 2016 12:29
-
-
Save iansinnott/0a0c212260386bdbfafb to your computer and use it in GitHub Desktop.
Automatically set up boot2docker on a Mac whenever `docker` is called
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "$@" | |
} |
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.
Pipe stderr to /dev/null if you don't want the "Writing ..." messages to appear.
eval $(boot2docker shellinit 2>/dev/null)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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