In this guide I will walk you through how to deploy Docker to Azure using only command-line tools.
Install Azure CLI and login
First you must install Azure CLI if you do not already have it on your machine. Please follow Microsoft’s instructions to install the tool before returning to this guide.
Now that you have Azure CLI installed, open up a command prompt and verify that the tool has been added to the PATH by running
az. You should see a welcome message and a list of the commands that are available to you.
Before we can go any further we need to tell Azure CLI which account we will be running commands against. We do this by running
az login and logging in to Azure through our web browser. Once authenticated we can then close the browser and Azure CLI will keep track of who we are for any subsequent commands that we run.
Create resource group
Each deployment in Azure must be associated with a “Resource Group”. You can think of a resource group as a logical container for all of the infrastructure resources needed to deploy a single application. These resources can then benefit from sharing of important details like billing, monitoring and access control. The encapsulation that resource groups offer also helps to keep large infrastructure setups more manageable.
To create resource groups we use the command
az group create passing in a location and a unique name to identify the resource group by. For the purposes of this guide we will create a resource group in the centralus location named MyResourceGroup using the following command.
az group create -l centralus -n MyResourceGroup
If the command is successful it will return some JSON with details of the newly created resource group, including its provisioning state which should be ‘Succeeded’.
Info: Find out how you can list location names in the command section of this article.
A deployment is a method of creating the necessary infrastructure to run an application. The specification of this infrastructure, which can include databases, virtual machines, public IP addresses and more is described in a JSON formatted template file. Azure CLI can read this template file from the local file system or from a URI.
parameters property lists any parameters that we must provide to the template during deployment. Those parameters with default values we will not be asked to provide. That leaves only the following parameters.
To deploy this template we will run
az group deployment create which takes the name of a resource group and a template URI as arguments.
az group deployment create -g MyResourceGroup --template-uri=https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/docker-simple-on-ubuntu/azuredeploy.json
Azure CLI will then prompt you to enter values for the three required template parameters and then it will go off and create all of the resources described by the template, resulting in a Docker instance that you can now ssh into.
SSH into your Docker instance
In order to ssh into the Docker instance that has been created we first need to obtain its FQDN. We can do that by running the following command and looking for the
az network public-ip list -g MyResourceGroup
Now we can ssh into the server using the admin credentials we provided during deployment and the FQDN.
Running a Docker image
Once we are on the server we can proceed to running an application with Docker. The application that we will be using is the official ASP.NET Core Docker sample aspnetapp. Microsoft has already submitted this sample to the public Docker registry under microsoft/dotnet-samples.
To start the sample inside a container we just run
docker run with the right arguments, as below.
docker run --name aspnetcore_sample --rm -it -p 8000:80 microsoft/dotnet-samples:aspnetapp
Docker will then pull the image, build it locally and then set it running on port 8000. To dissect the command you can reference the official documentation.
Warning: At this stage we are not ready to deploy to production. If the virtual machine reboots the application will not automatically restart itself. We will cover that in a separate guide.
Bonus: Explore more commands
The Azure CLI is well documented. You can access the usage instructions of each command by appending
If you are on a test account, try some of the following commands.
|List resource groups||az group list|
|Delete resource group||az group delete -g ResourceGroupName|
|List resources in resource group||az group deployment list -g ResourceGroupName|
|List available geographic locations||az account list-locations|
Don’t worry too much about breaking your Docker instance whilst exploring. If you do succeed in breaking it you can always delete the resource group and start again.