Skip to content

This provides sample .NET function apps using container images with Dockerfile and with MSBuild on Docker Desktop.

License

Notifications You must be signed in to change notification settings

devkimchi/msbuild-for-functions-on-containers

Repository files navigation

MSBuild for Azure Functions on Containers

As a .NET developer, when you build a container image for your Azure Functions app, you can use the Dockerfile to define the container image. However, you can also use the dotnet publish command to build and publish the container image without a Dockerfile. This repository provides sample .NET function apps using container images with Dockerfile and with dotnet publish.

Prerequisites

Getting Started

NOTE: Make sure Docker Desktop is running before you start.

Run with Dockerfile

  1. Run func init to create a new Azure Functions app with Docker support.

    func init FunctionAppWithDockerfile --worker-runtime dotnet-isolated --docker --target-framework net8.0
  2. Confirm the Dockerfile is created in the FunctionAppWithDockerfile folder.

  3. Run func new to add a new HTTP trigger function.

    pushd ./FunctionAppWithDockerfile
    func new -n HttpExampleTrigger -t HttpTrigger -a anonymous
  4. Open HttpExampleTrigger.cs and modify the line.

    // Before
    return new OkObjectResult("Welcome to Azure Functions!");
    
    // After
    return new OkObjectResult("Welcome to Azure Functions with Dockerfile!");
  5. Run docker build to build the container image.

    docker build . -t funcapp:latest-dockerfile
  6. Return to the repository root.

    popd
  7. Run the function app container.

    docker run -d -p 7071:80 --name funcappdockerfile funcapp:latest-dockerfile
  8. Open the browser and navigate to http://localhost:7071/api/HttpExampleTrigger to see the function app running.

  9. Run the following command to stop and remove the container.

    docker stop funcappdockerfile
    docker rm funcappdockerfile

Run with dotnet publish

  1. Run func init to create a new Azure Functions app without Docker support.

    func init FunctionAppWithMSBuild --worker-runtime dotnet-isolated --target-framework net8.0
  2. Run func new to add a new HTTP trigger function.

    pushd ./FunctionAppWithMSBuild
    func new -n HttpExampleTrigger -t HttpTrigger -a anonymous
  3. Open HttpExampleTrigger.cs and modify the line.

    // Before
    return new OkObjectResult("Welcome to Azure Functions!");
    
    // After
    return new OkObjectResult("Welcome to Azure Functions with MSBuild!");
  4. Open FunctionAppWithMSBuild.csproj and add the following item group nodes.

    <ItemGroup>
      <ContainerEnvironmentVariable Include="AzureWebJobsScriptRoot" Value="/home/site/wwwroot" />
      <ContainerEnvironmentVariable Include="AzureFunctionsJobHost__Logging__Console__IsEnabled" Value="true" />
    </ItemGroup>
    
    <ItemGroup Label="ContainerAppCommand Assignment">
      <ContainerAppCommand Include="/opt/startup/start_nonappservice.sh" />
    </ItemGroup>
  5. Return to the repository root.

    popd
  6. Run the following dotnet publish command to build and publish the function app.

    dotnet publish ./FunctionAppWithMSBuild `
        -t:PublishContainer `
        --os linux --arch x64 `
        -p:ContainerBaseImage=mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 `
        -p:ContainerRepository=funcapp `
        -p:ContainerImageTag=latest-msbuild `
        -p:ContainerWorkingDirectory="/home/site/wwwroot"
  7. Run the function app container.

    docker run -d -p 7071:80 --name funcappmsbuild funcapp:latest-msbuild
  8. Open the browser and navigate to http://localhost:7071/api/HttpExampleTrigger to see the function app running.

  9. Run the following command to stop and remove the container.

    docker stop funcappmsbuild
    docker rm funcappmsbuild

About

This provides sample .NET function apps using container images with Dockerfile and with MSBuild on Docker Desktop.

Resources

License

Stars

Watchers

Forks