简体   繁体   English

通过 Terraform 将容器部署到 Azure - 构建自定义映像并在 azure 容器组中使用它的最佳实践

[英]deploying containers to Azure via Terraform - best practice to build custom image and use it in azure container group

By now I am researching, how I could move my infrastructure to Azure using terraform.到目前为止,我正在研究如何使用 Terraform 将我的基础设施迁移到 Azure。 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.我现在遇到的问题是以下场景:我有一个 docker 映像,它使用复制命令将一些文件添加到其中。 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.我能想到的一个解决方案是,我首先使用 docker 构建此图像,将其推送到 azure 注册表,然后将其用作 azrerm_container_group 资源中的容器。 Using the kreuzwerker/docker provider I could also just use the dockerfile, but could not find a equivalent solution in Azure.使用 kreuzwerker/docker 提供程序,我也可以只使用 dockerfile,但在 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"

I tried in my environment with below code.我在我的环境中尝试使用以下代码。

  • Check the name of the image given while pushing docker image to the registry.在将 docker 映像推送到注册表时检查给定的映像名称。
  • Usually it will be pushed to dockerhub.通常它会被推送到 dockerhub。
  • To push an image to Microsoft Container Registry add or prepend mcr.microsoft.com/ to the name of your container registry.要将映像推送到 Microsoft Container Registry,请将mcr.microsoft.com/添加或添加到您的容器注册表的名称中。

Push to the microsoft container registry推送到 Microsoft 容器注册表

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供应商.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地形应用

在此处输入图像描述

Images in container:容器中的图像:

在此处输入图像描述

Reference: Use terraform to push docker image to azure container registry - Stack Overflow参考: 使用 terraform 将 docker 镜像推送到 azure 容器注册表 - 堆栈内存溢出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM