Azure-docs: "401 Unauthorized" when testing using a local Docker container

Created on 5 Sep 2019  ·  13Comments  ·  Source: MicrosoftDocs/azure-docs

Browser is replying with "401 Unauthorized" when testing using a local Docker container. Any ideas why?

>docker run -p 8080:80 -it ptuton/mydockerimage:v1.0.0
info: Host.Startup[503]
      Initializing Host.
info: Host.Startup[504]
      Host initialization: ConsecutiveErrors=0, StartupCount=1
info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
      LoggerFilterOptions
      {
        "MinLevel": "None",
        "Rules": [
          {
            "ProviderName": null,
            "CategoryName": null,
            "LogLevel": null,
            "Filter": "<AddFilter>b__0"
          },
          {
            "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
            "CategoryName": null,
            "LogLevel": "None",
            "Filter": null
          },
          {
            "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
            "CategoryName": null,
            "LogLevel": null,
            "Filter": "<AddFilter>b__0"
          }
        ]
      }
info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
      FunctionResultAggregatorOptions
      {
        "BatchSize": 1000,
        "FlushTimeout": "00:00:30",
        "IsEnabled": true
      }
info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
      SingletonOptions
      {
        "LockPeriod": "00:00:15",
        "ListenerLockPeriod": "00:01:00",
        "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
        "LockAcquisitionPollingInterval": "00:00:05",
        "ListenerLockRecoveryPollingInterval": "00:01:00"
      }
info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
      HttpOptions
      {
        "DynamicThrottlesEnabled": false,
        "MaxConcurrentRequests": -1,
        "MaxOutstandingRequests": -1,
        "RoutePrefix": "api"
      }
info: Microsoft.Azure.WebJobs.Hosting.JobHostService[0]
      Starting JobHost
info: Host.Startup[401]
      Starting Host (HostId=5bd7af83b185-2137340777, InstanceId=373f45ea-e9cf-4853-8dc3-1a3b3822d2e9, Version=2.0.12641.0, ProcessId=1, AppDomainId=1, InDebugMode=False, InDiagnosticMode=False, FunctionsExtensionVersion=)
info: Host.Startup[314]
      Loading functions metadata
info: Host.Startup[315]
      1 functions loaded
info: Host.Startup[0]
      Generating 1 job function(s)
info: Host.Startup[0]
      Found the following functions:
      Host.Functions.MyHttpTrigger

info: Host.Startup[412]
      Host initialized (173ms)
info: Host.Startup[413]
      Host started (187ms)
info: Host.Startup[0]
      Job host started
Hosting environment: Production
Content root path: /
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
info: Host.General[316]
      Host lock lease acquired by instance ID '000000000000000000000000A28956B2'.

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri2 assigned-to-author azure-functionsvc product-question triaged

Most helpful comment

@ptuton yep, I'm fixing that now. So far, you get the award for finding the best doc bug of the day 😁
Here's the note I added:
image

All 13 comments

Hi @ptuton - Thank you for your feedback! We will review and update as appropriate.

@ptuton Can you try changing the AuthorizationLevel to Anonymous as shown below :

 [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,

by default the AuthorizationLevel will be Anonymous

@ptuton you shouldn't need a function key to access an HTTP trigger when running locally in the Core Tools command prompt. Are you trying to run locally inside your docker container, or just using Core Tools?

@ggailey777 from the logs in the OP it seems @ptuton is try running function inside the docker container.

Thanks @KetanChawda-MSFT that was my guess. Unfortunately, running in the docker container is like running in Azure, and a function key is required. Since the function app doesn't exist yet, there's no key to provide at this point. Rather than have folks create an anonymous endpoint just to run locally in the container, I'll add a note that spells this out more clearly.

Also, @ptuton's title is pretty clear about that 😉

Additionally, it's worth noting that I'm following the instructions under "Test the image locally", which doesn't work when attempting to "test your function again, this time in the local container using the following URL: http://localhost:8080/api/myhttptrigger?name=<yourname>"

@ptuton yep, I'm fixing that now. So far, you get the award for finding the best doc bug of the day 😁
Here's the note I added:
image

How does the program get the function key when it's running on Azure? Through an environment variable or network call? One would think that it can be simulated in a non-Azure environment, i.e. running locally using Docker for example.

So the answer is to update the function access to be anonymous so I can forget I changed that when it's time to push to prod?

There's another limitation with running a function in a local Docker container. The local.settings.json file is not referenced hence any Application Settings, that had been modified for local use, will not be used as they are when running a local host. Instead the setting will be looked for in the Docker container, and will fail as the container doesn't appear to implement the application settings mechanism (I'm using os.environ['setting name'] in Python in my case). You would have to set those environment variables explicitly in the Docker image with the ENV statement.

@flyboyjon Yea you have to set those environment variables on your docker command/file. I supposed you could of course just copy that file into the container as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DeepPuddles picture DeepPuddles  ·  3Comments

varma31 picture varma31  ·  3Comments

Agazoth picture Agazoth  ·  3Comments

JeffLoo-ong picture JeffLoo-ong  ·  3Comments

bityob picture bityob  ·  3Comments