Azure-functions-core-tools: Deploying dotNet function to Kubernetes providing docker build args

Created on 11 Feb 2020  ยท  2Comments  ยท  Source: Azure/azure-functions-core-tools

Hi all,

I'm new to Azure functions and Keda, but I have working experience with k8s - appreciate some assistance in below description.

INFO

me@localhost  โžค  dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.100
 Commit:    cd82f021f4

Runtime Environment:
 OS Name:     arch
 OS Version:
 OS Platform: Linux
 RID:         arch-x64
 Base Path:   /usr/share/dotnet/sdk/3.1.100/

Host (useful for support):
  Version: 3.1.0
  Commit:  6e49edcd54

.NET Core SDKs installed:
  2.1.803 [/usr/share/dotnet/sdk]
  2.2.109 [/usr/share/dotnet/sdk]
  3.0.102 [/usr/share/dotnet/sdk]
  3.1.100 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.NETCore.App 2.1.15 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.7 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

me@localhost  โžค func --version
2.7.2045

Description:
Trying to deploy my dockerized dotNet function app on k8s, the app uses a custom dockerfile that facilitates the acquisition of credentials to restore NuGet packages hosted on private server feed with the help of Azure Artifacts Credential Provider plugin.

Problem:
Using the command func kubernetes deploy --name some-func-app --registry DOMAIN.azurecr.io --dotnet
Will fail because there is no build args being passed to the consumed docker build command by func kubernetes deploy parent command.

Dockerfile
Here is my docker file

# Downloading the dotnet sdk image. Could be any docker sdk image with sdk > 2.1.500
FROM microsoft/dotnet:2.2-sdk AS dotnet-builder

ARG PROJECT
ARG FEED_URL
ARG PAT

# Download and install latest credential provider. Not required after https://github.com/dotnet/dotnet-docker/issues/878
RUN wget -qO- https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh | bash

WORKDIR /src

# Optional: Sometimes the http client hangs because of a .NEt issue. Setting this in dockerfile helps
ENV DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER 0

# Environment variable to enable seesion token cache. More on this here: https://github.com/Microsoft/artifacts-credprovider#help
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true

# Add "FEED_URL" AND "PAT" using --build-arg in docker build step. "endpointCredentials" field is an array, you can add multiple endpoint configurations.
# Make sure that you *do not* hard code the "PAT" here. That is a sensitive information and must not be checked in.
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS "{\"endpointCredentials\": [{\"endpoint\":\"${FEED_URL}\", \"username\":\"azure_user\", \"password\":\"${PAT}\"}]}"

# Force NuGet to use basic authentication
ENV NUGET_AUTHENTICATION_TYPES basic

# Copy project files, use this if you have a nuget.config file with all the endpoints.
COPY . /src/dotnet-function-app

RUN cd /src/dotnet-function-app/"${PROJECT}" \
    && mkdir -p /home/site/wwwroot \
    && dotnet publish *.csproj --output /home/site/wwwroot

FROM mcr.microsoft.com/azure-functions/dotnet:2.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot
ENV AzureFunctionsJobHost__Logging__Console__IsEnabled=true
COPY --from=dotnet-builder ["/home/site/wwwroot", "/home/site/wwwroot"]

Build
As you can see the function apps uses nuget packages that is stored in a private Artifacts server feed - This is how I get a build with docker

docker build . \
--build-arg PROJECT=FUNCTIONS-DIRECTORY-IN-REPO \
--build-arg FEED_URL=https://pkgs.dev.azure.com/DOMAIN/Packages/nuget/v3/index.json \
--build-arg PAT=${PAT} -t DOMAIN.azurecr.io/PROJECT_NAME:${IMAGE_TAG}

Run
And this is how I run it

docker run -it -p 7071:80 \
--env-file=./local.settings.env \
DOMAIN.azurecr.io/PROJECT_NAME:${IMAGE_TAG}

App Directory Strutcture

me@localhost  โžค  tree -d -L 3
โœ”  ~/code/SomeFunctionApp [develop|โ€ฆ 5]
.
โ”œโ”€โ”€ SomeFunctionApp
โ”‚ย ย  โ”œโ”€โ”€ bin
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ Debug
โ”‚ย ย  โ”œโ”€โ”€ Constants
โ”‚ย ย  โ”œโ”€โ”€ Data
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Entities
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Exceptions
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Incoming
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Outgoing
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Partials
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Validators
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ ValueObjects
โ”‚ย ย  โ”œโ”€โ”€ Functions
โ”‚ย ย  โ”œโ”€โ”€ Helpers
โ”‚ย ย  โ”œโ”€โ”€ obj
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ Debug
โ”‚ย ย  โ””โ”€โ”€ Repositories
โ””โ”€โ”€ SomeFunctionAppTests
    โ””โ”€โ”€ Util

Most helpful comment

Instead of specifying --registry, which causes Functions Core Tools to run a docker build and push the resulting image to the registry, you can manually build the image by specifying custom build args and push it to the registry yourself.

Then you should be able to run kubernetes deploy --name some-func-app --image-name DOMAIN.azurecr.io/somefuncapp:latest to deploy it.

If you're having trouble, try dumping out the Kubernetes manifest by supplying the --dry-run flag. You can debug it, edit it to your needs, and run it manually.

All 2 comments

Instead of specifying --registry, which causes Functions Core Tools to run a docker build and push the resulting image to the registry, you can manually build the image by specifying custom build args and push it to the registry yourself.

Then you should be able to run kubernetes deploy --name some-func-app --image-name DOMAIN.azurecr.io/somefuncapp:latest to deploy it.

If you're having trouble, try dumping out the Kubernetes manifest by supplying the --dry-run flag. You can debug it, edit it to your needs, and run it manually.

Thanks @anthonychu
I was able to deploy the func app providing your answer with the --image-name parameter.
Cheers man

Was this page helpful?
0 / 5 - 0 ratings

Related issues

christopheranderson picture christopheranderson  ยท  6Comments

sonic1981 picture sonic1981  ยท  4Comments

cgillum picture cgillum  ยท  4Comments

lastcoolnameleft picture lastcoolnameleft  ยท  4Comments

brandonh-msft picture brandonh-msft  ยท  4Comments