DevOps(Day-62): Terraform and Docker ๐Ÿ”ฅ

DevOps(Day-62): Terraform and Docker ๐Ÿ”ฅ

ยท

3 min read

Terraform needs to be told which provider to be used 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 our main.tf

Blocks and Resources in Terraform

Terraform block

Task-01: Creating Provider block and resource.

  • Create a Terraform script with Blocks and Resources
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.21.0"
}
}
}

Terraform needs to be told which provider to be used in the automation, hence we need to give the provider name with source and version. Here we gave the source as kreuzwerker/docker which tells the repository from which the code is to be pulled and its version.

Note: kreuzwerker/docker, is shorthand for registry.terraform.io/kreuzwerker/docker.

Provider Block

The provider block configures the specified provider, in this case, docker. A provider is a plugin that Terraform uses to create and manage your resources.

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: Creating resource block for Docker image and container.

  • Create a resource Block for an nginx docker image
resource "docker_image" "nginx" {
 name         = "nginx:latest"
 keep_locally = false
}

  • Create a resource Block for running a docker container for Nginx.
resource "docker_container" "nginx" {
 image = docker_image.nginx.latest
 name  = "tutorial"
 ports {
   internal = 80
   external = 80
 }
}

Note: In case Docker is not installed

sudo apt-get install docker.io

sudo docker ps

sudo chown $USER /var/run/docker.sock

Now, the terraform file is created.

Let's execute the file.

  • Use the below command which will Initialize a new Terraform configuration in the current directory. This command will download and install all necessary plugins or modules needed to execute the configuration.

      terraform init
    

  • Use the below command which will show an execution plan of the changes that Terraform will make to infrastructure, without actually making those changes. This command helps to identify potential issues before applying infrastructure changes.

      terraform plan
    

  • Now use the below command which will apply the changes to the infrastructure defined in the Terraform configuration. This command creates, modifies, or deletes resources as necessary to bring the infrastructure into the desired state.

      terraform apply
    

  • Enter yes to apply the changes.

  • Now the docker container is created for the nginx image.

  • Navigate to the public URL to view the nginx default webpage.

Thanks for reading my article. Have a nice day.

You can follow me on LinkedIn for my daily updates:- linkedin.com/in/bandan-kumar-sahoo-131412203

ย