My Notes: Docker on Mac

I have been playing around with Docker recently and thought I would share some of my findings. Read on to learn more about Docker on Mac!

docker

Installation

Of course, in order to use Docker you need to install it. The first step would be to read the installation material specific to Mac available here. Next, head over to the Docker Toolbox page to download Docker for Mac. Finally, install the DMG.

Basics

The installation material above explains how to set up and use Docker, but I do want to cover a few items. First, on Mac you run containers through a docker-machine, which is a VM based on Oracle VM VirtualBox or really any VM platform (e.g. VMware Fusion). This means you start by creating a machine using

Assuming you have already deployed one or more machines, you can list them by running:

Of course, in order to use a machine you need to start it by running:

In order to control the container(s) running you need to know the environment information. This can be gathered by running:

Control of the container is done through the docker command. The docker command needs the environment information in order to function so you need to run something like:

Now you can switch to the docker command to control the containers instead of the machines. The most basic docker command would be:

This give you a list of containers based on the environment parameters. If you did not run the eval command above then the docker command will not work.

Just like with machines, you can start containers. For example:

Intermediate

With a basic understanding of how docker-machine and docker work, I do want to dig a little deeper. First, I want to cover the run command a bit more. While you can just use the run command, in many cases some additional flags are warranted. In the case of docker-machine containers, the –net flag is important because it will allow you to connect to the container. A common way to run the flag is:

The other flag to be aware of is -d which allows the container to basically run in the background. If you do not pass this flag then you current terminal will directly connect to the container you are running. If you then exit from the current terminal, the container will stop. Assuming you want the container to run on its own you need the -d flag:

If you have a container running in the background you may wish to log into it to perform some operation. One way to do this is with the attach command:

The problem is if you get a shell, you will not be able to detach without closing your terminal. To overcome this you can use the following flag:

Now you can use ^C to detach.

Finally, you may be tempting to connect to your container via SSH. Remember, a container is not a machine. In order to connect to the container you need to run something like a shell. This can be achieved by running something like:

Summary

Container like Docker are popular at the moment and worth taking for a spin. For those familiar with Linux and/or virtualization, many of the concepts carry over. One way to think about containers is just as an abstraction layer. Of course, you need to properly consider management and security of this new abstraction layer especially for production workloads. Do you use containers today?

© 2015, Steve Flanders. All rights reserved.

Leave a Reply