Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Last active February 11, 2023 04:35
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save PurpleBooth/635a35ed0cffb074014e to your computer and use it in GitHub Desktop.
Save PurpleBooth/635a35ed0cffb074014e to your computer and use it in GitHub Desktop.
How to use docker-machine on OS X

How to use Docker on OS X

  1. Prerequisites
  2. Running Code
    1. Boring Theory Bit
    2. Getting a VM with docker in running
    3. Making your current terminal window know about docker
    4. Running stuff (aka using Docker Compose)
      1. Checking stuff out
      2. Updating Code
      3. Running it
  3. Oh shit everything broke how do I undo it

Prerequisites

Install

Running things

Boring theory bit with no code to copy and paste

We run our code on docker. Docker is a system that allows you to create standardised distributable units called containers. These containers work using a feature of the Linux kernel called cgroups.

In order to use these on Docker containers on OS X we need to start a VM. Thankfully the docker toolbox comes with a handy commandline to make this easier. This tool is called docker-machine and the following commands will help you interact with it.

Getting a VM with docker in running

Creating a VM for the first time

This command will create a new VM in vitualbox called default

docker-machine create -d virtualbox default

It might error if you have already created the VM, you can check if you have by running docker-machine ls and seeing if default appears in that list.

Running an already existing VM again

If the VM already exists, and you just need to get it running type

docker-machine start default

Making your current terminal window know about docker

Environment variables are a standard way of configuring software. You can see what environment variables a running in the current shell by typing export.

In order to use docker and other tools around docker we need to tell docker about the VM that is running a docker server you have running, via environment variables. Fortunately docker machine has a command build in to make this easier.

eval $(docker-machine env default)

It's probably worth breaking down what this command is doing. Firstly $(something) will run something on the commandline and you can use it's output in another command. Secondly eval will take anything that's in a parameter to it, and run it in the shell like you had typed it.

In docker-machine env default this is code to set some environment variables. You can run docker-machine env default without the $() notation to see what this looks like.

Running stuff (aka using Docker Compose)

Checking stuff out for the first time

To check stuff out make sure git has your SSH keys, if these commands fail that might be why.

To checkout code run git clone $URL, where $URL is the SSH clone url for the repository. These look like this git@github.com:PurpleBooth/stupid-twitter-bot.git or ssh://git@github.com:PurpleBooth/stupid-twitter-bot.git

So the full command would look like this

git clone git@github.com:PurpleBooth/stupid-twitter-bot.git

Updating Code

Move to the directory you checked out the code and type

git pull

Now rebuild the container so docker knows about the updated code.

docker-compose build

Running it

docker-compose up

It'll tell you what port it's listening on. To find the IP of the server it's running on type

docker-machine ip default

Oh shit everything broke how do I undo it

docker version

Run this

docker-machine rm default && docker-machine create -d virtualbox default && eval $(docker-machine env default)

This will

  1. Delete the virtual machine for docker
  2. Create a new virtual machine
  3. and setup your shell again

git version

The following commands will reset any local changes and make your copy look like the one on the server

git checkout master 
git fetch
git reset --hard origin/master
  1. Checks out the "master" branch.
  2. Download changes from the server
  3. Make your master branch look like the remote master server.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment