Skip to content

Latest commit

 

History

History
273 lines (213 loc) · 14.2 KB

File metadata and controls

273 lines (213 loc) · 14.2 KB
author ggailey777
ms.service azure-functions
ms.custom
build-2024
ms.topic include
ms.date 01/03/2025
ms.author glenga

Choose your development language

First, you use Azure Functions tools to create your project code as a function app in a Docker container by using a language-specific Linux base image. Make sure to select your language of choice at the top of the article.

Core Tools automatically generates a Dockerfile for your project that uses the most up-to-date version of the correct base image for your functions language. You should regularly update your container from the latest base image and redeploy from the updated version of your container. For more information, see Create containerized function apps.

Prerequisites

Before you begin, you must have the following requirements in place:

::: zone pivot="programming-language-csharp"

::: zone pivot="programming-language-java,programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python"

::: zone pivot="programming-language-python"

  • Install the Azure CLI version 2.4 or later.

If you don't have an Azure subscription, create a free account before you begin.

To publish the containerized function app image you create to a container registry, you need a Docker ID and Docker Desktop running on your local computer. If you don't have a Docker ID, you can create a Docker account.

You also need to complete the Create a container registry section of the Container Registry quickstart. Make a note of your fully qualified login server name.

You should be all set.


[!INCLUDE functions-cli-create-venv]

Create and test the local functions project

::: zone pivot="programming-language-csharp,programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python"
In a terminal or command prompt, run the following command for your chosen language to create a function app project in the current folder:
::: zone-end
::: zone pivot="programming-language-csharp"

func init --worker-runtime dotnet-isolated --docker

::: zone-end
::: zone pivot="programming-language-javascript"

func init --worker-runtime node --language javascript --docker

::: zone-end
::: zone pivot="programming-language-powershell"

func init --worker-runtime powershell --docker

::: zone-end
::: zone pivot="programming-language-python"

func init --worker-runtime python --docker

::: zone-end
::: zone pivot="programming-language-typescript"

func init --worker-runtime node --language typescript --docker

::: zone-end ::: zone pivot="programming-language-java"
In an empty folder, run the following command to generate the Functions project from a Maven archetype:

mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype -DjavaVersion=8 -Ddocker
mvn archetype:generate "-DarchetypeGroupId=com.microsoft.azure" "-DarchetypeArtifactId=azure-functions-archetype" "-DjavaVersion=8" "-Ddocker"
mvn archetype:generate "-DarchetypeGroupId=com.microsoft.azure" "-DarchetypeArtifactId=azure-functions-archetype" "-DjavaVersion=8" "-Ddocker"

The -DjavaVersion parameter tells the Functions runtime which version of Java to use. Use -DjavaVersion=11 if you want your functions to run on Java 11. When you don't specify -DjavaVersion, Maven defaults to Java 8. For more information, see Java versions.

Important

The JAVA_HOME environment variable must be set to the install location of the correct version of the JDK to complete this article.

Maven asks you for values needed to finish generating the project on deployment. Follow the prompts and provide the following information:

Prompt Value Description
groupId com.fabrikam A value that uniquely identifies your project across all projects, following the package naming rules for Java.
artifactId fabrikam-functions A value that is the name of the jar, without a version number.
version 1.0-SNAPSHOT Select the default value.
package com.fabrikam.functions A value that is the Java package for the generated function code. Use the default.

Type Y or press Enter to confirm.

Maven creates the project files in a new folder named artifactId, which in this example is fabrikam-functions. ::: zone-end

The --docker option generates a Dockerfile for the project, which defines a suitable container for use with Azure Functions and the selected runtime.

::: zone pivot="programming-language-java"
Navigate into the project folder:

cd fabrikam-functions

::: zone-end
::: zone pivot="programming-language-csharp" Use the following command to add a function to your project, where the --name argument is the unique name of your function and the --template argument specifies the function's trigger. func new creates a C# code file in your project.

func new --name HttpExample --template "HTTP trigger"

::: zone-end

::: zone pivot="programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python" Use the following command to add a function to your project, where the --name argument is the unique name of your function and the --template argument specifies the function's trigger. func new creates a subfolder matching the function name that contains a configuration file named function.json.

func new --name HttpExample --template "HTTP trigger"

::: zone-end
To test the function locally, start the local Azure Functions runtime host in the root of the project folder. To ensure the function can be called later when hosted in Docker, check that the authorization level is set to AuthorizationLevel.Anonymous, or set it if not already configured. ::: zone pivot="programming-language-csharp"

func start  

::: zone-end
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"

func start  

::: zone-end
::: zone pivot="programming-language-typescript"

npm install
npm start

::: zone-end
::: zone pivot="programming-language-java"

mvn clean package  
mvn azure-functions:run

::: zone-end
::: zone pivot="programming-language-csharp"
After you see the HttpExample endpoint written to the output, navigate to that endpoint. You should see a welcome message in the response output. ::: zone-end
::: zone pivot="programming-language-java,programming-language-javascript,programming-language-powershell,programming-language-python" After you see the HttpExample endpoint written to the output, navigate to http://localhost:7071/api/HttpExample?name=Functions. The browser must display a "hello" message that echoes back Functions, the value supplied to the name query parameter. ::: zone-end

Press Ctrl+C (Command+C on macOS) to stop the host.

Build the container image and verify locally

(Optional) Examine the Dockerfile in the root of the project folder. The Dockerfile describes the required environment to run the function app on Linux. The complete list of supported base images for Azure Functions can be found in the Azure Functions base image page.

In the root project folder, run the docker build command, provide a name as azurefunctionsimage, and tag as v1.0.0. Replace <DOCKER-ID> with your Docker Hub account ID. This command builds the Docker image for the container.

docker build --tag <DOCKER-ID>/azurefunctionsimage:v1.0.0 .

When the command completes, you can run the new container locally.

To verify the build, run the image in a local container using the docker run command, replace <DOCKER-ID> again with your Docker Hub account ID, and add the ports argument as -p 8080:80:

docker run -p 8080:80 -it <DOCKER-ID>/azurefunctionsimage:v1.0.0

::: zone pivot="programming-language-csharp" After the image starts in the local container, browse to http://localhost:8080/api/HttpExample, which must display the same greeting message as before. Because the HTTP triggered function you created uses anonymous authorization, you can call the function running in the container without having to obtain an access key. For more information, see authorization keys. ::: zone-end
::: zone pivot="programming-language-java,programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python" After the image starts in the local container, browse to http://localhost:8080/api/HttpExample?name=Functions, which must display the same "hello" message as before. Because the HTTP triggered function you created uses anonymous authorization, you can call the function running in the container without having to obtain an access key. For more information, see authorization keys. ::: zone-end

After verifying the function app in the container, press Ctrl+C (Command+C on macOS) to stop execution.

Publish the container image to a registry

To make your container image available for deployment to a hosting environment, you must push it to a container registry. As a security best practice, you should use an Azure Container Registry instance and enforce managed identity-based connections. Docker Hub requires you to authenticate using shared secrets, which make your deployments more vulnerable.

Azure Container Registry is a private registry service for building, storing, and managing container images and related artifacts. You should use a private registry service for publishing your containers to Azure services.

  1. Use this command to sign in to your registry instance using your current Azure credentials. Replace <REGISTRY-NAME> with the name of your Container Registry instance.

    az acr login --name <REGISTRY-NAME>
    
  2. Use this command to tag your image with the fully qualified name of your registry login server. Replace <LOGIN-SERVER> with the fully qualified name of your registry login server and <DOCKER-ID> with your Docker ID.

    docker tag <DOCKER-ID>/azurefunctionsimage:v1.0.0 <LOGIN-SERVER>/azurefunctionsimage:v1.0.0 
    
  3. Use this command to push the container to your registry instance:

    docker push <LOGIN-SERVER>/azurefunctionsimage:v1.0.0
    

Docker Hub is a container registry that hosts images and provides image and container services.

  1. If you haven't already signed in to Docker, do so with the docker login command. This command prompts you for your username and password. A "sign in succeeded" message confirms that you're signed in.

    docker login
  2. After you've signed in, push the image to Docker Hub by using the docker push command, again replace the <DOCKER-ID> with your Docker Hub account ID.

    docker image push <DOCKER-ID>/azurefunctionsimage:v1.0.0

    Depending on your network speed, pushing the image for the first time might take a few minutes. Subsequent changes are pushed faster.