共用方式為


在 Azure 容器執行個體中掛接 gitRepo 磁碟區

了解如何掛接 gitRepo 磁碟區,以將 Git 存放庫複製到您的容器執行個體中。

注意

目前只有 Linux 容器才能掛接 gitRepo 磁碟區。 在我們設法將所有功能導入 Windows 容器的同時,您可先在概觀中找到目前的平台差異。

gitRepo 磁碟區

gitRepo 磁碟區會掛接目錄,並在容器建立期間將指定的 Git 存放庫複製到其中。 透過在您的容器執行個體中使用 gitRepo 磁碟區,您可以避免在您的應用程式中加入程式碼。

當您掛接 gitRepo 磁碟區時,您可以設定三個屬性來設定磁碟區:

屬性 必要 描述:
repository Yes 要複製之 Git 存放庫的完整 URL,包括 http://https://
directory No 要在其中複製存放庫的目錄。 路徑不能包含或開頭為 ".."。 如果您指定 ".",存放庫會複製到磁碟區的目錄中。 否則,Git 存放庫會複製到磁碟區目錄內指定名稱的子目錄中。
revision No 要複製之修訂的認可雜湊。 如果未指定,則 HEAD 修訂已複製。

掛接 gitRepo 磁碟區:Azure CLI

當您使用 Azure CLI 部署容器執行個體時,若要掛接 gitRepo 磁碟區,請將 --gitrepo-url--gitrepo-mount-path 參數提供給 az container create 命令。 您可以選擇性地指定磁碟區內要複製存放庫到其中的目錄 (--gitrepo-dir),以及要複製的修訂認可雜湊 (--gitrepo-revision)。

此範例命令會將 Microsoft aci-helloworld 範例應用程式複製至容器執行個體中的 /mnt/aci-helloworld

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 並列出目錄:

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 Manager

當您使用 Azure Resource Manager 範本部署容器執行個體時,若要掛接 gitRepo 磁碟區,請先在範本的容器群組 properties 區段中填入 volumes 陣列。 然後,針對您想要掛接 gitRepo 磁碟區所在容器群組中的每個容器,填入容器定義 properties 區段中的 volumeMounts 陣列。

例如,下列 Resource Manager 範本會建立一個由單一容器組成的容器群組。 容器會複製由 gitRepo 磁碟區區塊所指定的兩個 GitHub 存放庫。 第二個磁碟區包含指定要複製到其中之目錄的其他屬性,和要複製之指定修訂的認可雜湊。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "container1name": "aci-tutorial-app",
    "container1image": "mcr.microsoft.com/azuredocs/aci-helloworld"
  },
  "resources": [
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2021-03-01",
      "name": "volume-demo-gitrepo",
      "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"
            }
          }
        ]
      }
    }
  ]
}

上述範本中定義的兩個已複製存放庫的結果目錄結構是:

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

若要使用 Azure Resource Manager 範本來查看容器執行個體部署範例,請參閱在 Azure 容器執行個體中部署多個容器群組

私人 Git 存放庫驗證

若要為私人 Git 存放庫掛接 gitRepo 磁碟區,請在存放庫 URL 中指定認證。 通常,認證的形式是使用者名稱和個人存取權杖 (PAT),可授與存放庫的有限範圍存取。

例如,私人 GitHub 存放庫的 Azure CLI --gitrepo-url 參數會類似下列項目 (其中 "gituser" 是 GitHub 使用者名稱,"abcdef1234fdsa4321abcdef" 是使用者的個人存取權杖):

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

針對 Azure Repos Git 存放庫,指定任何使用者名稱 (您可以如同下列範例一樣使用 "azurereposuser") 與有效的 PAT 搭配使用:

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

如需 GitHub 和 Azure Repos 的個人存取權杖詳細資訊,請參閱下列項目:

GitHub:針對命令列建立個人存取權杖

Azure Repos:建立個人存取權杖來驗證存取

下一步

了解如何在 Azure 容器執行個體中掛接其他類型的磁碟區: