How to use powershell and the Azure CLI to create an Azure container app with storage.
Login
az login
Note A browser window will open allowing you to enter your credentials
Switch Subscription if you have multiple
az account list
az account set --subscription "VS Subscription"
az account show
List all the storage accounts in the subscription
az storage account list
List all resource groups
az group list
Creating a new azure files mount for an azure container app
Alot of the folloing comes from: Tutorial: Create an Azure Files storage mount in Azure Container Apps | Microsoft Learn
Set local environment variables to use for rest of process
export RESOURCE_GROUP="containerAppRG"
export ENVIRONMENT_NAME="my-app-environment"
export LOCATION="eastus2"
Tip to check the environment variable is set correctly
printenv {VariableName}
Ensure you have the latest Container Apps extension
az extension add -n containerapp --upgrade
Register the Microsoft.App namespace
az provider register --namespace Microsoft.App
Register the OperationalInsights if you haven’t used it before
az provider register --namespace Microsoft.OperationalInsights
Create the resource group
az group create \
--name $RESOURCE_GROUP \
--location $LOCATION \
--query "properties.provisioningState"
Create the Container App Environment
az containerapp env create \
--name $ENVIRONMENT_NAME \
--resource-group $RESOURCE_GROUP \
--location "$LOCATION" \
--query "properties.provisioningState"
In Progress:

It displays this, but if it doesn’t wait until the resource is created to go back to the prompt, you can check with
az containerapp env show -n container-app-environment -g containerAppRG
Check with the command:
![[Pasted-image-20221030072033.png]](/azure-container-app-powershell/Pasted-image-20221030072033.png)
Setting up the storage account
Set another environment variable
export STORAGE_ACCOUNT_NAME="myacastorageaccount$RANDOM"
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
Define a file share name
export STORAGE_SHARE_NAME="myfileshare"
# export STORAGE_SHARE_NAME="shareone"
Create the storage file share
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
Get the storage account key
export STORAGE_ACCOUNT_KEY=`az storage account keys list -n $STORAGE_ACCOUNT_NAME --query "[0].value" -o tsv`
# printenv STORAGE_ACCOUNT_KEY
Set the storage mount name
Note This value is the name used to define the storage mount link from your Container Apps environment to your Azure Storage account.
export STORAGE_MOUNT_NAME="mystoragemount"
Create the storage mount
Create the storage link in the environment
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
# currently do not know where this would be visible in the portal
This command creates a link between container app environment and the file share created with the az storage share-rm command.
Now that the storage account and environment are linked, you can create a container app that uses the storage mount.
Define the container app name
export CONTAINER_APP_NAME="my-container-app"
Create the container app
az containerapp create \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP \
--environment $ENVIRONMENT_NAME \
--image nginx \
--min-replicas 1 \
--max-replicas 1 \
--target-port 80 \
--ingress external \
--query properties.configuration.ingress.fqdn
This command displays the URL of your new container app.
Once the page loads, you’ll see the “Welcome to nginx!” message. Keep this browser tab open. You’ll return to the website during the storage mount verification steps.
Now that you’ve confirmed the container app is configured, you can update the app to with a storage mount definition.
Export the container app’s configuration
az containerapp show \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP \
--output yaml > app.yaml
Note While this application doesn’t have secrets, many apps do feature secrets. By default, when you export an app’s configuration, the values for secrets aren’t included in the generated YAML.
If you don’t need to change secret values, then you can remove the
secretssection and your secrets remain unaltered. Alternatively, if you need to change a secret’s value, make sure to provide both thenameandvaluefor all secrets in the file before attempting to update the app. Omitting a secret from thesecretssection deletes the secret.
Open the yaml in a code editor
nano app.yaml
# or
code app.yaml
Add a reference to the storage volumes to the template definition.
template:
volumes:
- name: my-azure-file-volume
storageName: mystoragemount
storageType: AzureFile
Add a volumeMounts section to the nginx container in the containers section
containers:
- image: nginx
name: my-container-app
volumeMounts:
- volumeName: my-azure-file-volume
mountPath: /var/log/nginx
Update the container app with the new storage mount configuration.
az containerapp update \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP \
--yaml app.yaml \
--output table
Verify the storage mount
Open an interactive shell inside the container app to execute commands inside the running container
# export CONTAINER_APP_NAME="my-container-app"
# export RESOURCE_GROUP="containerAppRG"
az containerapp exec \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP
This command may take a moment to open the remote shell. Once the shell is ready, you can interact with the storage mount via file system commands.
Files
Files located here are stored in the share, not in the container
cd /var/log/nginx
Cleanup the resource group if this was temporary or testing
export RESOURCE_GROUP="containerAppRG"
az group delete \
--name $RESOURCE_GROUP
Final Summary
While this was fun, I learned quite literally the day after about Azure Bicep which seems to simplify the configuration declaration. I am looking forward to doing this same process but with Bicep in the near future.