Docker Mac Brew



In this post I want to cover how you can setup a Docker development environment on Windows/Mac. As you might already know Docker requires a Linux kernel to run. Therefore we will always need a VM to run the actual Docker environment when you are on Windows or Mac OS. Hereby a few quotes from the Docker webpage to remember you what Docker is all about, or to give you a quick idea.

Apple M1 ARM based Mac as a developer: docker and brew will take a while to be working at all or supported « The Wiert Corner – irregular stream of stuff « Using both Google Fi and Google Voice means you need to use two Google accounts on the same phone Archiving Google Product Forums URLs ». Homebrew is a package manager for the Mac. You’ll also need Homebrew Cask, which you can install after Homebrew by running brew tap caskroom/cask in your Terminal. Install Docker for Mac. Docker is used to create, manage, and run our containers. Homebrew Docker Virtualbox (But not only VirtualBox) This formula resolves the Docker issue on AMD based MacOS (Ryzentosh). Also can be used on any Mac. It’s an app provided by Docker Company for Mac OS and Windows. The special feature in this toolbox is the requirement for VirtualBox, which will be a supervisor to run Linux. So, Docker is available as another machine in Docker environment.

Docker provides a common framework for developer and IT teams to collaborate on applications. With a clear separation of concerns and robust tooling, organizations are able to innovate faster with greater control.

AGILITY
Developers have the freedom to define environments, and the ability to create and deploy apps faster and easier. IT ops have the flexibility to quickly respond to change.

CONTROL
Developers own all the code from infrastructure to app. IT ops have the manageability to standardize, secure, and scale the operating environment.

Docker Mac Brew

PORTABILITY
Docker gives you choice without complexity, from a laptop to a team, to private infrastructure and public cloud providers.

Ok enough chatting, for the impatient ones continue to TL;DR. For all the others lets continue reading.

Put the whale to the water

Lets have a look how we can get an environment running on Windows or Mac. In this tutorial I’m going to use Chocolatey. Chocolatey is a package manager for Windows. It allows you to install the software you require from PowerShell. This makes it really powerful to script all the software you require on your computer by putting it in a PowerShell script. When unfamiliar with Chocolatey I recommend you to have a look at the Chocolatey webpage and install the tool as described on their page.

For the readers using a Mac, I will mention when you have to slightly differ your approach. In all the other cases you can run the same commands. For Mac I will be using a similar tool as Chocolatey, called Homebrew. When unfamiliar with this tool I assume you know how to install the packages manually or just have a look at Homebrew to figure out how to set it up on your Mac.

From this point I assume you have Chocolatey/Homebrew installed. In case you don’t I’ll also assume that you know how to install the required tools manually by downloading the correct installers. In order to get a VM running for our Docker environment we are going to use docker-machine to setup a docker-machine on VirtualBox. So lets start by installing Virtualbox, docker-machine and Docker using Chocolatey. In case you have any of these tools already installed you can skip this step.

Install tools

docker-machine

Now we have installed docker-machine we are able to create a new environment. In case you already had docker-machine installed we can first have a look on what machines we have available using the following command.

In my case I already have the machine created. In case you also have a machine there already you might want to just use that one. For the ones started from scratch I will show you now how to create a machine.

Note: In case you just created your machine from scratch, we first have to boot it. You can do so by running following command. In case your machine is already running you can skip this step.

With the command shown previously we created a machine named default using the virtualbox driver. So when you run the docker-machine ls command now, you should have similar output as you have seen before. You will also be able to see if the machine is running. We also see in the last line of the creation output that there is a special command which will provide you the information in order to connect your Docker client to this specific machine (docker-machine env default). Lets configure the PowerShell environment to connect Docker to this specific machine.

Remember when you are running this command from Mac it will output it differently. The same goes when you are using Bash/Cygwin on Windows or when you are using the regular Command-Prompt on Windows. So just run the command in your preferred console and execute the command as shown in the output. In our case I’m going to continue using PowerShell, which means I will execute the command as we have just seen.

docker

Lets configure our PowerShell.

For Mac users the command will be something like following on bash.

Now we will be able to use Docker on Windows. Download fast web browser for mac. Lets try using the docker/whalesay image to test our Docker environment.

In this article I won’t go much deeper in the usage of Docker itself. So things like creating your own images, multi-container solutions and other more in depth Docker stuff won’t be covered as it is out of scope. Therefore I only want to point you to docker --help on your console and the Docker documentation for now, so I won’t leave you completely in the dark. I might be publishing another post on how to run your project in Docker later.

