Skip to content

Arm64 support for Azure Functions Host #1164

@paldaniel

Description

@paldaniel

I have tried to run Azure Functions (host: 4.3.2, python 3.9) as an Azure IoT Edge module on Raspberry PI 4 (arm64) and had some success. I wanna share my steps to have official support for arm64 architecture.
Python image

  1. Change runtime-image architecture
FROM mcr.microsoft.com/dotnet/sdk:6.0.100-bullseye-slim-arm64v8 AS runtime-image
  1. Rebuild host to arm64, remove workers (could not build them to work on arm64 correctly)
RUN BUILD_NUMBER=$(echo ${HOST_VERSION} | cut -d'.' -f 3) && \
    git clone --branch v${HOST_VERSION} https://github.com/Azure/azure-functions-host /src/azure-functions-host && \
    cd /src/azure-functions-host && \
    HOST_COMMIT=$(git rev-list -1 HEAD) && \
    dotnet publish -v q /p:BuildNumber=$BUILD_NUMBER /p:CommitHash=$HOST_COMMIT src/WebJobs.Script.WebHost/WebJobs.Script.WebHost.csproj -c Release --output /azure-functions-host --runtime linux-arm64 --self-contained && \
    rm -rf /root/.local /root/.nuget /src /azure-functions-host/workers
  1. Rebuild extension bundles
    This worked only with full rebuild on my pc (amd64) to arm64
    For both 2.12.1, 3.9.1
dotnet add package Microsoft.Azure.WebJobs
dotnet build --output ./bin_v3/linux-arm64 --runtime linux-arm64
  1. Change python to arm64
FROM arm64v8/python:3.9.12-slim-bullseye
  1. Remove MSSQL support (could not build it to arm64)
