Chapter 1.10 - Docker Machine - A Guide to Orchestrating Docker Environments

Installation

Docker Machine can be installed on Linux, macOS, and Windows.

macOS and Windows

For macOS and Windows users, Docker Desktop (formerly Docker for Mac/Windows) bundles the docker-machine binary. After installing Docker Desktop, you can immediately use Docker Machine.

To verify your installation and check the version:

docker-machine -v

Linux

Installing Docker Machine on Linux is straightforward. You can download the pre-compiled binary directly from the official GitHub Releases page.

For a 64-bit Linux system, use the following commands to download and make the binary executable:

sudo curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-machine
sudo chmod +x /usr/local/bin/docker-machine

Once completed, verify the installation:

docker-machine -v

Usage

Docker Machine supports various backend drivers, including virtualizers, local hosts, and cloud platforms.

Creating a Local Host Instance with the VirtualBox Driver

To create a Docker host named test using the virtualbox driver:

docker-machine create -d virtualbox test

You can customize the host or its Docker daemon during creation with additional parameters:

For a comprehensive list of parameters, use:

docker-machine create --driver virtualbox --help

Using the Generic Driver

The generic driver is useful for connecting to existing machines (physical or virtual) that already have SSH access.

docker-machine create -d generic \
    --generic-ip-address=123.59.188.19 \
    --generic-ssh-user=root \
    --generic-ssh-key ~/.ssh/id_rsa \
    dev

macOS xhyve Driver

The xhyve driver (GitHub: https://github.com/zchee/docker-machine-driver-xhyve) is a lightweight virtualization engine for macOS, offering better performance than the VirtualBox driver.

First, install the driver:

brew install docker-machine-driver-xhyve

Then, create a Docker Machine instance:

docker-machine create \
      -d xhyve \
      --engine-opt dns=8.8.8.8 \
      --engine-registry-mirror https://docker.mirror.aliyuncs.com \
      --xhyve-memory-size 2048 \
      --xhyve-rawdisk \
      --xhyve-cpu-count 2 \
      xhyve

Note: For subsequent creations, it’s recommended to include --xhyve-boot2docker-url ~/.docker/machine/cache/boot2docker.iso to avoid re-downloading the ISO image from GitHub every time.

For more parameters, use:

docker-machine create --driver xhyve --help

Windows 10 Hyper-V Driver

On Windows 10, if you have Docker Desktop installed, you might not be able to use VirtualBox concurrently. In such cases, the hyperv driver is a suitable alternative.

docker-machine create --driver hyperv vm

For more parameters, use:

docker-machine create --driver hyperv --help

Getting Started with Your Docker Host

After creating your Docker host, you can view a list of all managed hosts:

docker-machine ls

You’ll see output similar to this:

NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER       ERRORS
test      -        virtualbox   Running   tcp://192.168.99.187:2376           v19.03.12-ce

To configure your shell to interact with the target Docker host, use the env command. This sets the necessary environment variables.

docker-machine env test

Follow the instructions provided in the output to set the environment variables in your current shell (usually by running eval $(docker-machine env test)). Once done, any docker commands you run will be executed on the test host.

You can also directly SSH into the host:

docker-machine ssh test

Once connected, you can use Docker commands directly on the remote host:

docker@test:~$ docker --version
Docker version 19.03.12-ce, build 48a66213fe

Official Supported Drivers

Docker Machine supports a wide array of drivers, which you can specify with the -d option:


Common Docker Machine Commands

Here’s a list of common docker-machine commands:

To view the specific usage and parameters for any command, use:

docker-machine COMMAND --help