Deploying Docker Compose to Azure
local graph
Chapter 12 — Deploying Docker Compose to Azure
Section titled “Chapter 12 — Deploying Docker Compose to Azure”In the previous chapter, we deployed our application to AWS. We deployed the application using autoscaling and health checks, and we even managed to access the application through a DNS domain. In this chapter, we shall focus on another popular cloud provider, Azure. Azure provides us with Azure Container Instances (ACI), a seamless way to run Compose-based applications without managing any infrastructure. Deploying to ACI is simple; first, we shall push the application images to an Azure container registry, and then, with a few adjustments, we shall deploy our application to Azure ACI.
Deploying to ACI comes with less infrastructure maintenance overhead as well as with the simplicity of the Compose syntax.
We will cover the following topics in this chapter:
- An introduction to ACI
- Pushing to an Azure container registry
- Adapting Compose files for Azure container groups
- Deploying your Compose application to Azure container groups
Technical requirements
Section titled “Technical requirements”The code for this book is hosted on the GitHub repository at https://github.com/PacktPublishing/A-Developer-s-Essential-Guide-to-Docker-Compose. If there is an update to the code, it will be updated on the GitHub repository.
In this chapter, Terraform plans will be used. To install Terraform on your system, you can follow the instructions from the official documentation (https://learn.hashicorp.com/tutorials/terraform/install-cli).
Also, az, the Azure command-line interface, needs to be installed. The instructions can be found in the official documentation (https://docs.microsoft.com/en-us/cli/azure/).
An introduction to ACI
Section titled “An introduction to ACI”ACI is a serverless solution provided by Microsoft Azure. ACI makes it possible to deploy containers without managing any servers and infrastructure.
A benefit of ACI is that you can run multiple containers without the need to deal with the complexity of a Docker orchestration engine.
However, ACI has some limitations. Based on the region, there are some limits on the maximum number of CPUs (four CPUs for every region) as well as on the available memory. More information on the limitations can be found in the docs (https://docs.microsoft.com/en-us/azure/container-instances/container-instances-region-availability). Another limitation is the fact that scaling on the containers is not available (https://docs.microsoft.com/en-us/azure/container-instances/container-instances-faq#how-do-i-scale-a-container-group-).
It is important to take the preceding into consideration, since they play a crucial role in understanding whether our application is suitable for ACI. In our case, our application can benefit from ACI since it is easy to deploy and has simple requirements.
To deploy to ACI, we need to have our images accessible in a way so that ACI will retrieve them. A registry must be in place, so we will use the Docker registry that Azure provides. Next, we will create an Azure container registry.
Pushing to an Azure container registry
Section titled “Pushing to an Azure container registry”An Azure container registry will play a significant role in our deployment. It will be a universally available registry well integrated with the Azure infrastructure that we will provision.
A basic step to creating resources on Azure is to manage them under a resource group. Think of an Azure resource group as a container that includes all the resources needed for our application. It is a way to logically separate infrastructure. For example, deleting a resource group will lead to the deletion of the resources provisioned under this resource group.
Our Azure container registry as well as our ACI will reside in a resource group that we will provision. This can be done in many ways – for example, through the command line, the Azure portal, as well as using Terraform.
Adding a resource group via Terraform should look like this:
resource "azurerm_resource_group" "guide_to_docker_compose_resource_group" { name = "guidetodockercompose" location = "eastus"}We picked eastus on purpose, since it has more features and fewer limitations than other regions when it comes to ACI.
Now that we have the resource group in place, we can create a registry:
resource "azurerm_container_registry" "guide_to_docker_compose_registry" { name = "developerguidetocomposeacr" resource_group_name = azurerm_resource_group.guide_to_docker_compose_resource_group.name location = azurerm_resource_group.guide_to_docker_compose_resource_group.location sku = "Basic" admin_enabled = false}Before we proceed to provision infrastructure using Terraform, we need to authenticate to Azure through the command line so that Terraform will be able to authenticate to Azure. One of the ways is through the Azure CLI. There are other ways to authenticate Terraform to Azure such as a service principal, or using a client secret or certificate; we will use the az command for simplicity purposes.
Authenticate using az:
$ az loginA web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with \`az login --use-device-code\`.[ { ... "user": { "name": "john@doe", "type": "user" ... } }]A browser link will open asking us to log in. Once done, we shall be able to execute commands using az but also, we should be able to run Terraform plans on our Azure account.
The commands to use are the same as we used in the previous chapter:
$ terraform init$ terraform applyThe infrastructure will be provisioned and the Terraform state will be hosted on our local filesystem. However, there is a more managed way to store the Terraform state, distribute it to other team members, and prevent conflicts when accessing it.
Storing the Terraform state file
Section titled “Storing the Terraform state file”By just running the previous snippet, the state of the infrastructure provisioned will be kept locally. This is not something recommended in production, as the state should remain physically somewhere where it is feasible for multiple team members to access it. We shall use a storage account from Azure to achieve our goal.
Since we can create multiple resource groups, there will be a dedicated storage account for our Terraform plans.
Let’s create the storage account through a script:
#!/bin/bashRESOURCE_GROUP_NAME=guide-to-docker-compose-tfSTORAGE_ACCOUNT_NAME=guidetodockercomposetfCONTAINER_NAME=tfstateaz group create --name $RESOURCE_GROUP_NAME --location northeuropeaz storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blobaz storage container create --name $CONTAINER_NAME --account-name $STORAGE_ACCOUNT_NAMEWe can now change the provider so that the state will be stored on the storage account:
terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "=2.48.0" } } backend "azurerm" { resource_group_name = "guide-to-docker-compose-tf" storage_account_name = "guidetodockercomposetf" container_name = "tfstate" key = "terraform.tfstate" }}provider "azurerm" { features {}}We set up the foundation to be able to use the ACI. We have a container registry available as well as a resource group where our ACI application will be provisioned. We will now proceed and adapt our application to make a deployment to ACI.
Deploying on ACI
Section titled “Deploying on ACI”Since we have a container registry in place, we can now upload the Docker images we shall build through our Compose application. The container endpoint can be retrieved by checking the registry on the Azure account.
The registry endpoint should be developerguidetocomposeecr.azurecr.io/developer-guide-to-compose.
We can now adapt our Compose file and set the name with the container images we shall push:
services: location-service:... image: developerguidetocomposeacr.azurecr.io/developer-guide-to-compose:location-service_0.1... event-service:... image: developerguidetocomposeacr.azurecr.io/developer-guide-to-compose:events-service_0.1... task-manager:... image: developerguidetocomposeacr.azurecr.io/developer-guide-to-compose:task-manager_0.1Changing the image to the full path of the Azure registry brings us closer to deployment; however, there are more adjustments to be made due to the nature of ACI. More specifically, there are limits on the ports that can be used on an application as well as the limitation in regards to the resources available.
One workaround we need has to do with ports.
As mentioned in the documentation (https://docs.microsoft.com/en-us/azure/container-instances/container-instances-container-groups#networking), “within a container group, container instances can reach each other via localhost on any port, even if those ports aren’t exposed externally on the group’s IP address or from the container.”
If Task Manager and the location service have the same port, then there is going to be a conflict and the following error will occur:
listen tcp :8080: bind: address already in useTo resolve this, we shall set Task Manager to use port 80:
task-manager: container_name: task-manager ... image: developerguidetocomposeacr.azurecr.io/developer-guide-to-compose:task-manager_0.1 ports: - 80:80 environment: ... - TASK_MANAGER_HOST=:80By using this workaround, we shall avoid the port conflict.
Another issue also has to do with the readiness of some services. event-service will try to access the Redis service immediately after being initiated, and this might cause a failure. At the time of writing, depends_on is unsupported (https://docs.microsoft.com/en-us/azure/app-service/configure-custom-container?pivots=container-linux#unsupported-options).
We can overcome this issue in a creative way. We shall adapt event-service and add some sleep before running the binary that is listening to the Redis stream:
event-service: container_name: event-service ... command: - /bin/sh - -c - | sleep 120 /events_serviceThis is a simple workaround; however, there can be more effective workarounds that might even include a change in the code base and a health check adaption.
The next issue we should tackle is the resource limitations that come along with ACI.
In most regions, ACI comes with the limitation of four CPUs. By default, when a service is spun up, one CPU is allocated per service. This makes our deployment non-feasible, since we have more than four services.
In order to tackle this, we will adapt the resources used by the containers in our Compose application.
Docker Compose enables us to define resource constraints on the containers of a service. The available resource options are CPU and memory.
When we define the memory in the resources section such as memory: 512M, we instruct the container of the service to use up to 512 megabytes.
When we define the CPU on the resources section such as cpu: 0.5, we instruct the container of the service to use half of the available time of a processing core.
When defining the resources that a container will use, we have two options, limits and reservations.
The purpose of limits is to constrain the amount of resources to be used. This means that if our application has a burst of requests that require extra resources, the resources allocated will be capped by the limits specified.
By using reservations, we reserve the minimum resources that our application needs in order to be functional. If the resources are not available, our application is not able to operate.
For our application, due to the resource limitations, and because the default settings of our application will surpass the limitations (one CPU per service), we will proceed to use the resources configuration from Compose:
Setting cpu and memory limits:services: location-service: ... deploy: resources: limits: cpus: '0.5' memory: 512M reservations: cpus: '0.5' memory: 512M event-service: ... deploy: resources: limits: cpus: '0.5' memory: 1024M reservations: cpus: '0.5' memory: 1024M task-manager: ... deploy: resources: limits: cpus: '0.5' memory: 1024M reservations: cpus: '0.5' memory: 1024M...So now, we can push the images and execute requests toward our application. Before we push the Docker images, we need to authenticate to the recently created Azure registry.
First, we need to log in to Azure and then the registry:
$ az login$ az acr login --name developerguidetocomposeacrNow, it is possible to push to the container registries.
Let’s build and push:
$ docker compose build --no-cache$ docker compose push...Pushing location-service: cf8204bbc172 Pushing [==================================================>] 557.8MB 265.5s[+] Running 1/43ion-service: db60c013991c Pushed 265.5s... Pushing event-service: 24302eb7d908 Pushed 269.7sPushing was a success; we now need to make it feasible for Docker to create resources on Azure:
$ docker login azureThis way, Docker will retrieve credentials in order to be able to interact with Azure. Onward, we shall create a Docker context that will interact with the Azure resource group we created previously:
$ docker context create aci guide-to-compose-azureUsing only available subscription : Pay-As-You-Go (c8ce802e-f8d5-4634-b279-8d203a9c4882)? Select a resource group [Use arrows to move, type to filter] create a new resource group> guidetodockercompose (eastus)We are set up and can proceed to deploy our application to ACI:
$ docker --context=guide-to-compose-azure compose up[+] Running 5/5 Group azure Created 10.1s redis Created 144.7s event-service Created 144.7s task-manager Created 144.7s location-service Created 144.7sBy using docker compose ps, we shall retrieve the IP for our application:
$ docker --context=guide-to-compose-azure compose psNAME COMMAND SERVICE STATUS PORTSazure_event-service "" event-service Runningazure_location-service "" location-service Runningazure_redis "" redis Runningazure_task-manager "" task-manager Running 20.237.67.76:80->80/tcp:80->80/TCPBy exposing a port through Compose, ACI will set up a load balancer and forward the requests from that IP to our service. Only one service can expose the specified port number.
As we can see, port 80 is exposed through the IP. We also configure the application to be accessible through a DNS domain:
services: task-manager: … domainname: "developerguidetocompose"By updating the application using compose up, we should be able to have the service exposed through DNS:
$ docker --context=guide-to-compose-azure compose up...$ docker --context=guide-to-compose-azure compose ps...azure_task-manager "" task-manager Running developerguidetocompose.eastus.azurecontainer.io:80->80/tcp:80->80/TCPWe can now access our application using a DNS. Using our own DNS name is also feasible.
As we can see, we have made it and managed to access our application through DNS. We uploaded the containers to the Azure container registry, adapted our application, and made it feasible to be deployed to ACI.
Summary
Section titled “Summary”In this chapter, we managed to deploy our Compose application to ACI. We created a Docker registry on Azure and pushed the Docker images to the registry. We then deployed our application to ACI without the need of provisioning any infrastructure. We did some essential modifications for our application to run and also tested our application through a DNS domain. So far, we have covered running Compose on two major cloud providers, AWS and Azure, while utilizing their container orchestration engine offerings.
In the next chapter, we shall try a different orchestration engine, the popular Kubernetes. We shall migrate an existing Compose application to a Kubernetes one.