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
.
- .NET SDK 8.0+ with .NET Aspire workload
- Visual Studio or Visual Studio Code + C# Dev Kit + Azure Functions
- Azure Functions Core Tools
- Docker Desktop
NOTE: Make sure Docker Desktop is running before you start.
-
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
-
Confirm the
Dockerfile
is created in theFunctionAppWithDockerfile
folder. -
Run
func new
to add a new HTTP trigger function.pushd ./FunctionAppWithDockerfile func new -n HttpExampleTrigger -t HttpTrigger -a anonymous
-
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!");
-
Run
docker build
to build the container image.docker build . -t funcapp:latest-dockerfile
-
Return to the repository root.
popd
-
Run the function app container.
docker run -d -p 7071:80 --name funcappdockerfile funcapp:latest-dockerfile
-
Open the browser and navigate to
http://localhost:7071/api/HttpExampleTrigger
to see the function app running. -
Run the following command to stop and remove the container.
docker stop funcappdockerfile docker rm funcappdockerfile
-
Run
func init
to create a new Azure Functions app without Docker support.func init FunctionAppWithMSBuild --worker-runtime dotnet-isolated --target-framework net8.0
-
Run
func new
to add a new HTTP trigger function.pushd ./FunctionAppWithMSBuild func new -n HttpExampleTrigger -t HttpTrigger -a anonymous
-
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!");
-
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>
-
Return to the repository root.
popd
-
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"
-
Run the function app container.
docker run -d -p 7071:80 --name funcappmsbuild funcapp:latest-msbuild
-
Open the browser and navigate to
http://localhost:7071/api/HttpExampleTrigger
to see the function app running. -
Run the following command to stop and remove the container.
docker stop funcappmsbuild docker rm funcappmsbuild