“Cara Menggunakan Terraform untuk membangun Kontainer Nginx Docker”
Daftar Isi
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
- OS Linux Rocky linux
- Sudah install terraform
- Sudah install docker
Cara Menggunakan Terraform untuk membangun Kontainer Nginx Docker
Membuat Infrastructur docker
- Buat Folder project1
mkdir -p project1 cd project1
- Buat file
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 } }
- Initial
terraform init terraform fmt terraform validate terraform apply terraform show terraform state list
localhost:8000
Cara Mengubah Infrastructur
- Buat folder aplikasi
- Kemudian masuk ke folder aplikasi
- Kemudian buat file main.tf
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 } }
- Initial
terraform init terraform apply
- Edit main.tf
external = 8080
- Simpan Perubahan
terraform apply terraform show
Destroy Infrastructur
- Destroy
terraform destroy
Menambahkan Variables
- Buat folder aplikasi
- Kemudian masuk ke folder aplikasi
- Kemudian buat file main.tf
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 } }
- Buat file
variables.tf
nano variables.tf variable "container_name" { description = "Value of the name for the Docker container" type = string default = "ExampleNginxContainer" }
- Edit main.tf
name = "tutorial" ubah jadi name = var.container_name
- Apply konfig
terraform apply terraform apply -var "container_name=YetAnotherName"
Menambahkan Query Data dengan Outputs
- Buat folder project1
- masuk ke project1
- Tambahkan main.tf dan variables.tf
# 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" }
- Apply
$ terraform init $ terraform apply
- Buat file outputs.tf
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 }
- simpan konfig
$ 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.
(Visited 139 times, 1 visits today)