在 Azure 容器实例中装载 gitRepo 卷Mount a gitRepo volume in Azure Container Instances

了解如何在容器实例中装载 gitRepo 卷以克隆 Git 存储库。Learn how to mount a gitRepo volume to clone a Git repository into your container instances.

备注

当前只有 Linux 容器能装载 gitRepo 卷。Mounting a gitRepo volume is currently restricted to Linux containers. 虽然我们正致力于为 Windows 容器提供全部功能,但你可在概述中了解当前的平台差异。While we are working to bring all features to Windows containers, you can find current platform differences in the overview.

gitRepo 卷gitRepo volume

gitRepo 卷可在容器启动时装载目录,并将指定的 Git 存储库克隆到该目录。The gitRepo volume mounts a directory and clones the specified Git repository into it at container startup. 在容器实例中使用 gitRepo 卷,可避免在应用程序中为执行此操作添加代码。By using a gitRepo volume in your container instances, you can avoid adding the code for doing so in your applications.

装载 gitRepo 卷时,可以设置三个属性以对卷进行配置:When you mount a gitRepo volume, you can set three properties to configure the volume:

属性Property 必须Required 说明Description
repository Yes 要克隆的 Git 存储库的完整 URL,包括 http://https://The full URL, including http:// or https://, of the Git repository to be cloned.
directory No 存储库应克隆到的目录。Directory into which the repository should be cloned. 路径不得包含“..”,也不能以其开头。The path must not contain or start with "..". 如果指定“.”,存储库将克隆到卷的目录。If you specify ".", the repository is cloned into the volume's directory. 否则,Git 存储库将克隆到卷目录中给定名称的子目录。Otherwise, the Git repository is cloned into a subdirectory of the given name within the volume directory.
revision No 要克隆的修订的提交哈希。The commit hash of the revision to be cloned. 如果未指定,则克隆 HEAD 修订。If unspecified, the HEAD revision is cloned.

装载 gitRepo 卷:Azure CLIMount gitRepo volume: Azure CLI

若要在使用 Azure CLI 部署容器实例时装载 gitRepo 卷,请在 az container create 命令中提供 --gitrepo-url--gitrepo-mount-path 参数。To mount a gitRepo volume when you deploy container instances with the Azure CLI, supply the --gitrepo-url and --gitrepo-mount-path parameters to the az container create command. 还可以指定要将卷克隆到其中的目录 (--gitrepo-dir) 和要克隆的修订版的提交哈希 (--gitrepo-revision)。You can optionally specify the directory within the volume to clone into (--gitrepo-dir) and the commit hash of the revision to be cloned (--gitrepo-revision).

此示例命令将 Microsoft aci-helloworld 示例应用程序克隆到容器实例中的 /mnt/aci-helloworldThis example command clones the Microsoft aci-helloworld sample application into /mnt/aci-helloworld in the container instance:

az container create \
    --resource-group myResourceGroup \
    --name hellogitrepo \
    --image mcr.microsoft.com/azuredocs/aci-helloworld \
    --dns-name-label aci-demo \
    --ports 80 \
    --gitrepo-url https://github.com/Azure-Samples/aci-helloworld \
    --gitrepo-mount-path /mnt/aci-helloworld

若要验证 gitRepo 卷是否已装载,请使用 az container exec 在该容器中启动 shell 并列出目录:To verify the gitRepo volume was mounted, launch a shell in the container with az container exec and list the directory:

az container exec --resource-group myResourceGroup --name hellogitrepo --exec-command /bin/sh
/usr/src/app # ls -l /mnt/aci-helloworld/
total 16
-rw-r--r--    1 root     root           144 Apr 16 16:35 Dockerfile
-rw-r--r--    1 root     root          1162 Apr 16 16:35 LICENSE
-rw-r--r--    1 root     root          1237 Apr 16 16:35 README.md
drwxr-xr-x    2 root     root          4096 Apr 16 16:35 app

装载 gitRepo 卷:Resource ManagerMount gitRepo volume: Resource Manager

若要在使用 Azure 资源管理器模板部署容器实例时装载 gitRepo 卷,请首先填充模板的容器组 properties 节中的 volumes 数组。To mount a gitRepo volume when you deploy container instances with an Azure Resource Manager template, first populate the volumes array in the container group properties section of the template. 然后,针对容器组中希望装载 gitRepo 卷的每个容器,在容器定义的 properties 节中填充 volumeMounts 数组。Then, for each container in the container group in which you'd like to mount the gitRepo volume, populate the volumeMounts array in the properties section of the container definition.

