Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 4 of 28
❮Prev Next❯

Run a Container on App Service with Settings and Secrets

Module 1: Containers ⏱ 9 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    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.

    Simulate it » No Azure account needed

    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.

    Azure portal: Run an ACR image on App Service Create a web app from an ACR image and add environment variables.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    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
    1. Open App Services and select + Create → Web App.
    2. Set Publish to Container and Linux. Then open the Container tab.
    3. Pick the registry, image and a version tag, not latest. The managed identity needs AcrPull on the registry.
    4. Review and Create.
    5. 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.
    6. Don’t paste the secret itself. A Key Vault reference keeps the value in Key Vault; the app’s identity reads it.
    7. 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.

    😟 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
    🛠️ Solved with: App Service app settings

    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

    1. Turn on the app’s managed identity (done above with --assign-identity).
    2. Give it read access to secrets: the Key Vault Secrets User role (or a Get access policy).
    3. 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. 1 Pull

      App Service pulls ragapi:1.4.0 from ACR with its managed identity.

    2. 2 Resolve

      Key Vault references are resolved with the same identity.

    3. 3 Inject

      All app settings are set as environment variables.

    4. 4 Ping

      App Service checks the container answers on WEBSITES_PORT.

    What happens when the container starts
    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

    Banking

    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.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    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.

    Question 1

    Question 1 of 5

    You deploy a custom container to Azure App Service. The application reads its model deployment name from an environment variable named MODEL_DEPLOYMENT. You must set the value without rebuilding the image. What should you do?

    Question 2

    Question 2 of 5

    A web app runs a custom container that listens on port 8000. App Service reports that the container did not respond to HTTP pings. Which app setting should you configure?

    Question 3

    Question 3 of 5 · Select 2

    A containerized web app on App Service must read an API key that is stored in Azure Key Vault. The key must be exposed to the application as an environment variable, and it must not be stored in the app configuration. Which TWO actions should you perform?

    Question 4

    Question 4 of 5 · Select 2

    A web app pulls its container image from Azure Container Registry. The registry admin account has been disabled. You need the web app to pull images without stored credentials. Which TWO actions should you perform?

    Question 5

    Question 5 of 5

    A Key Vault reference in an app setting omits the secret version. A new version of the secret is created in Key Vault. What is the behavior of App Service?

    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. 1.How do app settings reach a container on App Service?Show

      As environment variables.

    2. 2.Which command adds app settings from the CLI?Show

      az webapp config appsettings set

    3. 3.What is the format of a Key Vault reference?Show

      @Microsoft.KeyVault(SecretUri=https://kv-rag.vault.azure.net/secrets/search-key/)

    4. 4.Which role lets the web app pull from ACR?Show

      AcrPull.

    5. 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

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.