61

I am trying to automate setting up a developer environment on Mac. Part of that setup is installing the Docker Toolbox. I cannot find any documentation on how do to this via command line. How can I automate this installation (must be triggered via command line)?


Update: As pointed out in a commend by Dennis

Docker for Mac now exists, which is an alternative to Docker Toolbox. You can get it via the homebrew cask: brew cask install docker; open /Applications/Docker.app

samthebest
  • 30,803
  • 25
  • 102
  • 142
Rylander
  • 19,449
  • 25
  • 93
  • 144
  • 5
    Note that [Docker for Mac](https://docs.docker.com/docker-for-mac/) now exists, which is an alternative to Docker Toolbox. You can get it via the [homebrew cask](https://github.com/caskroom/homebrew-cask/blob/master/Casks/docker.rb): `brew cask install docker` – Dennis Jan 30 '17 at 22:55
  • After I run `brew cask install docker`, then I run `docker` and get `-bash: docker: command not found` – samthebest Feb 05 '20 at 16:11
  • 1
    Figured it out, you need `open /Applications/Docker.app` – samthebest Feb 05 '20 at 16:19

3 Answers3

95

Deprecation Warning

Docker Toolbox and Docker Machine have both been deprecated. Docker Desktop is the officially recommended replacement.

Original Answer

I found that Docker Toolbox is available via brew/cask

# Install Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install Cask
brew install caskroom/cask/brew-cask
# Install docker toolbox
brew cask install docker-toolbox

Once Docker Toolbox is installed you can setup your docker environment using the Get started with Docker Machine guide.


# create the docker machine
docker-machine create --driver "virtualbox" myBoxName

# start the docker machine
docker-machine start myBoxName

# this command allows the docker commands to be used in the terminal
eval "$(docker-machine env myBoxName)"

# at this point can run any "docker" or "docker-compose" commands you want
docker-compose up

At the end of this process, add eval "$(docker-machine env myBoxName)" to your .bash_profile or you will get the following error when you open a new shell or terminal.

"Cannot connect to the Docker daemon. Is the docker daemon running on this host?"

If you start the docker container after opening the terminal, you can either run eval "$(docker-machine env myBoxName)" manually, or reload your bash profile (source ~/.bash_profile).

Rylander
  • 19,449
  • 25
  • 93
  • 144
  • Wouldn't you still need to go through [the wizard-style setup](https://docs.docker.com/mac/step_one/#step-2-install-docker-toolbox) after installing the cask? – Dennis Jan 06 '16 at 16:35
  • @Dennis, No, you do not need to use the "wizard style setup". After installing you can use the command line to setup and configure anything docker related. – Rylander Jan 06 '16 at 17:41
  • 6
    Could you update your answer to complete the CLI setup instructions for Docker Toolbox? I'm guessing the missing steps are creating a default VM, starting it, and configuring the shell environment. In that case, we could also `brew install docker docker-machine docker-compose` instead of tapping the toolbox cask, no? – Dennis Jan 06 '16 at 18:55
  • @Dennis, you are correct that the missing steps involve creating and setting up a default VM. I added a link to guide for doing this. – Rylander Jan 15 '16 at 00:42
  • 1
    @Dennis, docker-machine and docker-compose were not available as standalone packages the last time I looked. If they have been added, then that may indeed work. – Rylander Jan 15 '16 at 00:47
  • 3
    As an alternative to the last part of the guide, you can run `open /Applications/Docker/Docker\ Quickstart\ Terminal.app` which will create the default docker machine in virtualbox just like in the wizard-style setup. – jcugat Apr 05 '16 at 11:45
  • The package is named "docker-toolbox" for me. – Heinrich Hartmann Aug 21 '16 at 11:15
  • What is myBoxName? Is that what you have to get by echoing your machine name? – codyc4321 Sep 26 '16 at 15:03
  • 1C02S21TWG8WMMBP:~ cchilders$ eval "$(docker-machine env myBoxName)" Host does not exist: "myBoxName"1 – codyc4321 Sep 26 '16 at 15:04
  • @codyc4321 is the name of the docker machine that you created. – Rylander Sep 26 '16 at 16:59
  • It's worth mentioning you may need docker-machine-nfs https://github.com/adlogix/docker-machine-nfs – Edward Nov 19 '17 at 13:56
  • Docker Toolbox and Docker Machine have been deprecated. – chris Nov 22 '21 at 22:02
  • 1
    @chris Good call out, I added a deprecation warning to the answer. – Rylander Nov 28 '21 at 01:00
9

Docker Toolbox is a good option but currently it seems like Docker for Mac/Windows is becoming better and Docker is investing a lot of time polishing the app. I recommend installing Docker mainly for 2 reasons:

  1. It doesn't interfere with Docker-Toolbox
  2. It runs on HyperKit and therefor runs on you own localhost rather than a Docker Machine IP.

The installation is quite simple:

brew cask install docker

To install docker-toolbox you can refer the above post

Saurabh
  • 71,488
  • 40
  • 181
  • 244
Alexander Luna
  • 5,261
  • 4
  • 30
  • 36
  • Also, on a Mac, you get the System toolbar icon (which you don't get installing Toolbox by itself). So, I install both. – djangofan Feb 19 '18 at 21:07
6

Homebrew Updates

I can clear up a few things:

brew cask commands were deprecated on 2020-12-01 with the release of Homebrew 2.6.0. Starting then, all brew cask commands succeeded but displayed a warning informing users that the command would soon be disabled. The message also provides the appropriate replacement.
brew cask commands were disabled on 2020-12-21 with the release of Homebrew 2.7.0. Starting then, all brew cask commands failed and displayed a warning informing users that the command is disabled. The message also provides the appropriate replacement.

With the release of Homebrew 2.8.0 (release date TBD), this disable message will be removed.

 The alternative to brew cask <command> is to use brew <command>. In many cases, you can add the --cask flag to specify casks only. For example, brew cask install atom is now brew install atom or brew install --cask atom. There are some casks that share a name with formulae (e.g. wireshark) so adding --cask ensures that the cask is installed not the formula.

I'm not much of an ansible user but I'm happy to try and help out if needed. Feel free to point me in the right direction if anything is blocked and could use feedback from the Homebrew side of things.

Now you can run like

brew install --cask docker 
Ali Hassan Mirza
  • 552
  • 11
  • 23