You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps中.NET7容器构建结合Docker@2推送镜像问题

使用dotnet publish构建镜像后,Docker@2推送任务的正确配置方法

核心问题根源

dotnet publish构建的镜像默认不带Azure容器注册表(ACR)的仓库前缀,而Docker@2推送任务默认会自动拼接服务连接中的ACR域名和随机生成标签,两者叠加就会出现双前缀、无效后缀的格式错误。


一、直接构建带ACR前缀的镜像(推荐方案)

在dotnet publish命令中直接指定完整的镜像标识(包含ACR域名前缀),避免后续手动补标签的步骤:

dotnet publish --os linux --arch x64 -c Release \
  -p:PublishProfile=DefaultContainer \
  -p:ContainerImageName=myregistry.azurecr.io/myproject-api \
  -p:ContainerImageTag=latest

通过ContainerImageName参数直接定义完整镜像名,ContainerImageTag指定固定标签(也可替换为流水线版本号如$(Build.BuildId))。


二、Docker@2推送任务的正确配置

不需要让任务自动生成标签或拼接仓库前缀,直接指定完整镜像标识即可,YAML配置示例:

- task: Docker@2
  displayName: 推送镜像到ACR
  inputs:
    command: 'push'
    containerRegistry: '你的ACR服务连接名称'
    repository: 'myregistry.azurecr.io/myproject-api'
    tags: |
      latest

注意事项:

  • repository字段必须填写完整的ACR镜像路径,不能只写myproject-api,否则任务会自动拼接服务连接中的ACR域名,导致双前缀问题。
  • tags字段填写你在dotnet publish中指定的标签,不要留空,避免任务生成随机后缀标签。

三、补全已有无前缀镜像的标签(兼容方案)

如果已经构建了不带前缀的myproject-api:latest,可在流水线中添加标签补全步骤后再推送:

- script: |
    docker tag myproject-api:latest myregistry.azurecr.io/myproject-api:latest
  displayName: 为镜像添加ACR前缀标签

- task: Docker@2
  displayName: 推送镜像到ACR
  inputs:
    command: 'push'
    containerRegistry: '你的ACR服务连接名称'
    repository: 'myregistry.azurecr.io/myproject-api'
    tags: |
      latest

内容的提问来源于stack exchange,提问作者rouen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 03:32:37