Monday, August 28, 2023

How to run a Python Docker Container on Azure Function


# How to upgrade Docker Container backed Azure Function

If you are using Azure Container registry to host your Azure Function then utilize these instructions to Upgrade them to Functions Runtime V4.

# Setup codebase

In hosts.json ensure this:
```
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.3.0, 4.0.0)"
  }
```

# Upgrade Production

### Upgrade The Extension Version
If all you are doing is the Production Slot This section all you need to do: (If you are upgrading a Slot set this value on your Slot and see below for more stuff you have to set):  

`FUNCTIONS_EXTENSION_VERSION=~4`

### Upgrade The Linux Version
When not working with a Container, instructions suggest to do this (don't do it):
~~`linux-fx-version=DOCKER|"Python|3.9"`~~

**But** when running a Docker Container **Set** this value (borrow the value from the URL of the Docker image in your current `Dockerfile`:

`linux-fx-version=DOCKER|mcr.microsoft.com/azure-functions/python:4-python3.10`

# Upgrade Slots. Not total upgrade.
You will have extra work to do if you are upgrading only one slot.

Set this value on the Production Slot:
`WEBSITE_OVERRIDE_STICKY_EXTENSION_VERSIONS=0`

Repeat for the Staging Slot:
`WEBSITE_OVERRIDE_STICKY_EXTENSION_VERSIONS=0`

# *Important*: Set Web Jobs Feature Flags.
If you are using Functions v2 syntax with an `function_app.py` and no `functions.json` files like you should be. You need to set the following value on the container. **If you don't** the Container won't be recognized and your Function App will just display a splash screen. 

This can be done through a new Configuration setting on the slot you are upgrading:
`AzureWebJobsFeatureFlags=EnableWorkerIndexing`

If you don't trust that this will work, some have put them in their `Dockerfile`'s `ENV` section like so:
```
ENV  
AzureWebJobsScriptRoot=/home/site/wwwroot  \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true  \
AzureWebJobsFeatureFlags=EnableWorkerIndexing  \
AzureWebJobsStorage=UseDevelopmentStorage=true
```

This one may not be needed:
`AzureWebJobsStorage=UseDevelopmentStorage=true`