0

By now I am researching, how I could move my infrastructure to Azure using terraform. Where I am stuck by now is the following scenario: I have a docker image, that uses the copy command to add some files into it. One solution I could think of is that I first build this Image using docker, push it to azure registry and then using it as container inside the azrerm_container_group resource. Using the kreuzwerker/docker provider I could also just use the dockerfile, but could not find a equivalent solution in Azure. So, what would be your suggestions? Thanks in Advance!

the base for azure is:

resource "azurerm_container_group" "containers" {
  name                = "xxx"
  location            = xxx
  resource_group_name = xxx
  ip_address_type     = "Public"
  os_type             = "Linux"

  container {
    name   = "xxx"
    image  = "xx/xx:latest"
    cpu    = "1"
    memory = "1.5"
herrm
  • 1

1 Answers1

0

I tried in my environment with below code.

  • Check the name of the image given while pushing docker image to the registry.
  • Usually it will be pushed to dockerhub.
  • To push an image to Microsoft Container Registry add or prepend mcr.microsoft.com/ to the name of your container registry.

Push to the microsoft container registry

Code:

provider "docker" {
 # host = azurerm_container_registry.acr.login_server
  registry_auth {
    address  = azurerm_container_registry.acr.login_server
    username = azurerm_container_registry.acr.admin_username
    password = azurerm_container_registry.acr.admin_password
  }
}



resource "docker_registry_image" "helloworld" {
  name = "mcr.microsoft.com/azuredocs/aci-helloworld:latest"

  build {
    context    = "${path.cwd}/absolutePathToContextFolder"
    dockerfile = "Dockerfile"
  }
   
 }

and then try to store/use in the container group.

resource "azurerm_container_group" "example" {
  name                = "kaexample-container"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  ip_address_type     = "Public"
  dns_name_label      = "kavyaaci-label"
  os_type             = "Linux"

  container {
    name   = "hello-world"
    image  = "mcr.microsoft.com/azuredocs/aci-helloworld:latest"
    cpu    = "0.5"
    memory = "1.5"

    ports {
      port     = 443
      protocol = "TCP"
    }
  }

  tags = {
    environment = "testing"
  }
}

providers.tf

terraform {
  required_providers {
 

    azapi = {
      source  = "azure/azapi"
      version = "=0.1.0"
    }

    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.2"   
    }

    docker = {
      source  = "kreuzwerker/docker"
      version = ">= 2.16.0"
    }
}

Code executed:

terraform apply

enter image description here

Images in container:

enter image description here

Reference: Use terraform to push docker image to azure container registry - Stack Overflow

kavyaS
  • 8,026
  • 1
  • 7
  • 19