Home > Mobile >  Run and watch Azure Function locally in docker
Run and watch Azure Function locally in docker

Time:07-05

I am trying to build a docker image of Azure function and watch for changes as dotnet watch in .Net API or nodemon for NodeJs.

I have created an Azure function in VS 2022 (default Azure Function project with docker file) and changed the docker file accordingly to reflect the watch changes.

The container runs, but it shows no HTTP routes mapped. However when debuding from VS 2022 there is a default route (Function1) and works fine.

docker-compose version: '3.4'

services:

func-test:
    image: func-test:dev
    build:
      context: ./FunctionApp1
      dockerfile: Dockerfile
    ports:
      - 8026:80
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - AZURE_FUNCTIONS_ENVIRONMENT=Development
      - DOTNET_USE_POLLING_FILE_WATCHER=1
    volumes:
      - ./FunctionApp1:/src

docker file

FROM mcr.microsoft.com/dotnet/sdk:6.0 as build
EXPOSE 80
RUN apt-get update
RUN apt-get -y install curl gnupg
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get -y install nodejs
RUN npm i -g azure-functions-core-tools@4 --unsafe-perm true
RUN apt-get install -y unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg
RUN apt-get update && apt-get install -y coreutils
WORKDIR /src
COPY ["FunctionApp1.csproj", "FunctionApp1/"]
RUN dotnet restore "FunctionApp1/FunctionApp1.csproj"
COPY . .
WORKDIR "/src/FunctionApp1"
RUN cat "host.json"
ENTRYPOINT ["func", "host", "start", "--verbose"]
# dotnet watch msbuild /t:RunFunctions
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

A few last lines from the output

func-test_1 | [2022-07-02T23:13:27.618Z] Initializing function HTTP routes func-test_1 | [2022-07-02T23:13:27.618Z] No HTTP routes mapped func-test_1 | [2022-07-02T23:13:27.618Z] func-test_1 | [2022-07-02T23:13:27.626Z] Host initialized (59ms) func-test_1 | [2022-07-02T23:13:27.627Z] Host started (68ms) func-test_1 | [2022-07-02T23:13:27.627Z] Job host started func-test_1 | [2022-07-02T23:13:32.558Z] Host lock lease acquired by instance ID '000000000000000000000000F603E931'.

Function

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered functions executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

The project folder is available in Github Project link

CodePudding user response:

Finally fixed it with below docker-compose and docker file. You can also have the working project from the gitHub mentioned in the question.

docker file

# FROM mcr.microsoft.com/azure-functions/dotnet:3 as base
FROM mcr.microsoft.com/dotnet/sdk:6.0
RUN apt-get update
RUN apt-get -y install curl gnupg
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get -y install nodejs
RUN npm i -g azure-functions-core-tools@4 --unsafe-perm true
RUN apt-get install -y unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg
# RUN apt-get update && apt-get install -y coreutils
RUN apt-get install -y tini
WORKDIR /src
COPY ["FunctionApp1.csproj", "FunctionApp1/"]
RUN dotnet restore "FunctionApp1/FunctionApp1.csproj"
WORKDIR "/src/FunctionApp1"
# COPY . .
# ENTRYPOINT ["func", "host", "start", "--verbose"]
# ENTRYPOINT ["func", "host", "start", "--verbose"]
ENTRYPOINT ["/usr/bin/tini","--"]
CMD ["dotnet" ,"watch", "msbuild", "/t:RunFunctions"]

docker-compose file

version: '3.4'

services:
    func-test:
        image: func-test:dev
        build:
          context: ./FunctionApp1
          dockerfile: Dockerfile
        init: true
        ports:
          - 8026:8080
        restart: on-failure
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
          - AZURE_FUNCTIONS_ENVIRONMENT=Development
          - DOTNET_USE_POLLING_FILE_WATCHER=1
          - AzureWebJobsScriptRoot=/home/site/wwwroot
          - AzureFunctionsJobHost__Logging__Console__IsEnabled=true
          - DOTNET_USE_POLLING_FILE_WATCHER=1
        volumes:
          - ./FunctionApp1:/src/FunctionApp1:delegated
  • Related