Play time

The docker/whalesay image also has command line help which reveals some more options (docker run docker/whalesay cowsay -h). It reveals for example the whale can also think instead of talk (docker run docker/whalesay cowthink 'Will he see the difference?'). Have fun playing around with the docker/whalesay image and the other options revealed in the help or simply try out some other images from the Docker hub.

Wrapup

Create Docker Machine

If you finished playing around with Docker you might want to shutdown the VM using docker-machine stop default. Next time you want to use it again simply run docker-machine start default and docker-machine env default to start and configure your environment. Last but not least I would like to point you to the --help option. It will provide you with usage information on the command line options for every single command.

Docker Mac Brew Instructions

TL;DR

As a bonus I want to give you all the commands we require to setup our environment in a PowerShell script. So feel free to save the contents in a ps1 file which you can call from your PowerShell.

Windows

Mac

Finally, when you are finished playing arround with Docker, you might want to shut down your vm. You can do so by running.

In case you where one of the impatient ones, now realizing you have no clue what just happened when you called the commands in above example. Here you go for a second shot.

References:

Again thanks for reading this article, and as always share your feedback and questions in the comments.

Question or issue on macOS:

I am running docker and I want bash command completion for docker commands and parameters.

How to solve this problem?

Solution no. 1:

If you have already homebrewbash-completioninstalled just install the docker completion script into the bash_completion.d

Note: If you do not have homebrew bash-completion installed, follow these instructions to install it before you execute the line above.

Note: the completion depends on some functions defined in debian bash-completion. Therefore, just sourcing the docker completion script as described in completion/bash/docker may not work. If you try to complete docker run (by hitting TAB) you may get an error like __ltrim_colon_completions: command not found. This could mean that you have not installed the bash-completion scripts.

Solution no. 2:

The official Docker for Mac page has a section on installing bash completion:

If you have Homebrew bash completion installed:

Solution no. 3:

The completion scripts come with Docker Beta. I want them to stay up to date. So, on OS X…

  • Install homebrew’s bash-completion
  • Symlink the files

Solution no. 4:

Mac Brew Start Docker

The stumbling point for me was that once you brew install bash-competion, you have to add few lines in your .bash_profile to load it once you launch Terminal:

Source:
http://davidalger.com/development/bash-completion-on-os-x-with-brew/

Because I haven’t found anywhere a step by step documentation, I’ve made a quick script to install homebrew, bash-completion and eventually the completion scripts for docker.
https://github.com/HypnoTheNomad/docker-bash-completion-macos-brew

Solution no. 5:

The auto completion of docker needed not only for mac, its also needed for ubuntu / bash terminals.

In Ubuntu

Completion will be available upon next login.

Since its top result in google I added answer here.

Solution no. 6:

The official Docker site has a section for Command-line completion, and for Mac to:

Helped for me with Homebrew:

After the installation, Brew displays the installation path. From documentation is correct for me:

/usr/local/etc/bash_completion.d/

Run the script:

Open and add following in the end of your file ~/.bash_profile:

Install Docker On Mac Brew

Restart terminal. It’s all.

Solution no. 7:

Just to be said:
if you use brew:
brew install docker
will do all what you need. It includes brew link docker which installs the completion into `brew --prefix`/etc/bash_completion.d/docker
same topic, same answer for docker-machine, docker-compose, etc. …

else (perhaps you are using Docker Beta (new “more native” docker installation package without Virtualbox) you still have to add it manually, then follow Michael’s answer plus have a look at the additional completion scripts for docker-machine, docker-compose and some shell-helper that are handled in the ‘script’ from CodeCorrupt

Solution no. 8:

Guide to setup autocomplete for ZSH on Mac OSX

Follow these steps if you are using oh-my-zsh and autocomplete is not working:

Step 1:

Make the following three links:

ln -s /Applications/Docker.app/Contents/Resources/etc/docker.zsh-completion /usr/local/share/zsh/site-functions/_docker

Docker Mac Brew Recipe

ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.zsh-completion /usr/local/share/zsh/site-functions/_docker-compose

Step 2:

Either add autoload -Uz compinit; compinit to .zshrc

or run echo 'autoload -Uz compinit; compinit' >> .zshrc in your shell

Hope this helps!