Run a Container on App Service with Settings and Secrets
30-second answer
App Service can run a custom container image from Azure Container Registry, pulled with a managed identity that has AcrPull. Every app setting you add (az webapp config appsettings set) reaches the container as an environment variable, and WEBSITES_PORT tells App Service which port your app listens on. For secrets, make the setting a Key Vault reference, @Microsoft.KeyVault(SecretUri=...), so the value is read from Key Vault at run time and never stored in the image or the setting.
Exam objective: 1.1.3 Deploy containers to Azure App Service, including configuring App Service to supply environment variables and secrets. (Skills measured as of October 7, 2026)
Simulations
Feel it before you read it. No account needed, nothing leaves this page.
Open <strong>App Services</strong> and select <strong>+ Create → Web App</strong>.
👆 Click the glowing item in the portal to continue
Read the steps as text
- Open App Services and select + Create → Web App.
- Set Publish to Container and Linux. Then open the Container tab.
- Pick the registry, image and a version tag, not latest. The managed identity needs AcrPull on the registry.
- Review and Create.
- Go to Settings → Environment variables. Every app setting reaches the container as an environment variable. WEBSITES_PORT tells App Service which port your app listens on.
- Don’t paste the secret itself. A Key Vault reference keeps the value in Key Vault; the app’s identity reads it.
- The green Key vault source means the reference resolved. Select Apply; the app restarts with the new settings.
🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.
🖱️ Drag to rotate · scroll or pinch to zoom
App Service runs one container
App Service pulls the image and runs it. App settings become environment variables inside the container. A setting can be a Key Vault reference, so the secret never sits in the image or in the portal in plain text.
🤔 What happens if…
Read this tour as text
- Build an image, push it to the registry. Your Python API and everything it needs are packed into a container image. You push it to Azure Container Registry (ACR) as
ragapi:1.4.0. A tag names a version. A digest (sha256:…) names the exact bytes and never changes. - ACR Tasks build in the cloud. ACR Tasks build the image inside Azure, so you don’t need Docker on your laptop.
az acr buildis a quick task. A task can also run on every git commit or when the base image (saypython:3.12-slim) gets a security patch. - App Service runs one container. App Service pulls the image and runs it. App settings become environment variables inside the container. A setting can be a Key Vault reference, so the secret never sits in the image or in the portal in plain text.
- Container Apps environment. An Azure Container Apps environment is a secure boundary with its own virtual network and Log Analytics workspace. Several container apps share it. Ingress decides who can reach each app: external (internet) or internal (only the environment).
- Revisions and traffic splitting. Every change to the template (image, env vars, scale rules) creates a new revision, an immutable snapshot. In multiple revision mode you can send 80% of traffic to the old one and 20% to the new one, then roll forward or back.
- KEDA scales on events. Container Apps uses KEDA scale rules. A Service Bus rule adds replicas as the queue grows (for example one replica per 5 messages), up to max replicas. When the queue empties, it scales back to min replicas, which can be zero.
- AKS runs what the manifest says. On AKS you describe the app in YAML manifests: a Deployment (image, replicas, probes, resources) and a Service (a stable address).
kubectl apply -fsends them. Kubernetes keeps the real state equal to what you declared. - Troubleshoot: logs, events, connectivity. When it breaks, look in three places. Logs (
kubectl logs,az containerapp logs show) show what the app said. Events (kubectl describe,kubectl get events) show what the platform did. Connectivity checks (DNS, ports, ingress) show whether traffic can reach it.
Host the chat UI image on App Service. The first try fails twice, for two very common reasons: the app can't pull from your private registry, and it pings the wrong port. Fix both and read the log that proves it.
🎓 Education-only simulation. Your lab is saved in this browser, so you can leave and continue later.
😟 THE PROBLEM
The API key for your model is baked into the container image.
😊 THE SOLUTION
Run the image on App Service and feed it settings and Key Vault references.
- Secrets inside the image✓ Key Vault references
- Rebuild to change a setting✓ App settings as env vars
- Risky releases✓ Deployment slots
Start with a question
Your AI search key is in the image. What happens when you need to change it?
You rebuild the image, push it, redeploy, and hope nobody pulled the old one and kept it. Worse, anyone who can pull the image can read the key.
On App Service, configuration lives outside the image. You add app settings, they show up in your container as environment variables, and the secret ones can point at Azure Key Vault. Change a setting and the app restarts with the new value. No rebuild.
Explain it like I’m 10
The technical explanation
Create the web app from an ACR image
az appservice plan create --resource-group rg-ai --name plan-rag --is-linux --sku P0v3
az webapp create --resource-group rg-ai --plan plan-rag --name ragapi-web \
--container-image-name myreg.azurecr.io/ragapi:1.4.0 \
--assign-identity '[system]'
Let the app pull with its managed identity instead of a registry password:
PRINCIPAL=$(az webapp identity show -g rg-ai -n ragapi-web --query principalId -o tsv)
ACR_ID=$(az acr show -n myreg --query id -o tsv)
az role assignment create --assignee $PRINCIPAL --role AcrPull --scope $ACR_ID
az webapp config set -g rg-ai -n ragapi-web --generic-configurations '{"acrUseManagedIdentityCreds": true}'
To move to a new version later, run az webapp config container set -g rg-ai -n ragapi-web --container-image-name myreg.azurecr.io/ragapi:1.5.0.
Settings become environment variables
az webapp config appsettings set -g rg-ai -n ragapi-web --settings \
WEBSITES_PORT=8000 \
MODEL_DEPLOYMENT=gpt-4.1-mini \
SEARCH_ENDPOINT=https://srch-rag.search.windows.net
import os
deployment = os.environ["MODEL_DEPLOYMENT"] # set as an app setting
search_key = os.environ["SEARCH_KEY"] # a Key Vault reference, resolved for you
App Service tries to detect the port, but if your container listens on 8000, set WEBSITES_PORT=8000. Saving settings restarts the app. Connection strings also become environment variables, with a prefix such as SQLAZURECONNSTR_ or CUSTOMCONNSTR_.
Secrets: Key Vault references
- Turn on the app’s managed identity (done above with
--assign-identity). - Give it read access to secrets: the Key Vault Secrets User role (or a Get access policy).
- Set the app setting value to a reference:
az webapp config appsettings set -g rg-ai -n ragapi-web --settings \
SEARCH_KEY="@Microsoft.KeyVault(SecretUri=https://kv-rag.vault.azure.net/secrets/search-key/)"
# the other accepted form:
# @Microsoft.KeyVault(VaultName=kv-rag;SecretName=search-key)
Leave the version off the URI and App Service uses the latest version, refreshing it within 24 hours (or immediately when the app’s config changes). Put a version on it and the value stays pinned.
-
1 Pull
App Service pulls ragapi:1.4.0 from ACR with its managed identity.
-
2 Resolve
Key Vault references are resolved with the same identity.
-
3 Inject
All app settings are set as environment variables.
-
4 Ping
App Service checks the container answers on WEBSITES_PORT.
Architect level Troubleshooting a reference that won't resolve
In the portal, the Environment variables page shows a status next to each Key Vault reference. A red status usually means the identity has no access, the vault’s firewall blocks App Service (allow trusted services, or use VNet integration and a private endpoint), or the secret name is wrong. If the app uses a user-assigned identity, set keyVaultReferenceIdentity on the site so App Service knows which identity to use. For container logs, turn on logging with az webapp log config --docker-container-logging filesystem and stream them with az webapp log tail.
Real-world business scenario
Woodgrove Bank (fictional)
- The problem
- A customer-support chat API ran in a container with the Azure AI Search key in a .env file baked into the image. A security review failed it.
- What they used
- App Service for Linux pulling from ACR with a system-assigned managed identity. Non-secret values are app settings; the search key is a Key Vault reference to a secret the identity can read.
- Why that choice
- The image holds no secrets, rotation happens in Key Vault, and the same image moves from staging to production slots with slot-specific settings.
- The result
- The review passed, and the team rotates keys without a rebuild.
When to use it and when not to
✓ Use it when…
- One containerised web app or API with steady traffic.
- You want deployment slots, custom domains and TLS with little setup.
- Secrets should live in Key Vault but reach the app as environment variables.
✕ Think twice when…
- Event-driven workers that should scale to zero on queue length.
- Many cooperating microservices that need service-to-service discovery.
↔ Alternatives to consider
- Azure Container Apps for scale to zero, KEDA rules and traffic splitting.
- Azure App Configuration when many apps share the same settings and feature flags.
Test yourself with exercises
5 original practice questions in the AI-200 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.
Interview preparation
Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.
Beginner How does your Python code inside the container read an App Service app setting?
Model answer
As a normal environment variable. If I add MODEL_DEPLOYMENT in the portal or with az webapp config appsettings set, my code reads os.environ["MODEL_DEPLOYMENT"]. App Service injects it when it starts the container.
Intermediate Your container listens on 8000 and App Service keeps reporting that it didn't respond to pings. What's wrong?
Model answer
App Service doesn't know the port. I'd set the WEBSITES_PORT app setting to 8000. The container also has to bind to 0.0.0.0, not localhost.
Experienced developer How do you get a Key Vault secret into the container without putting it in the app setting?
Model answer
I turn on the web app's managed identity, give it the Key Vault Secrets User role on the vault, and set the app setting value to @Microsoft.KeyVault(SecretUri=https://kv-rag.vault.azure.net/secrets/search-key/). The container still sees a normal environment variable with the real value, but the setting only holds the reference.
Interviewer follows up
What happens when you rotate the secret?
You
If the reference has no version, App Service picks up the new version within 24 hours, or straight away if I change any app setting, which restarts the app.
Scenario The web app can't pull its image from ACR after someone disabled the admin account. How do you fix it properly?
Model answer
Switch the pull to the managed identity. Assign AcrPull on the registry to the app's identity and set acrUseManagedIdentityCreds to true in the site config. No registry password is stored anywhere after that.
Quick revision
Key points
- App Service runs one custom container image per app from ACR or another registry.
- App settings become environment variables inside the container; changing them restarts the app.
- Set WEBSITES_PORT when the container listens on a port other than the one App Service detects.
- Key Vault references use @Microsoft.KeyVault(SecretUri=...) or @Microsoft.KeyVault(VaultName=...;SecretName=...).
- The app's managed identity needs AcrPull on the registry and secret read access on the vault.
Important terminology
- App setting
- A name-value pair on a web app that App Service injects into the container as an environment variable.
- WEBSITES_PORT
- The app setting that tells App Service which port the custom container listens on.
- Key Vault reference
- An app setting value in the form @Microsoft.KeyVault(...) that App Service resolves from Azure Key Vault.
- Managed identity
- An identity in Microsoft Entra ID that Azure gives the app, used to pull images and read secrets without passwords.
- Deployment slot setting
- An app setting marked to stay with its slot instead of moving during a swap.
Common mistakes
- Baking API keys into the image or committing them in a .env file.
- Forgetting to give the managed identity access to Key Vault, so the reference fails to resolve.
Exam tips
- "Supply configuration to the container without rebuilding" = app settings (environment variables).
- "Store the secret in Key Vault and expose it as an environment variable" = Key Vault reference plus managed identity.
- "Container listens on a non-default port" = WEBSITES_PORT.
Five quick revision questions
-
1.How do app settings reach a container on App Service?Show
As environment variables.
-
2.Which command adds app settings from the CLI?Show
az webapp config appsettings set
-
3.What is the format of a Key Vault reference?Show
@Microsoft.KeyVault(SecretUri=https://kv-rag.vault.azure.net/secrets/search-key/)
-
4.Which role lets the web app pull from ACR?Show
AcrPull.
-
5.Which app setting sets the container port?Show
WEBSITES_PORT
Official references
Finished this lesson? Tick it off. Your progress is saved in this browser.
Next up: Azure Container Apps: Environments and Revisions
Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges.