søndag den 18. februar 2018

Running Docker on Homebrew

Docker has been growing in popularity among developers and sysadmins for the last few years, but it wasn’t until recently I decided to look into it for some of the projects I run in my sparetime, and try it out for myself.

What is Docker?

Docker is an open-source engine that automates the deployment of any application as a lightweight, portable, self-sufficient container that will run virtually anywhere.

Docker ships applications using containers, containing any requirements for running the application, ensuring that the application runs in the same manner in production as in development. The use of containers also makes the systems isolated, enabling developers to run applications and their requirements inside a virtual machine, instead of having to install requirements on the workstation.

This means that getting a development environment up and running Rails, Wordpress etc., is now a matter of getting the right image from the Docker repository, and Docker will set up all requirements (application, database, webserver etc.)

There is one catch though, on Mac: It uses technology that doesnâ€™t work natively on Mac. However in recent versions, it’s become pretty easy to handle, as Docker has a lightweight Linux VM, which allows it to run on Mac.

Getting started

We’ll start off by installing Virtualbox, as it’s needed to run the Linux VM. I usually use Homebrew Cask for installing apps, so this is based on you having Homebrew and Homebrew Cask installed on your Mac. Cask is an addon for Homebrew for installing Mac binary packages via the command line. This allows you to install applications like Chrome, OmniFocus, Alfred etc. through command line, rather than having to locate them and download the apps. manually.

boot2docker installs the VM that Docker needs, in order to run on the Mac. The following will download and install the VM, and setup the daemon that Docker needs to run:

boot2docker download
boot2docker init
boot2docker up

Note: If boot2docker tells you to set DOCKER_HOST manually in order to connect to Docker, you may need to run the command given from the command line (It should look like this export DOCKER_HOST=tcp://localhost:4243).

The docker command line tool should now be ready to talk to the daemon, and you should be able to start up a Docker instance.

I had some issues with port forwarding, when I launched my first Docker instance on my laptop - as I needed to explicitly forward the ports needed for access through VirtualBox. This is a known issue on Mac, which a lot of developers experience.