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

如何获取Azure Pipeline中.NET 7构建的Docker镜像?

在Azure环境中访问Pipeline构建的容器镜像

你当前用dotnet publish /t:PublishContainer构建的镜像仅存在于Pipeline代理的Docker daemon中,并没有托管到Azure的镜像仓库服务。要在Azure环境中访问和使用这个镜像,核心是把镜像推送到Azure Container Registry (ACR)——Azure官方的容器镜像托管服务,之后就能在各类Azure服务中引用它。以下是具体流程:

1. 准备Azure Container Registry (ACR)

  • 先创建ACR实例,用Azure CLI的话:
    az acr create --resource-group <你的资源组名> --name <ACR名称> --sku Basic
    
  • 给你的Azure Pipeline服务主体分配ACR的AcrPush权限(用于推送镜像),CLI命令示例:
    az role assignment create --assignee <Pipeline服务主体ID> --scope <ACR的资源ID> --role AcrPush
    
    也可以直接在Azure门户的ACR「访问控制(IAM)」页面添加权限。

2. 修改Pipeline,将镜像推送到ACR

方式一:直接用dotnet publish推送

借助.NET 7的PublishContainer目标,直接在命令中指定ACR地址,让dotnet完成构建+推送:

dotnet publish -c Release --os linux --arch x64 /t:PublishContainer --container-registry <ACR名称>.azurecr.io --container-tag 1.0.0

执行后,镜像会直接推送到ACR,完整镜像路径为<ACR名称>.azurecr.io/testApp:1.0.0

方式二:手动用Docker命令推送

如果需要更灵活的控制,也可以分三步操作:

  1. 登录ACR:
    az acr login --name <ACR名称>
    
  2. 给本地镜像打ACR标签:
    docker tag testApp:1.0.0 <ACR名称>.azurecr.io/testApp:1.0.0
    
  3. 推送到ACR:
    docker push <ACR名称>.azurecr.io/testApp:1.0.0
    

3. 在Azure服务中引用镜像

镜像推送到ACR后,就能在各类Azure服务中使用:

  • Azure App Service:选择容器部署模式,指定ACR的镜像路径,配置ACR的访问凭证即可部署。
  • AKS(Azure Kubernetes Service):在Deployment的YAML配置中,将镜像地址设为<ACR名称>.azurecr.io/testApp:1.0.0,若AKS与ACR同租户且配置了信任关系,无需额外凭证就能拉取。
  • Azure Container Instances (ACI):创建ACI时直接指定ACR镜像路径,配置ACR的用户名/密码或托管身份,即可启动容器。

Azure环境中导出镜像为tar包的流程

如果需要把镜像导出为tar包保存:

  1. 在Pipeline代理或Azure虚拟机中拉取ACR镜像:
    docker pull <ACR名称>.azurecr.io/testApp:1.0.0
    
  2. 导出为tar包:
    docker save -o testApp.tar <ACR名称>.azurecr.io/testApp:1.0.0
    
  3. 可将tar包上传到Azure Blob Storage长期保存:
    az storage blob upload --container-name <存储容器名> --file testApp.tar --name testApp.tar --account-name <存储账户名>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 20:10:37