How to restart docker-machine after startup?

Zulhilmi Zainudin picture Zulhilmi Zainudin · May 29, 2018 · Viewed 7.9k times · Source

I'm using MacBook Pro 2009 which doesn't support the latest Docker for Mac. So, I'm using Docker Machine approach to get the Docker up and running for my machine.

Here are the commands I use to install Docker in my machine:

$ brew install docker docker-compose docker-machine
$ docker-machine create --driver virtualbox default
$ docker-machine env
$ eval $(docker-machine env default)

There is nothing wrong with the commands above. The Docker process is up and running perfectly everytime I run the last command.

But, when I reboot the host (MacBook), the Docker Machine is dead and I see Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? error.

I tried putting 2 lines below in my .bash_profile but it didn't work (yes, I already source it):

docker-machine restart default
docker-machine env
eval $(docker-machine env default)

I also tried creating a LaunchDaemons .plist file to run a bash script on startup, but it didn't work too:

<?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>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
    </dict>
    <key>Label</key>
    <string>com.startup</string>
    <key>Program</key>
    <string>/Users/zulh/scripts/start_docker.sh</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>LaunchOnlyOnce</key>        
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/startup.stdout</string>
    <key>StandardErrorPath</key>
    <string>/tmp/startup.stderr</string>
    <key>UserName</key>
    <string>zulh</string>
    <key>GroupName</key>
    <string>staff</string>
    <key>InitGroups</key>
    <true/>
  </dict>
</plist>

The bash script:

#!/bin/bash
docker-machine restart default
docker-machine env
eval $(docker-machine env default)

The command was:

$ sudo launchctl load -w /Library/LaunchDaemons/com.startup.plist

How do I restart Docker Machine everytime I start or restart my MacBook?

Answer

7anshuai picture 7anshuai · Sep 26, 2018

You can use the brew services command to do that.

  1. Run brew info docker-machine, you should see some information about the docker-machine formula.

To have launchd start docker-machine now and restart at login:

brew services start docker-machine

Or, if you don't want/need a background service you can just run:

docker-machine start

  1. Run brew services start docker-machine, will start the service docker-machine immediately and register it to launch at login.
  2. Run brew services list to list all running services.

If the docker-machine status is started, it should restart at login.

You can add the eval $(docker-machine env default) in .bash_profile to configure your shell.