如何使用 Azure node sdk 创建带有 azure private ACR 镜像的容器

how to use Azure node sdk to create a container with azure private ACR image

我有以下代码尝试使用 Azure 节点 sdk 创建具有私有 ACR 映像的 Azure 容器实例。

            let container = new client.models.Container();
            let acrcredentials = new client.models.ImageRegistryCredential();
            acrcredentials.server = '<acrreponame>.azurecr.io';
            acrcredentials.username = 'username';
            acrcredentials.password = 'password';
            acrcredentials.password = 'password';
            console.log('Launching a container for client', client);

            container.name = 'testcontainer';
            container.environmentVariables = [
                {
                    name: 'SOME_ENV_VAR',
                    value: 'test'
                }
            ];
            container.image = '<acrreponame>.azurecr.io/<image>:<tag>';
            container.ports = [{port: 80}];
            container.resources = {
                requests: {
                    cpu: 1,
                    memoryInGB: 1
                }
            };
            container.imageRegistryCredentials = acrcredentials;
            console.log('Provisioning a container', container);

            client.containerGroups.createOrUpdate(group, containerGroup,
                {
                    containers: [container],
                    osType: 'linux',
                    location: 'eastus',
                    restartPolicy: 'never'
                }
            ).then((r) => {
                console.log('Launched:', r);
            }).catch((r) => {
                console.log('Finished up with error', r);
            });

它给我以下错误:

  code: 'InaccessibleImage',
  body: 
   { code: 'InaccessibleImage',
     message: 'The image \'<acrreponame>.azurecr.io/<image>:<tag>\' in container group \'testgroup\' is not accessible. Please check the image and registry credential.' } 
   }

您要创建容器组,因此您需要在createOrUpdate 方法中设置imageRegistryCredentials。容器没有 imageRegistryCredentials 属性。

删除无效container.imageRegistryCredentials = acrcredentials;

并将imageRegistryCredentials:[acrcredentials],添加到createOrUpdate

{
      containers: [container],
      imageRegistryCredentials:[acrcredentials],
      osType: 'linux',
      location: 'eastus',
      restartPolicy: 'never'
}