例如,以下资源管理器模板创建了一个包含单个容器的容器组。For example, the following Resource Manager template creates a container group consisting of a single container. 该容器克隆由 gitRepo 卷块指定的两个 GitHub 存储库。The container clones two GitHub repositories specified by the gitRepo volume blocks. 第二个卷包括其他属性以指定要克隆到的目录和要克隆的特定修订的提交哈希。The second volume includes additional properties specifying a directory to clone to, and the commit hash of a specific revision to clone.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "container1name": "aci-tutorial-app",
    "container1image": "microsoft/aci-helloworld:latest"
  },
  "resources": [
    {
      "name": "volume-demo-gitrepo",
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2018-02-01-preview",
      "location": "[resourceGroup().location]",
      "properties": {
        "containers": [
          {
            "name": "[variables('container1name')]",
            "properties": {
              "image": "[variables('container1image')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGb": 1.5
                }
              },
              "ports": [
                {
                  "port": 80
                }
              ],
              "volumeMounts": [
                {
                  "name": "gitrepo1",
                  "mountPath": "/mnt/repo1"
                },
                {
                  "name": "gitrepo2",
                  "mountPath": "/mnt/repo2"
                }
              ]
            }
          }
        ],
        "osType": "Linux",
        "ipAddress": {
          "type": "Public",
          "ports": [
            {
              "protocol": "tcp",
              "port": "80"
            }
          ]
        },
        "volumes": [
          {
            "name": "gitrepo1",
            "gitRepo": {
              "repository": "https://github.com/Azure-Samples/aci-helloworld"
            }
          },
          {
            "name": "gitrepo2",
            "gitRepo": {
              "directory": "my-custom-clone-directory",
              "repository": "https://github.com/Azure-Samples/aci-helloworld",
              "revision": "d5ccfcedc0d81f7ca5e3dbe6e5a7705b579101f1"
            }
          }
        ]
      }
    }
  ]
}

前面的模板中定义的两个克隆存储库的生成目录结构如下:The resulting directory structure of the two cloned repos defined in the preceding template is:

/mnt/repo1/aci-helloworld
/mnt/repo2/my-custom-clone-directory

若要查看使用 Azure 资源管理器模板进行的容器实例部署示例,请参阅在 Azure 容器实例中部署多容器组To see an example of container instance deployment with an Azure Resource Manager template, see Deploy multi-container groups in Azure Container Instances.

专用 Git 存储库身份验证Private Git repo authentication

若要为专用 Git 存储库装载 gitRepo 卷,请在存储库 URL 中指定凭据。To mount a gitRepo volume for a private Git repository, specify credentials in the repository URL. 通常,凭据采用用户名和个人访问令牌 (PAT) 的形式,授予对存储库的范围访问权限。Typically, credentials are in the form of a user name and a personal access token (PAT) that grants scoped access to the repository.

例如,专用 GitHub 存储库的 Azure CLI --gitrepo-url 参数将类似于以下内容(其中“gituser”是 GitHub 用户名,“abcdef1234fdsa4321abcdef”是用户的个人访问令牌):For example, the Azure CLI --gitrepo-url parameter for a private GitHub repository would appear similar to the following (where "gituser" is the GitHub user name, and "abcdef1234fdsa4321abcdef" is the user's personal access token):

--gitrepo-url https://gituser:abcdef1234fdsa4321abcdef@github.com/GitUser/some-private-repository

对于 Azure Repos Git 存储库,请指定任何用户名(可以使用“azurereposuser”,如下例所示)并结合有效的 PAT:For an Azure Repos Git repository, specify any user name (you can use "azurereposuser" as in the following example) in combination with a valid PAT:

--gitrepo-url https://azurereposuser:abcdef1234fdsa4321abcdef@dev.azure.com/your-org/_git/some-private-repository

有关 GitHub 和 Azure Repos 的个人访问令牌的详细信息,请参阅以下内容:For more information about personal access tokens for GitHub and Azure Repos, see the following:

GitHub:创建命令行的个人访问令牌GitHub: Creating a personal access token for the command line

后续步骤Next steps

了解如何在 Azure 容器实例中装载其他卷类型:Learn how to mount other volume types in Azure Container Instances: