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 initto create a new Azure Functions app with Docker support.func init FunctionAppWithDockerfile --worker-runtime dotnet-isolated --docker --target-framework net8.0
-
Confirm the
Dockerfileis created in theFunctionAppWithDockerfilefolder. -
Run
func newto add a new HTTP trigger function.pushd ./FunctionAppWithDockerfile func new -n HttpExampleTrigger -t HttpTrigger -a anonymous -
Open
HttpExampleTrigger.csand modify the line.// Before return new OkObjectResult("Welcome to Azure Functions!"); // After return new OkObjectResult("Welcome to Azure Functions with Dockerfile!");
-
Run
docker buildto 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/HttpExampleTriggerto see the function app running. -
Run the following command to stop and remove the container.
docker stop funcappdockerfile docker rm funcappdockerfile
-
Run
func initto create a new Azure Functions app without Docker support.func init FunctionAppWithMSBuild --worker-runtime dotnet-isolated --target-framework net8.0
-
Run
func newto add a new HTTP trigger function.pushd ./FunctionAppWithMSBuild func new -n HttpExampleTrigger -t HttpTrigger -a anonymous -
Open
HttpExampleTrigger.csand modify the line.// Before return new OkObjectResult("Welcome to Azure Functions!"); // After return new OkObjectResult("Welcome to Azure Functions with MSBuild!");
-
Open
FunctionAppWithMSBuild.csprojand 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 publishcommand 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/HttpExampleTriggerto see the function app running. -
Run the following command to stop and remove the container.
docker stop funcappmsbuild docker rm funcappmsbuild