You’ve deployed an Azure Function using Docker to production, everything looks good, but when you try to call your function endpoint, you get a frustrating 401 Unauthorized error:
curl -X POST \ "https://my-prod-functions.azurewebsites.net/api/MyFunction?code=my-function-key" \ -H "Content-Type: application/json" # Response: 401 Unauthorized
Meanwhile, your non-dockerized development environment works perfectly with the exact same code and keys. What’s going on?
First, I checked all the usual suspects:
AuthorizationLevel.Function in my code.Application Insights revealed something interesting:
Request successfully matched the route with name 'MyFunction' and template 'api/MyFunction' Executing StatusCodeResult, setting HTTP status code 401
The request was reaching the function and matching the route, but Azure was returning 401 before the code even executed. This meant the issue was at the Azure Functions runtime authentication layer, not in my code.
I inspected the environment variables via Kudu console (https://my-functions.scm.azurewebsites.net) and found:
AzureWebJobsSecretStorageType = files WEBSITES_ENABLE_APP_SERVICE_STORAGE = false # I THOUGHT THIS IS THE PROBLEM!
Here’s what was happening:
Azure Functions can store authentication keys in two ways:
AzureWebJobsSecretStorageType = files)/home/data/Functions/secrets/AzureWebJobsSecretStorageType = blob)When you set WEBSITES_ENABLE_APP_SERVICE_STORAGE = false (common for stateless Docker containers), Azure does not mount persistent storage to your container.
This means:
Let me verify this in the container:
# SSH into container or via Kudu ls -la /home/data/ # Result: No such file or directory ls -la /azure-functions-host/Secrets/ #Result: No such file or directory
The secrets directory didn’t exist because storage wasn’t mounted!
When I initially tried to fix this by setting WEBSITES_ENABLE_APP_SERVICE_STORAGE = true, authentication worked but I got 404 Not Found instead!
\ Why? Because mounting Azure’s persistent storage at /home/site/wwwroot/ overwrote my Docker container’s application files. The mounted directory only had host.json but no compiled DLLs (in the docker container, not the host container it is very important to keep that in mind there are two containers here the host container and the app docker container) so Azure Functions runtime finds 0 functions to load so When a request comes in Authentication works (keys in blob) but Function not found so we get 404.In short, this option makes the host find the function key files (so authentication works) but loses the functions themselves. We cant use it and WEBSITES_ENABLE_APP_SERVICE_STORAGE needs to be false.
The correct fix for Dockerized Azure Functions is to use blob-based secret storage:
In Azure Portal:
AzureWebJobsSecretStorageTypeblobWEBSITES_ENABLE_APP_SERVICE_STORAGE is falseClick Save, then restart the function app. Azure will automatically:
azure-webjobs-secrets container in your storage accountGo to Portal → Function App → Functions → [Your Function] → Function Keys
Copy the key from the Portal (this is the decrypted version).
curl -X POST \ "https://my-prod-functions.azurewebsites.net/api/MyFunction?code=<KEY_FROM_PORTAL>" \ -H "Content-Type: application/json" \ -d '{"test": "data"}' # Response: 200 OK
Understanding Key Encryption
When you check blob storage, you’ll see keys stored like this:
{ "keys": [ { "name": "default", "value": "CfDJ8AAAAAAA...encrypted-value...", "encrypted": true } ] }
Important: You cannot use this encrypted value directly! Azure automatically:
Always get your keys from the Azure Portal UI, which shows the decrypted version.
FROM mcr.microsoft.com/azure-functions/dotnet:4 AS base WORKDIR /home/site/wwwroot EXPOSE 80 FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src COPY ["MyFunction/MyFunction.csproj", "MyFunction/"] RUN dotnet restore "MyFunction/MyFunction.csproj" COPY . . WORKDIR "/src/MyFunction" RUN dotnet build "MyFunction.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "MyFunction.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /home/site/wwwroot COPY --from=publish /app/publish . ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ AzureFunctionsJobHost__Logging__Console__IsEnabled=true
\
# Secret storage configuration AzureWebJobsSecretStorageType = blob AzureWebJobsStorage = <your-storage-connection-string> # Docker configuration WEBSITES_ENABLE_APP_SERVICE_STORAGE = false WEBSITE_RUN_FROM_PACKAGE = 0 # Functions runtime FUNCTIONS_WORKER_RUNTIME = dotnet FUNCTIONS_EXTENSION_VERSION = ~4
This issue is specific to Docker + Azure Functions because:
Non-dockerized function apps don’t have this problem because they naturally have access to the App Service file system.
Problem: Dockerized Azure Functions return 401 when using file-based secret storage without mounted volumes.
Solution: Use blob-based secret storage, which is stateless and Docker-friendly.
Key Settings:
AzureWebJobsSecretStorageType = blob WEBSITESENABLEAPPSERVICESTORAGE = false
This configuration allows your Docker container to remain stateless while still securely accessing authentication keys from Azure Blob Storage.
Troubleshooting a similar issue? Feel free to reach out in the comments below!


