DevOps, Day - 62

DevOps, Day - 62

Terraform and Docker

Terraform needs to be told which provider to use in the automation, hence we need to give the provider name with source and version. For Docker, we can use this block of code in your main.tf

Task-01

  • Create a Terraform script with Blocks and Resources, I also added a provider block in it.
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.21.0"
}
}
}

provider "docker" {}

Resource

Use resource blocks to define components of your infrastructure. A resource might be a physical or virtual component such as a Docker container, or it can be a logical resource such as a Heroku application.

Resource blocks have two strings before the block: the resource type and the resource name. In this example, the first resource type is docker_image and the name is Nginx.

Task-02

Create a resource Block for an nginx docker image

# Define a Docker image resource block
resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

# Define a Docker container resource block
resource "docker_container" "nginx" {
  name  = "tutorial"
  image = docker_image.nginx.latest
  ports {
    internal = 8080
    external = 90
  }
}
terraform init

terraform apply

Check docker the created docker container

check-in browser


Video Course

Terraform can be tricky, so best to use a Free video Course for Terraform here


Thank you so much for reading

Follow me on LinkedIn to see interesting posts like this : )

Linkedin

Β