Site icon Learning & Doing

Cara Menggunakan Terraform untuk membangun Kontainer Nginx Docker

docker

“Cara Menggunakan Terraform untuk membangun Kontainer Nginx Docker”

Pengantar

Setelah sebelumnya saya membahas mengenai Cara Install Terraform pada Rocky Linux 8 | Almalinux 8 , kali ini kita coba mulai membuat infratruktur docker kontainer dengan terraform.

Provide

Cara Menggunakan Terraform untuk membangun Kontainer Nginx Docker

Membuat Infrastructur docker

mkdir -p project1
cd project1
nano main.tf

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}
terraform init
terraform fmt
terraform validate
terraform apply
terraform show
terraform state list
localhost:8000

Cara Mengubah Infrastructur

nano main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}
terraform init
terraform apply
external = 8080
terraform apply
terraform show

Destroy Infrastructur

terraform destroy

Menambahkan Variables

nano main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8080
  }
}
nano variables.tf

variable "container_name" {
  description = "Value of the name for the Docker container"
  type        = string
  default     = "ExampleNginxContainer"
}
name  = "tutorial"
ubah jadi
name  = var.container_name
terraform apply
terraform apply -var "container_name=YetAnotherName"

Menambahkan Query Data dengan Outputs

# main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = var.container_name
  ports {
    internal = 80
    external = 8080
  }
}

# variables.tf

variable "container_name" {
  description = "Value of the name for the Docker container"
  type        = string
  default     = "ExampleNginxContainer"
}
$ terraform init
$ terraform apply
output "container_id" {
  description = "ID of the Docker container"
  value       = docker_container.nginx.id
}

output "image_id" {
  description = "ID of the Docker image"
  value       = docker_image.nginx.id
}
$ terraform apply
$ terraform output

Penutup

Sahabat Blog Learning & Doing demikianlah penjelasan mengenai Cara Menggunakan Terraform untuk membangun Kontainer Nginx Docker. Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.

Exit mobile version