Get Started with Docker on Azure

Published on
Reading time
Authors

The most important part of this whole post is that you need to know that the whale in the Docker logo is officially named "Moby Dock". Once you know that you can probably bluff your way through at least an introductory session on Docker 🙂.

It's been hard to miss the increasing presence of Docker, particularly if you work in cloud technology. Each of the major cloud providers has raced to provide container services (Azure, AWS, GCE and IBM) and these platforms see benefits in the higher density hosting they can achieve with minimal changes to existing infrastructure.

In this post I'm going to look at first steps to getting Docker running in Azure. There are other posts about that will cover this but there are a few gotchas along the way that I will cover off here.

First You Need a Beard

Anyone worth their take home pay who works with *nix needs to grow a beard. Not one of those hipstery-type things you see on bare-ankled fixie riders. No - a real beard.

While Microsoft works on adding Docker support in the next Windows Server release you are, for the most part, stuck using a Linux variant to host and manage your Docker containers.

The Azure Cross-Platform Command-Line Interface teases you with the ability to create Docker hosts from a Windows-based computer, but ultimately you'll have a much easier experience running it all from a Linux environment (even if you do download the xplat-cli there anyway).

If you do try to set things up using a Windows machine you'll have to do a little dancing to get certificates setup (see my answer on this stackoverflow post). This is shortly followed by the realisation that you then can't manage the host you just created by getting those nice certificates onto another host - too much work if you ask me :).

While we're on Docker and Windows let's talk a little about boot2docker. This is designed to provide an easy way to get started with Docker and while it's a great idea (especially for Windows users) you will have problems if you are running Hyper-V already due to boot2docker's use of Virtualbox which won't run if you already have Hyper-V installed.

So Linux it is then!

Management Machine

Firstly let's setup a Linux host that will be our Docker management host. For this post we'll use a CentOS 7 host (I've avoided using Ubuntu because there are some challenges installing and using node.js which is required for the Azure xplat CLI).

create-linux-vm.ps1
# Script assumes you have setup your subscription and
# have a default storage account in West US.

# You should change these to values you want.
$cloudService = "{cloudservice}"
$hostname = "{dockermanagementhost}"
$linuxUser = "{linxuser}"
$linuxPass = "{linxpasswd}"
$location = "West US"

# use CentOS 7 image
$centOSImageName = (Get-AzureVMImage | Where-Object {$_.ImageName -like "*OpenLogic-CentOS-70*"})[0].ImageName

New-AzureVMConfig -Name $hostname -InstanceSize Small -ImageName $centOSImageName | Add-AzureProvisioningConfig –Linux –LinuxUser $linuxUser -Password $linuxPass | New-AzureVM -ServiceName $cloudService -Location $location

Once this machine is up and running we can SSH into it and install the required packages. Note that you'll need to run this script as a root-equivalent user.

setup-docker-manager.sh
#!/bin/bash

if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi

# pull down the necessary bits to install node
curl -sL https://rpm.nodesource.com/setup | bash -

# install Node.js
yum install -y nodejs

# install Azure XPlat CLI
npm install azure-cli -g

# install core Git (scm) - required to pull our Dockerfile
yum install git-core

# install Docker so we get the native management tooling
yum install docker

Now we have our bits to manage the Docker environment we can now build an image and actual Docker container host.

Docker Container Host

On Azure the easiest way to get going to with Docker is to use the cross platform CLI's Docker features.

As a non-root user on our management linux box we can run the following commands to get our Docker host up and running. I'm using an Organisational Account here so I don't need to download any settings files.

# will prompt for username and password
[sw@sw1 ~]$ azure login

# set mode to service management
[sw@sw1 ~]$ azure config mode asm

# get the list of Ubuntu images - select one for the next command
[sw@sw1 ~]$ azure vm image list | grep Ubuntu-14_04

# setup the host - replace placeholders
[sw@sw1 ~]$ azure vm docker create -e 22 -l "West US" {dockerhost} "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04_1-LTS-amd64-server-20141125-en-us-30GB" {linxuser} {linxpwd}

At this point we now have a new Azure VM up and running that has all the necessary Docker bits installed for us. If we look at the VM's entry in the Azure Portal we can see that ports 22 and 4243 are already open for us. We can go ahead and test that everything's good. Don't forget to substitute your hostname!

[sw@sw1 ~]$ docker --tls -H tcp://{dockerhost}.cloudapp.net:4243 info

Deploy an Image to a Container

As we have our baseline infrastructure ready to rock so let's go ahead and deploy an image to it. For the purpose of this post we are going to use the wordpress-nginx image that can be built using the configuration in this Github repository.

On our management host we can run the following commands to build the image from the Dockerfile contained in the Git repository.

[sw@sw1 ~]$ git clone https://github.com/eugeneware/docker-wordpress-nginx.git
[sw@sw1 ~]$ docker --tls -H tcp://{dockerhost}.cloudapp.net:4243 build -t="docker-wordpress-nginx" docker-wordpress-nginx/

Note: you need to make sure you run this as the user who setup the Docker container host and that you do it in the home directory of the user. This is because the certificates generated by the container host setup are stored in the user's home folder in a directory called .docker. Also, expect this process to take a reasonable amount of time because it's having to pull down a lot of data!

Once our image build is finished we can verify that it is on the Docker host by issuing this command:

[sw@sw1 ~]$ docker --tls -H tcp://{dockerhost}.cloudapp.net:4243 images

Let's create a new containerised version of the image and map the HTTP port out so we can access it from elsewhere in the world (we're going to map port 80 to port 80). I'm also going to supply a friendly name for the container so I can easily reference it going forward (if I didn't do this I'd get a nice long random string I'd need to use each time).

[sw@sw1 ~]$ docker --tls -H tcp://{dockerhost}.cloudapp.net:4243 create -p 80:80 --name="dwn01" docker-wordpress-nginx

Now that we have created this we can start the container and it will happily run until we stop it 🙂.

[sw@sw1 ~]$ docker --tls -H tcp://{dockerhost}.cloudapp.net:4243 start dwn01

If we return to the VM management section in the Azure Management Portal and add an Endpoint to map to port 80 on our Docker container host we can then open up our Wordpress setup page in a web browser and configure up Wordpress.

If we simply stop the container we will lose any changes to the running environment. Docker provides us with the 'commit' command to rectify this. Let's go ahead and save our state:

[sw@sw1 ~]$ docker --tls -H tcp://{dockerhost}.cloudapp.net:4243 commit dwn01 sw/dwn01

and then we can stop the Container.

[sw@sw1 ~]$ docker --tls -H tcp://{dockerhost}.cloudapp.net:4243 stop dwn01

We now have a preserved state container along with the original unchanged one. If we want to move this container to another platform that supports Docker we could also do that, or we could repeat all our changes based on the original unchanged container.

This has been a very brief overview of Docker on Azure - hopefully it will get you started with the basics and comfortable with the mechanics of setting and up and managing Docker.