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

瞭解如何掛接 gitRepo 磁碟區,以將 Git 存放庫複製到您的容器實例。

注意

掛接 gitRepo 磁碟區目前僅限於 Linux 容器。 在我們設法將所有功能導入 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-mount-path 參數提供給 --gitrepo-url 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 啟動容器中的殼層並列出目錄:

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 磁碟區之容器群組中的每個容器,填 volumeMounts 入容器定義區段中的陣列 properties

例如,下列 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 容器執行個體中掛接其他類型的磁碟區: