Configuring and working with Docker Machine

docker

Docker Machine is a component of Docker that allows management of remote virtual machines hosting docker containers. This post covers on overview of Docker Machine and some handy commands to configure and manage it.

Commands

Some of the commands covered in this post include:

  • docker-machine ls - Lists configured remote hosts
  • docker-machine create - Creates a new remote host
  • docker-machine env - Displays environment variables for the remote host
  • docker-machine ip - Displays the Ip Address of the remote host

This is the complete list of Docker Machine commands available from the command line.

Examples

docker-machine ls

docker-machine create --driver hyperv manager1

docker-machine env manager1

docker-machine ip manager1

And, the environment variables used by the local docker engine to communicate with a remote host

DOCKER_TLS_VERIFY=1
DOCKER_HOST=tcp://192.168.1.6:2376
DOCKER_CERT_PATH=C:\Users\sam.medley\.docker\machine\machines\manager1
DOCKER_MACHINE_NAME=manager1

Listing the configured machines

docker-machine ls

NAME       ACTIVE   DRIVER   STATE     URL                      SWARM   DOCKER    ERRORS
manager1   -        hyperv   Running   tcp://192.168.1.6:2376           v1.12.4

Create a new docker machine

This command creates a new docker machine host using the HyperV driver (on Windows) and names it manager1. It downloads and uses a basic OS image Boot2Docker to use as an operating system for the machine and configures a network and SSH for connections.

docker-machine create --driver hyperv manager1

Creating CA: C:\Users\sam.medley\.docker\machine\certs\ca.pem
Creating client certificate: C:\Users\sam.medley\.docker\machine\certs\cert.pem
Running pre-create checks...
(manager1) Image cache directory does not exist, creating it at C:\Users\sam.medley\.docker\machine\cache...
(manager1) No default Boot2Docker ISO found locally, downloading the latest release...
(manager1) Latest release for github.com/boot2docker/boot2docker is v1.12.4
(manager1) Downloading C:\Users\sam.medley\.docker\machine\cache\boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v1.12.4/boot2docker.iso...
(manager1) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(manager1) Copying C:\Users\sam.medley\.docker\machine\cache\boot2docker.iso to C:\Users\sam.medley\.docker\machine\machines\manager1\boot2docker.iso...
(manager1) Creating SSH key...
(manager1) Creating VM...
(manager1) Using switch "Hyper V Wifi"
(manager1) Creating VHD
(manager1) Starting VM...
(manager1) Waiting for host to start...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!

To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env manager1

Configuring your local shell

docker machine env is used to provide the details required for the local docker to configure management of the new machine. It provides a set of environment variables that can be configured in the shell. Without these, Docker won’t know which remote host you wish to use.

In order to see the environment variables to set for manager1, run the following.

docker-machine env manager1

When running this from the Windows Command Prompt, it will output:

SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.1.6:2376
SET DOCKER_CERT_PATH=C:\Users\sam.medley\.docker\machine\machines\manager1
SET DOCKER_MACHINE_NAME=manager1
REM Run this command to configure your shell:
REM     @FOR /f "tokens=*" %i IN ('docker-machine env manager1') DO @%i

The last line in the output can be executed in order to actually set the environment variables.

@FOR /f "tokens=*" %i IN ('docker-machine env manager1') DO @%i

Testing the setup

To ensure everything is set up correctly, a container can be run against the remote host.

docker run --rm busybox echo hello world

You’ll notice the remote host wasn’t mentioned in the command. The local Docker Engine will use the environment variable DOCKER_MACHINE_NAME to know which machine to interact with.

Other useful commands

This displays the IP address of manager1

docker-machine ip manager1

192.168.1.6

Alternatively, to see the currently configured machine that docker will communicate with

echo %DOCKER_MACHINE_NAME%

manager1

docker-machine ip %DOCKER_MACHINE_NAME%

192.168.1.6

References and further reading