Quickstart: Deploy an existing container image with the command line
The Azure Container Apps service enables you to run microservices and containerized applications on a serverless platform. With Container Apps, you enjoy the benefits of running containers while you leave behind the concerns of manual cloud infrastructure configuration and complex container orchestrators.
This article demonstrates how to deploy an existing container to Azure Container Apps.
Note
Private registry authorization is supported via registry username and password.
Prerequisites
- An Azure account with an active subscription.
- If you don't have one, you can create one for free.
- Install the Azure CLI.
- Access to a public or private container registry, such as the Azure Container Registry.
Setup
To sign in to Azure from the CLI, run the following command and follow the prompts to complete the authentication process.
az login
To ensure you're running the latest version of the CLI, run the upgrade command.
az upgrade
Next, install or update the Azure Container Apps extension for the CLI.
If you receive errors about missing parameters when you run az containerapp
commands in Azure CLI or cmdlets from the Az.App
module in Azure PowerShell, be sure you have the latest version of the Azure Container Apps extension installed.
az extension add --name containerapp --upgrade
Note
Starting in May 2024, Azure CLI extensions no longer enable preview features by default. To access Container Apps preview features, install the Container Apps extension with --allow-preview true
.
az extension add --name containerapp --upgrade --allow-preview true
Now that the current extension or module is installed, register the Microsoft.App
and Microsoft.OperationalInsights
namespaces.
az provider register --namespace Microsoft.App
az provider register --namespace Microsoft.OperationalInsights
Set environment variables
Set the following environment variables. Replace the <PLACEHOLDERS>
with your values:
RESOURCE_GROUP="<RESOURCE_GROUP>"
LOCATION="<LOCATION>"
CONTAINERAPPS_ENVIRONMENT="<CONTAINERAPPS_ENVIRONMENT>"
Create an Azure resource group
Create a resource group to organize the services related to your container app deployment.
az group create \
--name $RESOURCE_GROUP \
--location "$LOCATION"
Create an environment
An environment in Azure Container Apps creates a secure boundary around a group of container apps. Container Apps deployed to the same environment are deployed in the same virtual network and write logs to the same Log Analytics workspace.
To create the environment, run the following command:
az containerapp env create \
--name $CONTAINERAPPS_ENVIRONMENT \
--resource-group $RESOURCE_GROUP \
--location "$LOCATION"
Create a container app
Now that you have an environment created, you can deploy your first container app.
Set the environment variables.
Replace the
<PLACEHOLDERS>
with your values. Your user principal name will typically be in the format of an email address (for example,username@domain.com
).CONTAINER_APP_NAME=my-container-app KEY_VAULT_NAME=my-key-vault USER_PRINCIPAL_NAME=<USER_PRINCIPAL_NAME> SECRET_NAME=my-secret-name CONTAINER_IMAGE_NAME=<CONTAINER_IMAGE_NAME> REGISTRY_SERVER=<REGISTRY_SERVER> REGISTRY_USERNAME=<REGISTRY_USERNAME>
Create the key vault.
Storing your container registry password using a service such as Azure Key Vault keeps the values secure at all times. The steps in this section show how to create a key vault, store your container registry password the Key Vault, and then retrieve the password for use in your code.
az keyvault create --name $KEY_VAULT_NAME --resource-group $RESOURCE_GROUP
Give your user account permissions to manage secrets in the key vault.
KEY_VAULT_ID=$(az keyvault show --name $KEY_VAULT_NAME --query id --output tsv) az role assignment create --role "Key Vault Secrets Officer" --assignee "$USER_PRINCIPAL_NAME" --scope "$KEY_VAULT_ID"
Store your container registry password in the key vault.
Replace
<REGISTRY_PASSWORD>
with your value.az keyvault secret set --vault-name $KEY_VAULT_NAME --name $SECRET_NAME --value "<REGISTRY_PASSWORD>"
Retrieve your container registry password from the key vault.
REGISTRY_PASSWORD=$(az keyvault secret show --name $SECRET_NAME --vault-name $KEY_VAULT_NAME --query value --output tsv)
Deploy a container image to Azure Container Apps.
az containerapp create \ --name $CONTAINER_APP_NAME \ --location $LOCATION \ --resource-group $RESOURCE_GROUP \ --image $CONTAINER_IMAGE_NAME \ --environment $CONTAINERAPPS_ENVIRONMENT \ --registry-server $REGISTRY_SERVER \ --registry-username $REGISTRY_USERNAME \ --registry-password $REGISTRY_PASSWORD
If you have enabled ingress on your container app, you can add
--query properties.configuration.ingress.fqdn
to thecreate
command to return the public URL for the application.
Set the environment variables.
CONTAINER_APP_NAME=my-container-app CONTAINER_IMAGE_NAME=mcr.microsoft.com/k8se/quickstart:latest
Deploy a container image to Azure Container Apps.
az containerapp create \ --image $CONTAINER_IMAGE_NAME \ --name $CONTAINER_APP_NAME \ --resource-group $RESOURCE_GROUP \ --environment $CONTAINERAPPS_ENVIRONMENT
If you have enabled ingress on your container app, you can add
--query properties.configuration.ingress.fqdn
to thecreate
command to return the public URL for the application.
Verify deployment
To verify a successful deployment, you can query the Log Analytics workspace. You might have to wait a few minutes after deployment for the analytics to arrive for the first time before you're able to query the logs. This depends on the console logging implemented in your container app.
Use the following commands to view console log messages.
LOG_ANALYTICS_WORKSPACE_CLIENT_ID=`az containerapp env show --name $CONTAINERAPPS_ENVIRONMENT --resource-group $RESOURCE_GROUP --query properties.appLogsConfiguration.logAnalyticsConfiguration.customerId --out tsv`
az monitor log-analytics query \
--workspace $LOG_ANALYTICS_WORKSPACE_CLIENT_ID \
--analytics-query "ContainerAppConsoleLogs_CL | where ContainerAppName_s == $CONTAINER_APP_NAME | project ContainerAppName_s, Log_s, TimeGenerated" \
--out table
Clean up resources
If you're not going to continue to use this application, run the following command to delete the resource group along with all the resources created in this quickstart.
Caution
The following command deletes the specified resource group and all resources contained within it. If resources outside the scope of this quickstart exist in the specified resource group, they will also be deleted.
az group delete --name $RESOURCE_GROUP
Tip
Having issues? Let us know on GitHub by opening an issue in the Azure Container Apps repo.