Search code examples
azuredockerterraformdocker-image

How to push a docker image to Azure container registry using terraform?


I am a beginner in Terraform/Azure and I want to deploy a docker image in ACR using terraform but was unable to find internet solutions. So, if anybody knows how to deploy a docker image to an azure container registry using Terraform, please share. Tell me whether this is possible or not.


Solution

  • You may use Terraform resource null_resource and execute your own logic in Terraform.

    Example:

    resource "azurerm_resource_group" "rg" {
      name     = "example-resources"
      location = "West Europe"
    }
    
    resource "azurerm_container_registry" "acr" {
      name                     = "containerRegistry1"
      resource_group_name      = azurerm_resource_group.rg.name
      location                 = azurerm_resource_group.rg.location
      sku                      = "Premium"
      admin_enabled            = true
      georeplication_locations = ["East US", "West Europe"]
    }
    
    resource "azurerm_azuread_application" "acr-app" {
      name = "acr-app"
    }
    
    resource "azurerm_azuread_service_principal" "acr-sp" {
      application_id = "${azurerm_azuread_application.acr-app.application_id}"
    }
    
    resource "azurerm_azuread_service_principal_password" "acr-sp-pass" {
      service_principal_id = "${azurerm_azuread_service_principal.acr-sp.id}"
      value                = "Password12"
      end_date             = "2022-01-01T01:02:03Z"
    }
    
    resource "azurerm_role_assignment" "acr-assignment" {
      scope                = "${azurerm_container_registry.acr.id}"
      role_definition_name = "Contributor"
      principal_id         = "${azurerm_azuread_service_principal_password.acr-sp-pass.service_principal_id}"
    }
    
       resource "null_resource" "docker_push" {
          provisioner "local-exec" {
          command = <<-EOT
            docker login ${azurerm_container_registry.acr.login_server} 
            docker push ${azurerm_container_registry.acr.login_server}
          EOT
          }
        }