Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Start up local Docker Machine on OSX automatically.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>{{user-path}}</string>
</dict>
<key>Label</key>
<string>com.docker.machine.default</string>
<key>ProgramArguments</key>
<array>
<string>{{docker-machine}}</string>
<string>start</string>
<string>default</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

Notice

This script is no longer required with Docker for Mac which includes an option to run Docker at startup and doesn't use docker-machine to administer the local Docker engine.

Requirements

  • Docker Machine + Docker
  • curl
  • A Virtualbox-driven Docker Machine called "default" docker-machine create --driver virtualbox default (this is the default with Docker toolkit).

Usage

The git.io URL (http://git.io/vsk46) is a shortened form of the raw url of the plist.

$ curl -sL http://git.io/vsk46 | \
    sed -e "s?{{docker-machine}}?$(which docker-machine)?" \
        -e "s?{{user-path}}?$(echo $PATH)?" \
    >~/Library/LaunchAgents/com.docker.machine.default.plist && \
    launchctl load ~/Library/LaunchAgents/com.docker.machine.default.plist

certik commented Apr 6, 2016

@andystanton, I just tried it and it works! Thanks for preparing this.

I also put the following into my ~/.bashrc:

eval $(docker-machine env default)

So now I can use the docker command from my terminal just like if I was on Linux.

Owner

andystanton commented Apr 8, 2016

I'm glad it was useful @certik!

@certik this worked for me! Thanks again @andystanton

TrevorSayre commented May 18, 2016

Why not place the following in your startup file
e.g. ~/.bashrc, ~/.zshrc, etc.

DOCKER_MACHINE="dev"
if docker-machine status $DOCKER_MACHINE | grep "Running" &> /dev/null
  then
    eval "$(docker-machine env $DOCKER_MACHINE)"
  else
    docker-machine start $DOCKER_MACHINE && eval "$(docker-machine env $DOCKER_MACHINE)"
fi

For fish place the following in ~/.config/fish/config.fish

set DOCKER_MACHINE dev
if docker-machine status $DOCKER_MACHINE | grep "Running" > /dev/null
  eval (docker-machine env $DOCKER_MACHINE)
else
  docker-machine start $DOCKER_MACHINE and eval (docker-machine env $DOCKER_MACHINE)
end
Owner

andystanton commented May 18, 2016

@TrevorSayre that starts Docker only when you begin a terminal session after logging in. Using the launchctl script starts Docker when you log in, and is agnostic to what shell you use.

@andystanton Thank you for the response. I guess that difference doesn't affect me, I only use Docker in my terminal.

ellisio commented Jun 30, 2016

For some reason this does not start my Docker Machine on boot. OS-X 10.11.5 running the latest Docker Toolbox.

Owner

andystanton commented Jul 1, 2016

Hi @ellisio, can you let me know the output of the following commands:

echo $PATH
cat ~/Library/LaunchAgents/com.docker.machine.default.plist
launchctl list | grep docker
which docker-machine
docker-machine --version
docker-machine ls
which docker
docker --version

@andystanton:

echo $PATH

/Users/kulikov-im/.yarn/bin:/usr/local/sbin:/Users/kulikov-im/.yarn/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/kulikov-im/.rvm/bin:/Users/kulikov-im/Library/Android/sdk/tools:/Users/kulikov-im/Library/Android/sdk/platform-tools:/Users/kulikov-im/.rvm/bin:/Users/kulikov-im/.rvm/bin:/Users/kulikov-im/.golang//bin

cat ~/Library/LaunchAgents/com.docker.machine.dev.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>EnvironmentVariables</key>
        <dict>
            <key>PATH</key>
            <string>/Users/kulikov-im/.yarn/bin:/usr/local/sbin:/Users/kulikov-im/.yarn/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/kulikov-im/.rvm/bin:/Users/kulikov-im/Library/Android/sdk/tools:/Users/kulikov-im/Library/Android/sdk/platform-tools:/Users/kulikov-im/.rvm/bin:/Users/kulikov-im/.rvm/bin:/Users/kulikov-im/.golang//bin</string>
        </dict>
        <key>Label</key>
        <string>com.docker.machine.default</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/docker-machine</string>
            <string>start</string>
            <string>dev</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/usr/local/var/log/dockermachine-errors.log</string>
        <key>StandardOutPath</key>
        <string>/usr/local/var/log/dockermachine-status.log</string>
    </dict>
</plist>

launchctl list | grep docker

$ launchctl list | grep docker
2805    0   com.docker.machine.default

which docker-machine:

/usr/local/bin/docker-machine

docker-machine --version:

docker-machine version 0.8.2, build e18a919

docker-machine ls:

$ docker-machine ls
NAME   ACTIVE   DRIVER   STATE     URL   SWARM   DOCKER   ERRORS
dev             xhyve    Timeout

which docker:

$ which docker
/usr/local/bin/docker

docker --version:

$ docker --version
Docker version 1.12.1, build 23cf638

StandardErrorPath + StandardOutPath:

==> /usr/local/var/log/dockermachine-errors.log <==

==> /usr/local/var/log/dockermachine-status.log <==
Starting "dev"...
(dev) Waiting for VM to come online...
(dev) Waiting on a pseudo-terminal to be ready... done
(dev) Hook up your terminal emulator to /dev/ttys003 in order to connect to your VM

==> /usr/local/var/log/dockermachine-errors.log <==
Too many retries waiting for SSH to be available.  Last error: Maximum number of retries (60) exceeded

@andystanton if I add --debug to run command, like this docker-machine --debug start dev, then i see next (looping this message):

(dev) DBG | SSH cmd err, output: exit status 255:
(dev) DBG | Error getting ssh command 'exit 0' : Something went wrong running an SSH command!
(dev) DBG | command : exit 0
(dev) DBG | err     : exit status 255
(dev) DBG | output  :
(dev) DBG |
(dev) DBG | Getting to WaitForSSH function...
(dev) DBG | Using SSH client type: external
(dev) DBG | {[-o BatchMode=yes -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -o ConnectionAttempts=3 -o ConnectTimeout=10 -o ControlMaster=no -o ControlPath=none docker@192.168.64.14 -o IdentitiesOnly=yes -i /Users/kulikov-im/.docker/machine/machines/dev/id_rsa -p 22] /usr/bin/ssh}
(dev) DBG | About to run SSH command:
(dev) DBG | exit 0

suplo commented Nov 2, 2016

In my case, I want to start the docker-machine when booting (not when user logging in), so I put com.docker.machine.dev.plist into /Library/LaunchDaemons/, add

<key>UserName</key>
<string>{{ your_username }}</string>

as running docker-machine as root will throw Host does not exist: "default"

beside that, change file's owner to root:wheel

sudo chown root:wheel /Library/LaunchDaemons/com.docker.machine.dev.plist

then everything is fine.

This worked great for me. Thanks, @andystanton!

warden commented Jun 6, 2017

Good job, thanks @andystanton!

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