我正在尝试通过在 Azure DevOps 中创建执行以下操作的管道来自动化部署过程
- 构建我的项目,创建 Docker 镜像,然后将该镜像推送到私有(private) Azure 注册表服务。
- 将镜像部署到 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 镜像?
已更新
更多地查看日志给了我这个错误
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,而不是使用管理员凭据。 (为此,它不会涉及系统身份或托管身份)
- Register a new app in Azure将从 ACR 读取
- 在 ACR 中 -> 访问控制 -> 授予此应用 ACR PULL 权限
- 将您的应用服务应用设置替换为以下内容
- DOCKER_REGISTRY_SERVER_USERNAME = 已创建应用的客户端 ID
- DOCKER_REGISTRY_SERVER_PASSWORD = 已创建应用的客户端 key
关于azure - 无法从 Azure 注册表中的 docker 镜像运行 Azure Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71712286/