Azure App Service in Container mode overrides environment variables for the running container with a significant delay

Pavel Makul 0 Reputation points
2024-10-03T21:56:24.6066667+00:00

I deploy my web app to Azure App Service using the Container Publishing model. The App Service Plan is configured with Linux OS and Auto Scale rules, and Azure App Service is set up with Deployment Slots.

I have a Dockerfile with predefined environment variables and default values. At the same time, on the Azure App Service side, I define valid environment variables and connection strings.

When I deploy the container to Azure App Service, it does not immediately override the environment variables in the container (defined in the Dockerfile) with the valid environment variables and connection strings from Azure App Service. The override only happens after some time. This doesn't break my web app during the deployment step because I have a Health Check in my CI/CD pipelines, which, for example, uses the database connection string from the environment variables and checks for database dependency (and yes, this check fails 4 or 5 times until Azure App Service overrides all environment variables with valid values). Typically, 10 attempts are enough for deployment.

However, when my web app is under load and the Auto Scale rules execute, many errors occur during the startup of new instances because the environment variables are not overridden in time before startup. As a result, my app crashes under load.

How can I fix this issue?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,757 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 16,346 Reputation points Microsoft Employee
    2024-10-07T03:12:00.6966667+00:00

    @Pavel Makul The issue you are facing is likely due to the way that Azure App Service handles environment variables in container mode. By default, Azure App Service sets environment variables for the container at startup time, which can cause delays in the environment variables being updated.

    To fix this issue, you can try setting the environment variables for your container using the Azure App Service Application Settings. This will ensure that the environment variables are set before the container starts up, which should prevent delays in the environment variables being updated.

    To set the environment variables using the Azure App Service Application Settings, follow these steps:

    1. Go to the Azure portal and navigate to your App Service.
    2. Click on the "Configuration" tab.
    3. Under "Application Settings", click on "New application setting".
    4. Enter the name and value of the environment variable you want to set.
    5. Click on "OK" to save the environment variable.

    Repeat these steps for each environment variable you want to set.

    Once you have set the environment variables using the Azure App Service Application Settings, you can remove the environment variables from your Dockerfile. This will ensure that the environment variables are set correctly at startup time.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.