Freigeben über


Step by step: Set up a Docker host on Azure using CLI on a Mac

After I published my architectural discussion on how to scale a simple dockerized Apache-PHP application for production use at large scale on github, I got several requests to publish the steps to get a Docker host up and running - mostly because that is a pre-requisite to replicate my small project.

Hence, I am going to quickly document the steps you can follow from your Mac to get a Docker host running on Microsoft Azure.

In order to do follow these steps, you will need an Azure subscription, which I assume you will have.

  1. Install Node and npm on on your Mac, as that is one of the easier ways to get the Azure CLI (Command Line Interface) installed.

    Follow the steps here to do so: https://blog.teamtreehouse.com/install-node-js-npm-mac

    One thing that the above blog does not go into the details of is how to install the correct Xcode on your Mac. If you go to the Apple Store, you will only get the latest version of Xcode. But your Mac may be running an older version of OSX, which cannot install the latest version of Xcode. If that happens, you will have to find which Xcode version is good for your OSX (usually Xcode 6.2 works well on OSX 10.X, please verify the support matrix by running some searches). Once you know which Xcode version to install, download it from Apple developer download center and install it.

  2. Install the Azure CLI

    npm install -g azure-cli

  3. Check if the CLI is working by running some basic commands

    azure help
    azure --version

  4. Login to your Azure account (the following command is for interactive login using a browser window, there are other ways too)

    azure login

  5. If you have multiple subscriptions, run this command to see all of them so that you can verify whether the CLI is using the correct one. The "Current" column of the output will be "true" for the subscription being currently used.

    azure account list

  6. If you have multiple subscriptions, and if the above command shows that the CLI session is using the incorrect one, use the following command to set the correct one as default:

    azure account set <<name of the subscription you want to set as default from the "Name" column displayed in the output of the "azure account list" command>>

  7. Go to Azure ARM mode (if you do not know the differences between the two modes or deployment models supported by Azure - the older "classic" model and the newer "ARM" model, where ARM stands for "Azure Resource Manager", see this):

    azure config mode arm

  8. An easy way to create a Docker host on Azure is to just execute a pre-existing JSON template that Microsoft template developers have contributed to github. You can find a lot of templates in this quickstart template repository.

    I picked one that says docker-simple-on-ubuntu. The beauty of these templates is that you just run a CLI command with the template file's location on github and your infrastructure, whatever the template is written to deploy, will get created and deployed within a few minutes!

    azure group create --name file-upload-container-demo --location "West US" --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/docker-simple-on-ubuntu/azuredeploy.json

    In the above command, I passed, as value for the --name parameter, "file-upload-container-demo". That creates an Azure Resource Group by that name. The benefit of creating your entire infrastructure within a new Resource Group is that you can, later, simply delete the Resource Group, and all the VM-s, VNET-s, Load Balancers, IP Addresses, Network Security Groups, NIC-s, etc. you had deployed withing that Resource Group will all be removed in one go - so cleaning up is easy. You may be thinking... why am I going to deploy all these things to run a simple Docker host?

    The answer to that is, you need some of these basic infrastructure components to have a usable Virtual Machine. For example, you can spin up a VM, but unless you have a public IP address, you cannot access it easily. Unless you have a NSG (Network Security Group), you cannot selectively open ports (like 22 for SSH or 80 for web). When you create a VM from the Azure portal, these peripheral infrastructure components are automatically created for you. But when you are creating something from a command line or code, you will need to create all of these. Luckily, that is where the template helps. The template here has instruction to create all of these, and just one CLI command to execute the ARM Template is enough to stand up the whole gamut of components needed to start using your Docker host quickly!

    One possible down-side of using this pre-written template is that you may not want your Docker Host to be of size standard F1. But that is what you will get, as the template specifies that. You may want your storage disks to be either HDD or SSD, but you will get whatever the template specifies. In short, if you need to customize your deployment like what you want, you will have to write your own template. Some template authors go the extra mile to make these choices configurable. In that case, you will have to pass these as parameters for the template.

  9. The above command is interactive, and it will ask you a few things. Once it succeeds, your Docker host is ready. It will interactively ask you some questions like the DNS name for your VM, use a unique name for the region you are using in the command.

    In my case, I specified the DNS Name "uploadnshare0123" and user name "user_name", so trying to access the VM through the DNS name associated with it via SSH from my Mac is done by a simple ssh command

    ssh user_name@uploadnshare0123.westus.cloudapp.azure.com

  10. Once you are in, start running docker commands

    sudo docker ps

  11. If you are wondering how exactly Docker is running on the machine without you installing it, it is all magic done inside the JSON ARM template. Read it here to understand it in greater details. Good luck!