-
-
Save iansinnott/0a0c212260386bdbfafb to your computer and use it in GitHub Desktop.
#!/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 "$@" | |
} |
you are amazing
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
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
Automatically set up
boot2docker
on a MacI 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 thedocker
command and set the necessary environment variables if they are undefined.If
DOCKER_HOST
is already defined all arguments will be automatically passed todocker
.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: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:
Then simply past into your browser.