skip to Main Content

I want to deploy Azure function (node and/or python) from the lightest DockerFile.

So I need to be able to run az login and func.

For now the only one which work well is 2.27GB.


I tried a lot of possibilities with alpines, but calling func always fails.

Examples :

    1. [Fails] Alpine Latest – Install CLI from pip and func from NPM (1.36GB)
FROM alpine:latest

RUN 
  apk update && 
  apk add bash make py3-pip && 
  apk add --virtual=build gcc libffi-dev musl-dev openssl-dev python3-dev make && 
  pip3 --no-cache-dir install -U pip && 
  pip3 install azure-cli && 
  apk del --purge build

RUN apk add --update nodejs nodejs-npm && 
    npm i -g azure-functions-core-tools@latest --unsafe-perm true

Error is when I call func -h :

events.js:291
      throw er; // Unhandled 'error' event
      ^

Error: spawn /usr/local/lib/node_modules/azure-functions-core-tools/bin/func ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
    at onErrorNT (internal/child_process.js:470:16)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on ChildProcess instance at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:274:12)
    at onErrorNT (internal/child_process.js:470:16)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'spawn /usr/local/lib/node_modules/azure-functions-core-tools/bin/func',
  path: '/usr/local/lib/node_modules/azure-functions-core-tools/bin/func',
  spawnargs: [ '-h' ]
}

    1. [Fails] Node (12 or 14) alpine – Install CLI from pip and func from NPM (1.55GB)
FROM node:12-alpine # or 14-alpine

RUN 
  apk update && 
  apk add bash make py3-pip && 
  apk add --virtual=build gcc libffi-dev musl-dev openssl-dev python3-dev make && 
  pip3 --no-cache-dir install -U pip && 
  pip3 install azure-cli && 
  apk del --purge build

RUN npm i -g azure-functions-core-tools@3 --unsafe-perm true

Same error as alpine

    1. [Success] node:12 / apt installation (2.27GB) :
FROM node:12

# Install base dependencies
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y -q --no-install-recommends 
        curl 
        lsb-release 
        apt-transport-https 
        gnupg 
        ssh 
    && apt-get clean

# Install azure function
RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg 
    && mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg 
    && sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-$(lsb_release -cs)-prod $(lsb_release -cs) main" > /etc/apt/sources.list.d/dotnetdev.list' 
    && sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/debian/$(lsb_release -rs | cut -d'.' -f 1)/prod $(lsb_release -cs) main" > /etc/apt/sources.list.d/dotnetdev.list'

#Install Azure CLI
RUN curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null
RUN AZ_REPO=$(lsb_release -cs) && echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | tee /etc/apt/sources.list.d/azure-cli.list

RUN apt-get update 
    && apt-get install -y azure-functions-core-tools 
    && apt-get install azure-cli

2

Answers


  1. The latest azure-cli docker image is built with Alpine.
    Unfortunately, I wasn’t able to also run azure-functions-core-tools on Alpine… (see https://github.com/Azure/azure-functions-core-tools/issues/1358)

    However, I managed to make a slightly "smaller" image starting from ubuntu:20.04

    Size 1.77GB, running in Gitlab CI without problems (the image is also published in our Gitlab, so the pull is pretty fast)

    FROM ubuntu:20.04
    
    RUN apt-get update && apt-get install -y ca-certificates curl apt-transport-https lsb-release gnupg
    
    RUN curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.gpg
    RUN AZ_REPO=$(lsb_release -cs) && 
        echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | tee /etc/apt/sources.list.d/azure-cli.list && 
        echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-$AZ_REPO-prod $AZ_REPO main" | tee /etc/apt/sources.list.d/dotnetdev.list
    
    RUN apt-get update && apt-get install -y azure-cli azure-functions-core-tools-3
    
    RUN curl -fsSL https://deb.nodesource.com/setup_12.x | bash && apt-get install -y nodejs
    
    ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
    
    Login or Signup to reply.
  2. For anyone trying to make it work independent of the size, here is a solution using the official Microsoft image, which ends up being 2.81 GB. Based on this issue comment I was able to make it work. However, it needed some modifications. Here is what I ended up with:

    # Dockerfile
    FROM mcr.microsoft.com/azure-functions/python:2.0.12493-python3.6-buildenv
    
    # Install azure-cli
    RUN apt-get update
    RUN apt-get install -y python3 python3-pip
    RUN pip3 install azure-cli
    
    # Install azure-functions-core-tools
    RUN apt-get install -y build-essential
    RUN apt-get install -y gcc musl-dev python3-dev libffi-dev libssl-dev cargo
    RUN apt-get update && apt-get install -y ca-certificates curl apt-transport-https lsb-release gnupg
    RUN curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.gpg
    RUN AZ_REPO=$(lsb_release -cs) && 
    RUN echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | tee /etc/apt/sources.list.d/azure-cli.list && 
    RUN echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-$AZ_REPO-prod $AZ_REPO main" | tee /etc/apt/sources.list.d/dotnetdev.list
    RUN apt-get update && apt-get install -y azure-functions-core-tools-3
    

    You can use this directly locally, but I needed it for the GitLab CI pipeline. So first you need to build and push the image to the GitLab Container Repository:

    docker build -t registry.gitlab.com/GROUP/PROJECT/REPO/container-name .
    docker push registry.gitlab.com/GROUP/PROJECT/REPO/container-name:latest
    

    And finally use it in the GitLab CI pipeline:

    # .gitlab-ci.yml
    deploy:
      image: registry.gitlab.com/GROUP/PROJECT/REPO/container-name:latest
      stage: deploy
      tags:
        - dockerbuild-machine
      script:
        - pip3 install --target=".python_packages/lib/python3.8/site-packages" -r requirements.txt
        - az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID
        - az account set --subscription $AZURE_SUBSCRIPTION_ID
        - func azure functionapp publish $FUNCTION_APP --no-build
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search