How to deploy a Local Docker Container to Azure Container Apps
In the next few sections, we are going to take a fully built docker container from our local environment and deploy it to Azure Container Services.
Docker for Local Development
Local development with Docker enables developers to work faster, setting up complex development environments quickly, with short, simple commands, and taking them down as required. It enables the development process to continue seamlessly without the overhead of bloating development machines with several versions of software frameworks, DBMS, or other kind of development dependencies.
For this walkthrough, we will be using a Simple dotnet 7 API, we show its dockerfile below, but any locally built container will work if you are following along
Deploying Local Docker Containers
While using Docker for local development provides a seamless experience, deploying containers to production, or a test environment can be quite challenging with the number of offerings available, and not very straightforward processes. In this post, we are going to deploy a local docker container to Azure using Azure Container services, entirely from the CLI.
The process takes advantage of the following Azure Services:
- Azure Resource Groups
- Azure Container Registry
- Azure Container Apps
- Azure Container Apps Environment
We are using the Azure CLI here as it gets us into the habit of writing and using scripts for deployment, rather than clicking through the Azure Portal every time we want to deploy, or make an update.
Deploying Containers to Azure Container Services
Azure Container Services makes managing containers effortless, providing such benefits as automatic health check, auto scaling, metrics, monitoring and alerts, etc.
The Azure CLI makes deploying to azure painless, from login to container endpoint. Here’s an outline of what we will be doing:
- Installing the Azure CLI
- Login to Azure from the Azure CLI
- Create a Resource Group to manage associated services
- Create an Azure Container Registry Instance to store our container images in Azure
- Login to our ACR with docker so we can push local containers
- Create an Azure Container Apps environment to manage related containers
- Tag and push our local container to ACR
- Create the Container App
Installing the Azure CLI and AZ Login
The Azure CLI is available for Windows, Mac, or Linux and can be installed from https://docs.microsoft.com/en-us/cli/azure/install-azure-cli. After Installation, run the command `az login` and follow the prompt to log into Azure.
Creating a Resource Group
We create a resource group to manage all resources required for this deployment, the azure cli command for creating a resource group required the resource group name and a location. We create a resource group named “banki-rg” in the Canada Central Location by running the command
az group create `
--name banki-rg `
--location canadacentral
Creating an Azure Container Registry Instance
An Azure Container Registry Instance holds our container images and revisions in Azure, we need to create one to push our local container image to. The az cli command to create a container registry takes a few parameters, including; the registry name, the resource group and the sku.
With the command below we create an ACR in the previously created resource group using the name “bankiacr”
az acr create `
--name bankiacr`
--resource-group banki-rg`
--sku Basic `
--admin-enabled true
If this command executes successfully, we can proceed to retrieve the login credentials for our ACR automatically generated by Azure. To retrieve our login credentials from ACR, we run the command
az acr credential show --resource-group banki-rg --name bankiacr
Copy and save the credentials returned from the command.
Docker Login
We need a way to get our local containers into our created ACR, for this, we use the docker login command for authentication with the credentials retrieved from the previous step. Run the command below to authenticate out local docker install against our ACR
docker login bankiacr.azurecr.io --username <username> --password <password>
At this point, our local docker daemon is ready to push images to ACR.
Pushing Local Container Images to ACR
We will now push our local container image to ACR, first we will tag a container image we have locally, I am using an image named banki-api here, remember to replace with whatever name you have given your images locally. The following command tags, and pushes the container to ACR.
docker tag banki-api bankiacr.azurecr.io/banki/api
docker push bankiacr.azurecr.io/banki/api
At this point we can verify from the Azure portal that we do indeed have an image named “api” in our Registry.
Create An Azure Container Apps Environment
Azure container Apps environments can be used to managed related containers, we can create one using the command below to deploy our pushed container image into.
az containerapp env create `
--name banki-prod-env `
--resource-group banki-rg `
--location canadacentral
Deploy the Image
At this point we have our image in Azure, we also have our container apps environment ready and simply need to deploy the image. We use the `az containerapp create` command to create the container as shown below
az containerapp create `
--name api `
--resource-group banki-rg `
--environment banki-prod-env `
--image bankiacr.azurecr.io/banki/api:latest `
--target-port 80 `
--ingress 'external' `
--query properties.configuration.ingress.fqdn `
--registry-server bankiacr.azurecr.io
Note that we have supplied the image to be deployed to the container in the –image parameter. This command returns an endpoint we can use to interact with our running container. Here's how our container looks in Azure below
Conclusion
In this post, we have successfully taken a local container image and deployed it to docker, showing the required resources and commands. In future posts we will talk about how to add more containers to out container environment, cross container communication, metrics and management.