RUN apt-get update && \
    apt-get install -y wget && \
    echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
    apt-get update && \
    apt-get install -y apt-transport-https curl gnupg && \
    # MSSQL dependencies
    # curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
    # curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list && \
    # # Needed for libss1.0.0 and in turn MS SQL
    # echo 'deb http://security.debian.org/debian-security jessie/updates main' >> /etc/apt/sources.list && \
    # # install necessary locales for MS SQL
    # apt-get update && apt-get install -y locales && \
    # echo 'en_US.UTF-8 UTF-8' > /etc/locale.gen && \
    # locale-gen && \
    # # install MS SQL related packages
    # apt-get update && \
    # apt-get install -y unixodbc msodbcsql18 mssql-tools && \
    # .NET Core dependencies
    apt-get install -y --no-install-recommends ca-certificates \
    libc6 libgcc1 libgssapi-krb5-2 libicu67 libssl1.1 libstdc++6 zlib1g && \
    rm -rf /var/lib/apt/lists/* && \
    # Custom dependencies:
    #  OpenCV dependencies:
    apt-get update && \
    apt-get install -y libglib2.0-0 libsm6 libxext6 libxrender-dev && \
    #  binutils
    apt-get install -y binutils && \
    #  OpenMP dependencies
    apt-get install -y libgomp1 && \
    # mysql dependencies
    apt-get install -y default-libmysqlclient-dev
  1. Rebuild Azure Functions Python Worker and add required files:
COPY --from=runtime-image ["/azure-functions-host", "/azure-functions-host"]
COPY --from=runtime-image ["/FuncExtensionBundles", "/FuncExtensionBundles" ]

RUN mkdir -p /azure-functions-host/workers/python/3.9/LINUX/Arm64 &&\
    apt install -y git &&\
    pip install grpcio-tools &&\
    pip install git+https://github.com/Azure/azure-functions-python-worker#egg=azure-functions-worker --target=/azure-functions-host/workers/python/3.9/LINUX/Arm64 &&\
    apt purge git -y &&\
    pip uninstall -y grpcio-tools

COPY ["worker.py", "/azure-functions-host/workers/python/3.9/LINUX/Arm64"]
COPY ["worker.config.json", "/azure-functions-host/workers/python" ]
COPY ["start.sh", "/azure-functions-host" ]
COPY ["sshd_config", "/etc/ssh/"]

worker.py:

import os
import sys

from pathlib import Path

# User packages
PKGS_PATH = "site/wwwroot/.python_packages"
VENV_PKGS_PATH = "site/wwwroot/worker_venv"

PKGS = "lib/site-packages"

# Azure environment variables
AZURE_WEBSITE_INSTANCE_ID = "WEBSITE_INSTANCE_ID"
AZURE_CONTAINER_NAME = "CONTAINER_NAME"
AZURE_WEBJOBS_SCRIPT_ROOT = "AzureWebJobsScriptRoot"


def is_azure_environment():
    """Check if the function app is running on the cloud"""
    return (AZURE_CONTAINER_NAME in os.environ
            or AZURE_WEBSITE_INSTANCE_ID in os.environ)


def add_script_root_to_sys_path():
    """Append function project root to module finding sys.path"""
    functions_script_root = os.getenv(AZURE_WEBJOBS_SCRIPT_ROOT)
    if functions_script_root is not None:
        sys.path.append(functions_script_root)


def determine_user_pkg_paths():
    """This finds the user packages when function apps are running on the cloud

    For Python 3.7+, we only accept:
        /home/site/wwwroot/.python_packages/lib/site-packages
    """
    minor_version = sys.version_info[1]

    home = Path.home()
    pkgs_path = os.path.join(home, PKGS_PATH)
    usr_packages_path = []

    if minor_version in (7, 8, 9, 10):
        usr_packages_path.append(os.path.join(pkgs_path, PKGS))
    else:
        raise RuntimeError(f'Unsupported Python version: 3.{minor_version}')

    return usr_packages_path


if __name__ == '__main__':
    # worker.py lives in the same directory as azure_functions_worker
    func_worker_dir = str(Path(__file__).absolute().parent)
    env = os.environ

    if is_azure_environment():
        user_pkg_paths = determine_user_pkg_paths()
        joined_pkg_paths = os.pathsep.join(user_pkg_paths)

        # On cloud, we prioritize third-party user packages
        # over worker packages in PYTHONPATH
        env['PYTHONPATH'] = f'{joined_pkg_paths}:{func_worker_dir}'
        os.execve(sys.executable,
                  [sys.executable, '-m', 'azure_functions_worker']
                  + sys.argv[1:],
                  env)
    else:
        # On local development, we prioritize worker packages over
        # third-party user packages (in .venv)
        sys.path.insert(1, func_worker_dir)
        add_script_root_to_sys_path()
        from azure_functions_worker import main

        main.main()

worker.config.json

{
    "description":{
        "language":"python",
        "defaultRuntimeVersion":"3.9",
        "supportedOperatingSystems":["LINUX", "OSX", "WINDOWS"],
        "supportedRuntimeVersions":["3.7", "3.8", "3.9", "3.10"],
        "supportedArchitectures":["X64", "X86", "Arm64"],
        "extensions":[".py"],
        "defaultExecutablePath":"python",
        "defaultWorkerPath":"%FUNCTIONS_WORKER_RUNTIME_VERSION%/{os}/{architecture}/worker.py"
    }
}
  1. Based on requirements I have used the start.sh entrypoint
ENTRYPOINT ["/azure-functions-host/start.sh"]

The full Dockerfile:

ARG HOST_VERSION=4.3.2

FROM mcr.microsoft.com/dotnet/sdk:6.0.100-bullseye-slim-arm64v8 AS runtime-image
ARG HOST_VERSION


ENV PublishWithAspNetCoreTargetManifest=false

RUN BUILD_NUMBER=$(echo ${HOST_VERSION} | cut -d'.' -f 3) && \
    git clone --branch v${HOST_VERSION} https://github.com/Azure/azure-functions-host /src/azure-functions-host && \
    cd /src/azure-functions-host && \
    HOST_COMMIT=$(git rev-list -1 HEAD) && \
    dotnet publish -v q /p:BuildNumber=$BUILD_NUMBER /p:CommitHash=$HOST_COMMIT src/WebJobs.Script.WebHost/WebJobs.Script.WebHost.csproj -c Release --output /azure-functions-host --runtime linux-arm64 --self-contained && \
    rm -rf /root/.local /root/.nuget /src /azure-functions-host/workers

RUN apt-get update && \
    apt-get install -y gnupg wget unzip && \
    EXTENSION_BUNDLE_VERSION_V2=2.12.1 && \
    EXTENSION_BUNDLE_FILENAME_V2=Microsoft.Azure.Functions.ExtensionBundle.${EXTENSION_BUNDLE_VERSION_V2}_linux-arm64.zip && \
    wget CUSTOM_STORAGE/$EXTENSION_BUNDLE_FILENAME_V2 && \
    mkdir -p /FuncExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/$EXTENSION_BUNDLE_VERSION_V2 && \
    unzip /$EXTENSION_BUNDLE_FILENAME_V2 -d /FuncExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/$EXTENSION_BUNDLE_VERSION_V2 && \
    rm -f /$EXTENSION_BUNDLE_FILENAME_V2 &&\
    EXTENSION_BUNDLE_VERSION_V3=3.9.1 && \
    EXTENSION_BUNDLE_FILENAME_V3=Microsoft.Azure.Functions.ExtensionBundle.${EXTENSION_BUNDLE_VERSION_V3}_linux-arm64.zip && \
    wget CUSTOM_STORAGE/$EXTENSION_BUNDLE_FILENAME_V3 && \
    mkdir -p /FuncExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/$EXTENSION_BUNDLE_VERSION_V3 && \
    unzip /$EXTENSION_BUNDLE_FILENAME_V3 -d /FuncExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/$EXTENSION_BUNDLE_VERSION_V3 && \
    rm -f /$EXTENSION_BUNDLE_FILENAME_V3 &&\
    find /FuncExtensionBundles/ -type f -exec chmod 644 {} \;

FROM arm64v8/python:3.9.12-slim-bullseye
ARG HOST_VERSION

ENV LANG=C.UTF-8 \
    ACCEPT_EULA=Y \
    AzureWebJobsScriptRoot=/home/site/wwwroot \
    HOME=/home \
    FUNCTIONS_WORKER_RUNTIME=python \
    ASPNETCORE_URLS=http://+:80 \
    DOTNET_RUNNING_IN_CONTAINER=true \
    DOTNET_USE_POLLING_FILE_WATCHER=true \
    HOST_VERSION=${HOST_VERSION} \
    ASPNETCORE_CONTENTROOT=/azure-functions-host

# Install Python dependencies
RUN apt-get update && \
    apt-get install -y wget && \
    echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
    apt-get update && \
    apt-get install -y apt-transport-https curl gnupg && \
    # MSSQL dependencies
    # curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
    # curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list && \
    # # Needed for libss1.0.0 and in turn MS SQL
    # echo 'deb http://security.debian.org/debian-security jessie/updates main' >> /etc/apt/sources.list && \
    # # install necessary locales for MS SQL
    # apt-get update && apt-get install -y locales && \
    # echo 'en_US.UTF-8 UTF-8' > /etc/locale.gen && \
    # locale-gen && \
    # # install MS SQL related packages
    # apt-get update && \
    # apt-get install -y unixodbc msodbcsql18 mssql-tools && \
    # .NET Core dependencies
    apt-get install -y --no-install-recommends ca-certificates \
    libc6 libgcc1 libgssapi-krb5-2 libicu67 libssl1.1 libstdc++6 zlib1g && \
    rm -rf /var/lib/apt/lists/* && \
    # Custom dependencies:
    #  OpenCV dependencies:
    apt-get update && \
    apt-get install -y libglib2.0-0 libsm6 libxext6 libxrender-dev && \
    #  binutils
    apt-get install -y binutils && \
    #  OpenMP dependencies
    apt-get install -y libgomp1 && \
    # mysql dependencies
    apt-get install -y default-libmysqlclient-dev
# Fix from https://github.com/GoogleCloudPlatform/google-cloud-dotnet-powerpack/issues/22#issuecomment-729895157
#apt-get install -y libc-dev

# This is current not working with the .NET 6.0 image based on bullseye. Tracking here: https://github.com/Azure/azure-functions-docker/issues/451
# Chrome headless dependencies
# https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
#RUN apt-get install -y xvfb gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 \
#    libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 \
#    libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 \
#    libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 \
#    libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget

COPY --from=runtime-image ["/azure-functions-host", "/azure-functions-host"]
COPY --from=runtime-image ["/FuncExtensionBundles", "/FuncExtensionBundles" ]

RUN mkdir -p /azure-functions-host/workers/python/3.9/LINUX/Arm64 &&\
    apt install -y git &&\
    pip install grpcio-tools &&\
    pip install git+https://github.com/Azure/azure-functions-python-worker#egg=azure-functions-worker --target=/azure-functions-host/workers/python/3.9/LINUX/Arm64 &&\
    apt purge git -y &&\
    pip uninstall -y grpcio-tools

COPY ["worker.py", "/azure-functions-host/workers/python/3.9/LINUX/Arm64"]
COPY ["worker.config.json", "/azure-functions-host/workers/python" ]
COPY ["start.sh", "/azure-functions-host" ]
COPY ["sshd_config", "/etc/ssh/"]

ENV FUNCTIONS_WORKER_RUNTIME_VERSION=3.9

ENTRYPOINT ["/azure-functions-host/start.sh"]

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

WORKDIR $AzureWebJobsScriptRoot

COPY custom_app/requirements.txt .

RUN pip install -r requirements.txt

COPY start.sh /azure-functions-host

RUN chmod +x /azure-functions-host/start.sh

COPY custom_app .

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions