您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Linux 上使用自定义映像创建函数Create a function on Linux using a custom image

使用 Azure Functions 可将函数托管在 Linux 上的自己的自定义容器中。Azure Functions lets you host your functions on Linux in your own custom container. 还可以托管在默认的 Azure 应用服务容器中You can also host on a default Azure App Service container. 此功能要求使用 Functions 2.x 运行时This functionality requires the Functions 2.x runtime.

本教程介绍如何将函数作为自定义 Docker 映像部署到 Azure。In this tutorial, you learn how to deploy your functions to Azure as a custom Docker image. 需要自定义内置的应用服务容器映像时,此模式很有用。This pattern is useful when you need to customize the built-in App Service container image. 当函数需要特定的语言版本,或者需要内置映像中未预配的特定依赖项或配置时,可以使用自定义映像。You may want to use a custom image when your functions need a specific language version or require a specific dependency or configuration that isn't provided within the built-in image. Azure Functions 支持的基础映像可以在 Azure Functions 基础映像存储库中找到。Supported base images for Azure Functions are found in the Azure Functions base images repo. Python 支持目前为预览版。Python support is in preview at this time.

本教程逐步讲解如何使用 Azure Functions Core Tools 在自定义 Linux 映像中创建函数。This tutorial walks you through how to use Azure Functions Core Tools to create a function in a custom Linux image. 请将此映像发布到 Azure 中的函数应用,该应用是使用 Azure CLI 创建的。You publish this image to a function app in Azure, which was created using the Azure CLI.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 使用 Core Tools 创建函数应用和 Dockerfile。Create a function app and Dockerfile using Core Tools.
  • 使用 Docker 生成自定义映像。Build a custom image using Docker.
  • 将自定义映像发布到容器注册表。Publish a custom image to a container registry.
  • 创建 Azure 存储帐户。Create an Azure Storage account.
  • 创建 Linux 应用服务计划。Create a Linux App Service plan.
  • 从 Docker 中心部署函数应用。Deploy a function app from Docker Hub.
  • 将应用程序设置添加到函数应用。Add application settings to the function app.
  • 启用持续部署Enable continuous deployment

支持在 Mac、Windows 或 Linux 计算机上执行以下步骤。The following steps are supported on a Mac, Windows, or Linux computer.

先决条件Prerequisites

运行此示例之前,必须做好以下准备:Before running this sample, you must have the following:

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

创建本地函数应用项目Create the local function app project

从命令行运行以下命令,以便在当前本地目录的 MyFunctionProj 文件夹中创建一个函数应用项目。Run the following command from the command line to create a function app project in the MyFunctionProj folder of the current local directory.

func init MyFunctionProj --docker

包括 --docker 选项时,会为项目生成 dockerfile。When you include the --docker option, a dockerfile is generated for the project. 此文件用于创建一个可在其中运行项目的自定义容器。This file is used to create a custom container in which to run the project. 所用基础映像取决于所选辅助角色运行时语言。The base image used depends on the worker runtime language chosen.

当系统提示时,请从下面的语言中选择一个辅助角色运行时:When prompted, choose a worker runtime from the following languages:

  • dotnet:创建一个 .NET 类库项目 (.csproj)。dotnet: creates a .NET class library project (.csproj).
  • node:创建一个 JavaScript 项目。node: creates a JavaScript project.
  • python:创建 Python 项目。python: creates a Python project.

备注

用于 Azure Functions 的 Python 当前为预览版。Python for Azure Functions is currently in preview. 若要接收重要更新,请订阅 GitHub 上的 Azure 应用服务公告存储库。To receive important updates, subscribe to the Azure App Service announcements repository on GitHub.

执行命令后,看到的内容如以下输出所示:When the command executes, you see something like the following output:

Writing .gitignore
Writing host.json
Writing local.settings.json
Writing Dockerfile

使用以下命令导航到新的 MyFunctionProj 项目文件夹。Use the following command to navigate to the new MyFunctionProj project folder.

cd MyFunctionProj

创建函数Create a function

以下命令创建一个名为 MyHttpTrigger 的 HTTP 触发的函数。The following command creates an HTTP-triggered function named MyHttpTrigger.

func new --name MyHttpTrigger --template "HttpTrigger"

执行命令后,看到的内容如以下输出所示:When the command executes, you see something like the following output:

The function "MyHttpTrigger" was created successfully from the "HttpTrigger" template.

更新函数Update the function

默认情况下,模板会创建一个函数,该函数在进行请求时需要功能键。By default, the template creates a function that requires a function key when making requests. 为了便于在 Azure 中测试此函数,需更新此函数,使之允许匿名访问。To make it easier to test the function in Azure, you need to update the function to allow anonymous access. 进行此更改的方式取决于函数项目语言。The way that you make this change depends on your functions project language.

C#C#

打开 MyHttpTrigger.cs 代码文件(你的新函数),并将函数定义中的 AuthorizationLevel 属性更新为值 Anonymous,然后保存所做的更改。Open the MyHttpTrigger.cs code file that is your new function and update the AuthorizationLevel attribute in the function definition to a value of Anonymous and save your changes.

[FunctionName("MyHttpTrigger")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log)

JavaScriptJavaScript

在文本编辑器中打开新函数的 function.json 文件,将 bindings 中的 authLevel 属性更新为 anonymous,然后保存所做的更改。Open the function.json file for your new function in a text editor, update the authLevel property in bindings to anonymous, and save your changes.

  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]

现在可以在 Azure 中调用此函数,不需提供功能键。Now you can call the function in Azure without having to supply the function key. 在本地运行时,从不需要功能键。The function key is never required when running locally.

在本地运行函数Run the function locally

以下命令用于启动函数应用。The following command starts the function app. 此应用使用 Azure 中的 Azure Functions 运行时来运行。The app runs using the same Azure Functions runtime that is in Azure.

func host start --build

若要编译 C# 项目,--build 选项是必需的。The --build option is required to compile C# projects. 不需对 JavaScript 项目使用此选项。You don't need this option for a JavaScript project.

Functions 主机启动时,会写入类似于以下输出的内容。为了可读性,这些输出已经被截断:When the Functions host starts, it write something like the following output, which has been truncated for readability:


                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %

...

Content root path: C:\functions\MyFunctionProj
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.

...

Http Functions:

        HttpTrigger: http://localhost:7071/api/MyHttpTrigger

[8/27/2018 10:38:27 PM] Host started (29486ms)
[8/27/2018 10:38:27 PM] Job host started

从运行时输出中复制 HttpTrigger 函数的 URL,将其粘贴到浏览器的地址栏中。Copy the URL of your HttpTrigger function from the runtime output and paste it into your browser's address bar. 将查询字符串 ?name=<yourname> 追加到此 URL 并执行请求。Append the query string ?name=<yourname> to this URL and execute the request. 下面演示浏览器中本地函数返回的对 GET 请求的响应:The following shows the response in the browser to the GET request returned by the local function:

在浏览器中进行本地测试

在本地运行函数以后,即可在 Azure 中创建函数应用和其他必需的资源。Now that you have run your function locally, you can create the function app and other required resources in Azure.

从 Docker 文件生成映像Build the image from the Docker file

查看项目根文件夹中的 DockerfileTake a look at the Dockerfile in the root folder of the project. 此文件描述在 Linux 上运行函数应用所需的环境。This file describes the environment that is required to run the function app on Linux. 以下示例是一个用于创建容器的 Dockerfile,该容器在 JavaScript (Node.js) 辅助角色运行时上运行函数应用:The following example is a Dockerfile that creates a container that runs a function app on the JavaScript (Node.js) worker runtime:

FROM mcr.microsoft.com/azure-functions/node:2.0

ENV AzureWebJobsScriptRoot=/home/site/wwwroot
COPY . /home/site/wwwroot

备注

Azure Functions 支持的基础映像的完整列表可以在 Azure Functions 基础映像页中找到。The complete list of supported base images for Azure Functions can be found in the Azure Functions base image page.

运行 build 命令Run the build command

在根文件夹中运行 docker build 命令,并提供名称 mydockerimage 和标记 v1.0.0In the root folder, run the docker build command, and provide a name, mydockerimage, and tag, v1.0.0. <docker-id> 替换为 Docker 中心帐户 ID。Replace <docker-id> with your Docker Hub account ID. 此命令为容器生成 Docker 映像。This command builds the Docker image for the container.

docker build --tag <docker-id>/mydockerimage:v1.0.0 .

执行命令后,看到的内容如以下输出所示,在此示例中适用于 JavaScript 辅助角色运行时:When the command executes, you see something like the following output, which in this case is for a JavaScript worker runtime:

Sending build context to Docker daemon  17.41kB
Step 1/3 : FROM mcr.microsoft.com/azure-functions/node:2.0
2.0: Pulling from azure-functions/node
802b00ed6f79: Pull complete
44580ea7a636: Pull complete
73eebe8d57f9: Pull complete
3d82a67477c2: Pull complete
8bd51cd50290: Pull complete
7bd755353966: Pull complete
Digest: sha256:480e969821e9befe7c61dda353f63298f2c4b109e13032df5518e92540ea1d08
Status: Downloaded newer image for mcr.microsoft.com/azure-functions/node:2.0
 ---> 7c71671b838f
Step 2/3 : ENV AzureWebJobsScriptRoot=/home/site/wwwroot
 ---> Running in ed1e5809f0b7
Removing intermediate container ed1e5809f0b7
 ---> 39d9c341368a
Step 3/3 : COPY . /home/site/wwwroot
 ---> 5e196215935a
Successfully built 5e196215935a
Successfully tagged <docker-id>/mydockerimage:v1.0.0

在本地测试映像Test the image locally

通过在本地容器中运行生成的 Docker 映像,验证该映像是否正常工作。Verify that the image you built works by running the Docker image in a local container. 发布 docker run 命令并将映像的名称和标记传递给它。Issue the docker run command and pass the name and tag of the image to it. 确保使用 -p 参数指定端口。Be sure to specify the port using the -p argument.

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

使用本地 Docker 容器中运行的自定义映像,通过浏览到 http://localhost:8080 验证函数应用和容器是否正常运行。With the custom image running in a local Docker container, verify the function app and container are functioning correctly by browsing to http://localhost:8080.

在本地测试函数应用。

可以选择再次测试函数,这一次请使用以下 URL 在本地容器中进行:You can optionally test your function again, this time in the local container using the following URL:

http://localhost:8080/api/myhttptrigger?name=<yourname>

验证容器中的函数应用后,请停止执行。After you have verified the function app in the container, stop the execution. 现在,可将自定义映像推送到 Docker 中心帐户。Now, you can push the custom image to your Docker Hub account.

将自定义映像推送到 Docker 中心Push the custom image to Docker Hub

注册表是一种托管映像、提供服务映像和容器服务的应用程序。A registry is an application that hosts images and provides services image and container services. 若要共享映像,必须先将其推送至注册表。To share your image, you must push it to a registry. Docker 中心是一种用于 Docker 映像的注册表,使用该注册表,用户能够托管自己的存储库,无论是公共的还是专用的均可。Docker Hub is a registry for Docker images that allows you to host your own repositories, either public or private.

推送映像之前,必须先使用 docker login 命令登录到 Docker 中心。Before you can push an image, you must sign in to Docker Hub using the docker login command. 请将 <docker-id> 替换为自己的帐户名,并在控制台中的提示符下键入密码。Replace <docker-id> with your account name and type in your password into the console at the prompt. 有关其他 Docker 中心密码选项,请参阅 docker login 命令的文档For other Docker Hub password options, see the docker login command documentation.

docker login --username <docker-id>

如果看到“登录成功”消息,说明登录成功。A "login succeeded" message confirms that you are logged in. 登录后,使用 docker push 命令将映像推送到 Docker 中心。After you have signed in, you push the image to Docker Hub by using the docker push command.

docker push <docker-id>/mydockerimage:v1.0.0

可通过检查命令的输出来验证推送是否成功。Verify that the push succeeded by examining the command's output.

The push refers to a repository [docker.io/<docker-id>/mydockerimage:v1.0.0]
24d81eb139bf: Pushed
fd9e998161c9: Mounted from <docker-id>/mydockerimage
e7796c35add2: Mounted from <docker-id>/mydockerimage
ae9a05b85848: Mounted from <docker-id>/mydockerimage
45c86e20670d: Mounted from <docker-id>/mydockerimage
v1.0.0: digest: sha256:be080d80770df71234eb893fbe4d... size: 1796

现在,可在 Azure 中将此映像用作新函数应用的部署源。Now, you can use this image as the deployment source for a new function app in Azure.

创建资源组Create a resource group

使用 az group create 创建资源组。Create a resource group with the az group create. Azure 资源组是在其中部署和管理 Azure 资源(例如函数应用、数据库和存储帐户)的逻辑容器。An Azure resource group is a logical container into which Azure resources like function apps, databases, and storage accounts are deployed and managed.

以下示例创建名为 myResourceGroup 的资源组。The following example creates a resource group named myResourceGroup.
如果不使用云 Shell,请先使用 az login 登录。If you are not using Cloud Shell, sign in first using az login.

az group create --name myResourceGroup --location westeurope

通常在附近的区域中创建资源组和资源。You generally create your resource group and the resources in a region near you. 若要查看应用服务计划的所有支持位置,请运行 az appservice list-locations 命令。To see all supported locations for App Service plans, run the az appservice list-locations command.

创建 Azure 存储帐户Create an Azure Storage account

Functions 使用通用帐户在 Azure 存储中保留状态以及有关函数的其他信息。Functions uses a general-purpose account in Azure Storage to maintain state and other information about your functions. 使用 az storage account create 命令在创建的资源组中创建通用存储帐户。Create a general-purpose storage account in the resource group you created by using the az storage account create command.

在以下命令中,请将 <storage_name> 占位符替换成全局唯一存储帐户名。In the following command, substitute a globally unique storage account name where you see the <storage_name> placeholder. 存储帐户名称必须为 3 到 24 个字符,并且只能包含数字和小写字母。Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.

az storage account create --name <storage_name> --location westeurope --resource-group myResourceGroup --sku Standard_LRS

创建存储帐户后,Azure CLI 会显示类似于以下示例的信息:After the storage account has been created, the Azure CLI shows information similar to the following example:

{
  "creationTime": "2017-04-15T17:14:39.320307+00:00",
  "id": "/subscriptions/bbbef702-e769-477b-9f16-bc4d3aa97387/resourceGroups/myresourcegroup/...",
  "kind": "Storage",
  "location": "westeurope",
  "name": "myfunctionappstorage",
  "primaryEndpoints": {
    "blob": "https://myfunctionappstorage.blob.core.windows.net/",
    "file": "https://myfunctionappstorage.file.core.windows.net/",
    "queue": "https://myfunctionappstorage.queue.core.windows.net/",
    "table": "https://myfunctionappstorage.table.core.windows.net/"
  },
     ....
    // Remaining output has been truncated for readability.
}

创建 Linux 应用服务计划Create a Linux App Service plan

消耗计划目前不支持在 Linux 上托管 Functions。Linux hosting for Functions is currently not supported on consumption plans. 必须在 Linux 应用服务计划中托管 Linux 容器应用。You have to host Linux container apps in a Linux App Service plan. 若要了解有关托管的详细信息,请参阅 Azure Functions 托管计划比较To learn more about hosting, see Azure Functions hosting plans comparison.

在 Cloud Shell 中,使用 az appservice plan create 命令在资源组中创建应用服务计划。In the Cloud Shell, create an App Service plan in the resource group with the az appservice plan create command.

以下示例在基本定价层 (--sku B1) 和 Linux 容器 (--is-linux) 中创建名为 myAppServicePlan 的应用服务计划。The following example creates an App Service plan named myAppServicePlan in the Basic pricing tier (--sku B1) and in a Linux container (--is-linux).

az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku B1 --is-linux

创建应用服务计划后,Azure CLI 会显示类似于以下示例的信息:When the App Service plan has been created, the Azure CLI shows information similar to the following example:

{ 
  "adminSiteName": null,
  "appServicePlanName": "myAppServicePlan",
  "geoRegion": "West Europe",
  "hostingEnvironmentProfile": null,
  "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan",
  "kind": "linux",
  "location": "West Europe",
  "maximumNumberOfWorkers": 1,
  "name": "myAppServicePlan",
  < JSON data removed for brevity. >
  "targetWorkerSizeId": 0,
  "type": "Microsoft.Web/serverfarms",
  "workerTierName": null
} 

创建并部署自定义映像Create and deploy the custom image

函数应用托管函数的执行环境。The function app hosts the execution of your functions. 使用 az functionapp create 命令基于 Docker 中心映像创建一个函数应用。Create a function app from a Docker Hub image by using the az functionapp create command.

在以下命令中,请将 <app_name> 占位符替换成唯一函数应用名称,将 <storage_name> 替换为存储帐户名。In the following command, substitute a unique function app name where you see the <app_name> placeholder and the storage account name for <storage_name>. <app_name> 将用作 Function App 的默认 DNS 域,因此,该名称需要在 Azure 中的所有应用之间保持唯一。The <app_name> is used as the default DNS domain for the function app, and so the name needs to be unique across all apps in Azure. 如前所述,<docker-id> 是你的 Docker 帐户名称。As before, <docker-id> is your Docker account name.

az functionapp create --name <app_name> --storage-account  <storage_name>  --resource-group myResourceGroup \
--plan myAppServicePlan --deployment-container-image-name <docker-id>/mydockerimage:v1.0.0

创建 Function App 后,Azure CLI 会显示类似于以下示例的信息:After the function app has been created, the Azure CLI shows information similar to the following example:

{
  "availabilityState": "Normal",
  "clientAffinityEnabled": true,
  "clientCertEnabled": false,
  "containerSize": 1536,
  "dailyMemoryTimeQuota": 0,
  "defaultHostName": "quickstart.azurewebsites.net",
  "enabled": true,
  "enabledHostNames": [
    "quickstart.azurewebsites.net",
    "quickstart.scm.azurewebsites.net"
  ],
   ....
    // Remaining output has been truncated for readability.
}

deployment-container-image-name 参数表示 Docker 中心托管的、用于创建函数应用的映像。The deployment-container-image-name parameter indicates the image hosted on Docker Hub to use to create the function app. 若要查看用于部署的映像的相关信息,请使用 az functionapp config container show 命令。Use the az functionapp config container show command to view information about the image used for deployment. 若要从另一映像进行部署,请使用 az functionapp config container set 命令。Use the az functionapp config container set command to deploy from a different image.

配置函数应用Configure the function app

该函数需要使用连接字符串连接到默认的存储帐户。The function needs the connection string to connect to the default storage account. 将自定义映像发布到专用容器帐户时,应该使用 ENV 指令或类似的命令将这些应用程序设置指定为 Dockerfile 中的环境变量。When you are publishing your custom image to a private container account, you should instead set these application settings as environment variables in the Dockerfile using the ENV instruction, or something similar.

在本例中,<storage_name> 是所创建的存储帐户的名称。In this case, <storage_name> is the name of the storage account you created. 使用 az storage account show-connection-string 命令获得连接字符串。Get the connection string with the az storage account show-connection-string command. 使用 az functionapp config appsettings set 命令将这些应用程序设置添加到函数应用。Add these application settings in the function app with the az functionapp config appsettings set command.

storageConnectionString=$(az storage account show-connection-string \
--resource-group myResourceGroup --name <storage_name> \
--query connectionString --output tsv)

az functionapp config appsettings set --name <app_name> \
--resource-group myResourceGroup \
--settings AzureWebJobsDashboard=$storageConnectionString \
AzureWebJobsStorage=$storageConnectionString

备注

如果容器为专用,则还需设置以下应用程序设置If your container is private, you would have to set the following application settings as well

  • DOCKER_REGISTRY_SERVER_USERNAMEDOCKER_REGISTRY_SERVER_USERNAME
  • DOCKER_REGISTRY_SERVER_PASSWORDDOCKER_REGISTRY_SERVER_PASSWORD

需停止函数应用,然后再将其启动,以便这些值生效You will have to stop and then start your function app for these values to be picked up

现在,可以测试 Azure 中的 Linux 上运行的函数。You can now test your functions running on Linux in Azure.

在 Azure 中测试函数Test the function in Azure

使用 cURL 测试已部署的函数。Use cURL to test the deployed function. 使用从上一步复制的 URL,将查询字符串 &name=<yourname> 追加到该 URL,如以下示例所示:Using the URL that you copied from the previous step, append the query string &name=<yourname> to the URL, as in the following example:

curl https://myfunctionapp.azurewebsites.net/api/httptrigger?code=cCr8sAxfBiow548FBDLS1....&name=<yourname>

使用 cURL 在 Azure 中调用函数。

还可以将复制的 URL 粘贴到 Web 浏览器的地址中。You can also paste the copied URL in to the address of your web browser. 同样,在执行请求之前,先将查询字符串 &name=<yourname> 追加到该 URL。Again, append the query string &name=<yourname> to the URL before you execute the request.

使用 Web 浏览器调用函数。

启用 Application InsightsEnable Application Insights

监视函数执行的建议方法是将函数应用与 Azure Application Insights 集成。The recommended way to monitor the execution of your functions is by integrating your function app with Azure Application Insights. 在 Azure 门户中创建函数应用时,默认情况下会为你完成此集成。When you create a function app in the Azure portal, this integration is done for you by default. 但是,当你使用 Azure CLI 创建函数应用时,Azure 的函数应用中的集成并未完成。However, when you create your function app by using the Azure CLI, the integration in your function app in Azure isn't done.

若要为函数应用启用 Application Insights,请执行以下操作:To enable Application Insights for your function app:

可以通过 Functions 轻松地将 Application Insights 集成从 Azure 门户添加到某个函数应用。Functions makes it simple to add Application Insights integration to a function app from the Azure portal.

  1. 门户中选择“所有服务”>“Function App”,接着选择你的函数应用,然后选择窗口顶部的“Application Insights”横幅 In the portal, select All services > Function Apps, select your function app, and then choose the Application Insights banner at the top of the window

    从门户启用 Application Insights

  2. 创建 Application Insights 资源,方法是使用图像下面表中指定的设置:Create an Application Insights resource by using the settings specified in the table below the image:

    创建 Application Insights 资源

    设置Setting 建议的值Suggested value 说明Description
    名称Name 唯一的应用名称Unique app name 使用与函数应用相同的名称是最方便的,该名称在订阅中必须独一无二。It's easiest to use the same name as your function app, which must be unique in your subscription.
    位置Location 西欧West Europe 尽可能使用与函数应用相同的区域,或与之相近的区域。If possible, use the same region as your function app, or near to it.
  3. 选择“确定”。 Choose OK. Application Insights 资源在与函数应用相同的资源组和订阅中创建。The Application Insights resource is created in the same resource group and subscription as your function app. 创建完以后,请关闭 Application Insights 窗口。After creation completes, close the Application Insights window.

  4. 回到函数应用中,选择“应用程序设置”,然后向下滚动到“应用程序设置”。 Back in your function app, select Application settings, and scroll down to Application settings. 如果看到名为 APPINSIGHTS_INSTRUMENTATIONKEY 的设置,则表明已为在 Azure 中运行的函数应用启用 Application Insights 集成。When you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY, it means that Application Insights integration is enabled for your function app running in Azure.

若要了解详细信息,请参阅监视 Azure FunctionsTo learn more, see Monitor Azure Functions.

启用持续部署Enable continuous deployment

使用容器的一大好处是,在注册表中更新容器时可以自动部署更新。One of the benefits of using containers is being able to automatically deploy updates when containers are updated in the registry. 使用 az functionapp deployment container config 命令启用持续部署。Enable continuous deployment with the az functionapp deployment container config command.

az functionapp deployment container config --enable-cd \
--query CI_CD_URL --output tsv \
--name <app_name> --resource-group myResourceGroup

此命令在启用持续部署后返回部署 Webhook URL。This command returns the deployment webhook URL after continuous deployment is enabled. 也可使用 az functionapp deployment container show-cd-url 命令返回该 URL。You can also use the az functionapp deployment container show-cd-url command to return this URL.

复制部署 URL 并浏览到 DockerHub 存储库,选择“Webhook”选项卡,键入 Webhook 的 Webhook 名称,将 URL 粘贴到“Webhook URL”中,然后选择加号 (+)。Copy the deployment URL and browse to your DockerHub repo, choose the Webhooks tab, type a Webhook name for the webhook, paste your URL in Webhook URL, and then choose the plus sign (+).

将 Webhook 添加到 DockerHub 存储库中

设置 Webhook 以后,只要 DockerHub 中的链接映像进行了更新,函数应用就会下载并安装最新映像。With the webhook set, any updates to the linked image in DockerHub result in the function app downloading and installing the latest image.

清理资源Clean up resources

本教程系列中的其他快速入门教程是在本文的基础上制作的。Other quickstarts in this collection build upon this quickstart. 如果打算继续学习后续的快速入门或相关教程,请不要清除在本快速入门中创建的资源。If you plan to continue on to work with subsequent quickstarts or with the tutorials, do not clean up the resources created in this quickstart. 如果不打算继续学习,请使用以下命令删除通过本快速入门创建的所有资源:If you do not plan to continue, use the following command to delete all resources created by this quickstart:

az group delete --name myResourceGroup

出现提示时请键入 yType y when prompted.

后续步骤Next steps

本教程介绍了如何:In this tutorial, you learned how to:

  • 使用 Core Tools 创建函数应用和 Dockerfile。Create a function app and Dockerfile using Core Tools.
  • 使用 Docker 生成自定义映像。Build a custom image using Docker.
  • 将自定义映像发布到容器注册表。Publish a custom image to a container registry.
  • 创建 Azure 存储帐户。Create an Azure Storage account.
  • 创建 Linux 应用服务计划。Create a Linux App Service plan.
  • 从 Docker 中心部署函数应用。Deploy a function app from Docker Hub.
  • 将应用程序设置添加到函数应用。Add application settings to the function app.

了解如何启用内置于核心应用服务平台中的持续集成功能。Learn how to enable continuous integration functionality built into the core App Service platform. 可以配置函数应用,以便在更新 Docker 中心内的映像时重新部署容器。You can configure your function app so that the container is redeployed when you update your image in Docker Hub.