Up and running with Azure driver for docker-machine

What is Docker Machine?

According to the documentation:

“Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands. You can use Machine to create Docker hosts on your local Mac or Windows box, on your company network, in your data center, or on cloud providers like Azure, AWS, or Digital Ocean.”

Docker Machine automates the flow of provisioning a VM with an Operating System, installing Docker engine and required dependencies on a cloud provider or on on-premises system(s). It is also helpful to setup a quick cluster of Docker engines and setting up a Swarm.

Using Docker Machine with Microsoft Azure

Now let’s see Docker Machine in action with Microsoft Azure driver. Here we will provision a new Azure Ubuntu 16.04 LTS VM with Docker installed via docker-machine CLI.

Installing Docker Machine

Docker machine comes along with Docker for Windows, Docker for Mac and legacy Docker Toolbox. For Linux users, refer to this article to learn how to install Docker Machine for your particular distribution.

Creating Azure VM with docker-machine

Open up your terminal and look for docker-machine command to make sure that Docker Machine is up and running on your system. Run the docker-machine ls to see existing Docker Machines on your system.

To create a Docker Machine, we use a Docker Machine driver for a particular cloud provider or hyper-visor (such as Oracle VBox) to provision the virtual machine and install latest version of docker. We will use docker-machine create command with the following Syntax:

docker-machine create --driver [provider] --azure-subscription-id [id] --azure-subnet-prefix [azure-vnet-subnet] --azure-open-port [port] --azure-private-ip-address [private-ip-address] --azure-location [azure-region] [machine-name] 

Although there are other options\flags available to further customize your deployments such as changing the VM image etc. I’ll use the following command:

docker-machine create --driver azure --azure-open-port 80 --azure-subscription-id axxxxx-xxxx-xxxx-xxxx-xxxxx --azure-subnet-prefix 10.0.0.0/24 --azure-private-ip-address 10.0.0.5 --azure-location "Southeast Asia" machine

This will setup an Azure Virtual Machine:

  • In Azure SouthEast Asia Region with the VM name machine
  • In a Azure VNet with a Public-IP master-ip address prefix
  • In a private Subnet 10.0.0.0/24 and with a private IP address 10.0.0.5
  • Install latest version of Docker and allow Internet traffic from port 80 via an NSG
  • With a Ubuntu 16.04 LTS VM Image (default)

After a short time, it will successfully provision the VM with its components and now run docker-machine ls on your machine with Microsoft Azure driver and you’ll see Docker Machine is ready.

docker-machine-azure-1

Configuring Docker Client to connect to the docker-machine

The final step needed here is to configure your docker CLI to point the Docker Machine running in Azure. To do this, run docker-machine env machine command to see the required configuration details.

docker-machine-azure-1

Note the DOCKER_HOST environment variable. It displays the Public IP address of the VM that we just provisioned.

After setting Docker client, every Docker command that you run on your location terminal will be executed on the remote Azure VM. So pull a sample image by running:

docker pull kjanshair/aspnetcore-example

Run the container:

docker run -d --name app -p 80:80 kjanshair/aspnetcore-example

And browse to the Public IP address of the VM in a browser and you will see that the application is up and running.

docker-machine-azure-1