azure - 无法从 Azure 注册表中的 docker 镜像运行 Azure Web 服务

标签 azure docker azure-web-app-service azure-pipelines devops

我正在尝试通过在 Azure DevOps 中创建执行以下操作的管道来自动化部署过程

  1. 构建我的项目,创建 Docker 镜像,然后将该镜像推送到私有(private) Azure 注册表服务。
  2. 将镜像部署到 Azure Web 服务中名为staging 的插槽上。

这是我正在使用的 .yaml 文件

trigger:
- master

resources:
- repo: self

variables:
  dockerRegistryServiceConnection: 'MyPrivateRegistry'
  imageRepository: 'MyPrivateRepositoryName'
  containerRegistry: 'MyPrivateRepositoryName.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'
  azureSubscription: 'MyPrivateSubscribtionName(5c4b9a4b-private-subscribtion-id-91503531e1a0)'
  appName: 'private_appname'
  resourceGroupName: 'PrivateResourceGroup'
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Push and Build
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: '$(dockerRegistryServiceConnection)'
        tags: |
          $(tag)
  - job: DeployToStaging
    displayName: Deploy To staging
    dependsOn: Build
    condition: succeeded()
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: AzureWebAppContainer@1
      inputs:
        azureSubscription: $(azureSubscription)
        appName: $(appName)
        deployToSlotOrASE: true
        resourceGroupName: $(resourceGroupName)
        slotName: 'staging'
        containers: '$(containerRegistry)/$(imageRepository):$(tag)'

项目已成功构建并按预期推送到私有(private)注册表。我可以验证新图像是否已使用新 tagId 推送。但是,我的容器无法启动并出现以下错误

Image pull failed since Inspect image returned null: MyPrivateRepositoryName.azurecr.io/MyPrivateRepositoryName:151

这是我看到的建议

Please check the repository name, image name, and container definitions defined by DOCKER_REGISTRY_SERVER_USERNAME, DOCKER_REGISTRY_SERVER_URL, and DOCKER_REGISTRY_SERVER_PASSWORD.

当我进入暂存槽配置时,我看到以下内容,并且值全部正确。启用管理员用户后,我从容器注册表服务中的“访问键”部分复制了这些值

我在这里缺少什么?如何获得插槽以从包含注册表中正确提取 docker 镜像?

enter image description here

已更新

更多地查看日志给了我这个错误

2022-04-01T20:36:21.409Z ERROR - Pull image threw Exception: Input string was not in a correct format.
2022-04-01T20:36:21.411Z INFO  - Pulling image from Docker hub: privateregistry.azurecr.io/privateimage:152
2022-04-01T20:36:21.594Z ERROR - DockerApiException: Docker API responded with status code=InternalServerError, response={"message":"Get https://privateregistry.azurecr.io/v2/privateimage/manifests/152: unauthorized: authentication required, visit https://aka.ms/acr/authorization for more information."}

听起来好像没有权限从存储库中提取 docker 镜像。问题是什么对象需要有这个权限?我是否可以向私有(private)存储库添加权限,或者是否可以将其添加到 Web 服务?

最佳答案

如果Enable Admin Access,您可以检查一些事情不起作用。

  • 您的应用服务与 ACR 属于同一订阅吗?如果没有,请尝试将其移至同一订阅。
  • 在 Azure 门户 -> 应用服务 -> 部署中心中,查看是否有冲突的设置。如果为空,请尝试在此处设置容器注册表信息,而不是将应用设置作为环境变量传递。

如果仍然有错误,我建议您创建另一个凭据来登录 ACR,而不是使用管理员凭据。 (为此,它不会涉及系统身份或托管身份)

  1. Register a new app in Azure将从 ACR 读取
  2. 在 ACR 中 -> 访问控制 -> 授予此应用 ACR PULL 权限
  3. 将您的应用服务应用设置替换为以下内容
  • DOCKER_REGISTRY_SERVER_USERNAME = 已创建应用的客户端 ID
  • DOCKER_REGISTRY_SERVER_PASSWORD = 已创建应用的客户端 key

关于azure - 无法从 Azure 注册表中的 docker 镜像运行 Azure Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71712286/

相关文章:

sql-server - 在租户中找不到 Active Directory 对象 ''

azure - 使用 AzureRmWebAppDeployment@4 将 net6 ASP 应用程序部署到 azure 应用程序服务时出现 SSL 错误

docker - 命令在 docker 容器内工作,但在 docker exec 下失败

bash - kubectl bash 完成在 ubuntu docker 容器中不起作用

Docker 私有(private)注册表 - 方法不允许 405

laravel - "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable"Azure 上的 Laravel

c# - Azure webjob调用内部服务

azure - 定义AAD中的应用范围

c# - IIS 因 Windows Azure 存储加密而随机挂起

azure - 授予 Datastore.register_azure_blob_container 的工作区访问权限 - 文档?