How To Set Up Volumes for Azure Container Apps (step-by-step)

No Comments
Published: 18.12.2022

Are you looking for a way to set up volumes for your Azure Container Apps? Look no further! In this blog post, we will explain the different types of container app storage and provide step-by-step instructions on how to set up volumes using the CLI. By the end of this tutorial, you will have a container app volume connected to an Azure Storage account. So, let’s get started!

Different types of Azure Container App Storage and Volumes

Azure Container Apps allow developers to run and manage containers inside Azure. Container apps support different types of storage, including the container file system and Azure Files. The container file system is temporary storage scoped to the local container and is removed with the container. Azure Files can be used as permanent storage, also called volumes. Azure Files can be used for writing files to a file share and to make data accessible by other systems and containers.

Set up Volumes for Azure Container Apps

Requirements for the following steps are that you already have a running container app that you want to set up with volumes.

To set up volumes for azure container apps, you have to work mostly with the CLI, but you also need to do one step in your app’s configuration file. Follow these steps to set it up correctly:

  1. Define the following variables inside your CLI:
RESOURCE_GROUP={resource-group} #the resource group containing you container app
ENVIRONMENT_NAME={ca-env-name} #container app environment name
STORAGE_ACCOUNT_NAME={storage-name} #name of the storage in your resource group
LOCATION={location} #location for the storage account (ex. canadacentral)
STORAGE_SHARE_NAME={share-name} #the name of the share inside the storage
STORAGE_MOUNT_NAME={mount-name} #the mount name inside the container app
  1. Create the storage account:
az storage account create \
  --resource-group $RESOURCE_GROUP \
  --name $STORAGE_ACCOUNT_NAME \
  --location "$LOCATION" \
  --kind StorageV2 \
  --sku Standard_LRS \
  --enable-large-file-share \
  --query provisioningState
  1. Create a share inside the storage account:
az storage share-rm create --resource-group $RESOURCE_GROUP --storage-account $STORAGE_ACCOUNT_NAME --name $STORAGE_SHARE_NAME --quota 1024 --enabled-protocols SMB --output table 
  1. Get the account key for the next step:
STORAGE_ACCOUNT_KEY=`az storage account keys list -n $STORAGE_ACCOUNT_NAME --query "[0].value" -o tsv` 
  1. Create a mount inside the container app:
az containerapp env storage set --access-mode ReadWrite --azure-file-account-name $STORAGE_ACCOUNT_NAME --azure-file-account-key $STORAGE_ACCOUNT_KEY --azure-file-share-name $STORAGE_SHARE_NAME --storage-name $STORAGE_MOUNT_NAME --name $ENVIRONMENT_NAME --resource-group $RESOURCE_GROUP --output table 
  1. Download the data to add the volume and mount:
az containerapp show --name $CONTAINER_APP_NAME --resource-group $RESOURCE_GROUP --output yaml > app.yaml
  1. Add a reference to the storage volume in the template section:
template:
  volumes:
  - name: {my-volume}
    storageName: {$STORAGE_MOUNT_NAME}
    storageType: AzureFile
  1. Add a volumeMounts section to your container:
containers:
  - image: ...
    name: ...
    volumeMounts:
    - volumeName: {my-volume}
      mountPath: {absolute/path/to/volume/location}
  1. Upload the data to update the container app:
az containerapp update --name $CONTAINER_APP_NAME --resource-group $RESOURCE_GROUP --yaml app.yaml --output table

With that, we created a volume connected to an azure storage account for our container app!

Conclusion

In conclusion, setting up volumes for your Azure Container Apps is not as straightforward as you would wish. But by following the steps outlined in this blog post, you can smoothly connect your container app to an Azure Storage account and take advantage of its benefits.

Need help or want to share feedback? Join my discord community!

If you found this tutorial helpful, consider subscribing to my monthly newsletter, where I inform subscribers of new blog posts and personal projects. Additionally, if you have a question feel free to contact me via chat, comments, or mail@programonaut.com. Thanks for reading!

[convertkit form=2303042]

KOFI Logo

If this guide is helpful to you and you like what I do, please support me with a coffee!

Discussion (0)