# Day 62 - Terraform and Docker 🔥   (Jan 27, 2024)

## **🙏 Introduction:**

In this blog, we will create a Terraform script to manage Docker containers and images, specifically focusing on setting up and running an Nginx container.

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 your [main.tf](http://main.tf)

## **Terraform block**

# **🎯Task: 1**

* ### Create a Terraform script with Blocks and Resources
    

```plaintext
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.21.0"
}
}
}
```

Note: kreuzwerker/docker, is shorthand for [registry.terraform.io/kreuzwerker/docker](http://main.tf)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704737562460/58f8b4c6-4cb9-4f97-b1a9-4c08da1f8cc4.png?auto=compress,format&format=webp align="left")

## **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.
    

```plaintext
provider "docker" {}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704737748568/4e15972b-4dbb-49e2-9eb9-fdaee52d0de6.png?auto=compress,format&format=webp align="left")

## **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: 2**

* ### **Create a resource Block for an nginx docker image**
    

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704738026365/4a92b3e5-e9e1-4dd3-9f05-f2c4b07eeb39.png?auto=compress,format&format=webp align="left")

* ### **Create a resource Block for running a docker container for nginx**
    

```plaintext
resource "docker_container" "nginx" {
 image = docker_image.nginx.latest
 name  = "tutorial"
 ports {
   internal = 80
   external = 80
 }
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704738312671/c505b19a-dbaa-481e-846f-455f82f3775f.png?auto=compress,format&format=webp align="left")

We have created terraform configuration file, now we will use the following Terraform commands to provision and manage our infrastructure

* `terraform init`**:** Initializes a new or existing Terraform working directory by downloading and installing any required providers and modules, initializing the backend, and downloading any necessary plugins.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704738981898/57aa53e4-5340-4e41-a15c-02c25159b9f3.png?auto=compress,format&format=webp align="left")

* `terraform plan`**:** Generates an execution plan that shows what actions Terraform will take to reach the desired state specified in the configuration file. This command also reports any changes that will be made to the infrastructure
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704739160070/29378eed-111c-4f22-a3fd-4980405012da.png?auto=compress,format&format=webp align="left")

* `terraform apply`**:** Executes the actions proposed in the execution plan generated by terraform plan. This command provisions and configures the infrastructure defined in the configuration file.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704739259450/0ce8a45d-8db8-4549-aecd-daa03bb53001.png?auto=compress,format&format=webp align="left")

* Browse public IP address of EC2 instance
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704739422651/f07b2ab3-bdab-4578-9b70-ae87f51b6123.png?auto=compress,format&format=webp align="left")
