# #90daysofdevop #62 - Terraform and Docker

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)

## Blocks and Resources in Terraform

## Terraform block

## Task-01

* 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://registry.terraform.io/kreuzwerker/docker).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917113314/548afe64-8e81-4e0c-916d-f03a7e596439.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/v1683917158263/c294c137-9647-403e-9c4c-8df28b5eee13.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-02

* Create a resource Block for an nginx docker image
    

Hint:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917242000/df9952a5-3103-4d16-8edf-bb0549d45d3f.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/v1683917293168/a87b4ac5-d954-47e9-8aae-9ba7a196b809.png?auto=compress,format&format=webp align="left")

Note: In case Docker is not installed

`sudo apt-get install` [`docker.io`](http://docker.io)

`sudo docker ps`

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917459594/b63fb328-34b8-409c-afa4-61f32326ebfa.png?auto=compress,format&format=webp align="left")

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.
    
    ```plaintext
      terraform init
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917567790/a52b4edf-6e3a-4195-9b79-6cc7d0f0bce9.png?auto=compress,format&format=webp align="left")
    
* 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.
    
    ```plaintext
      terraform plan
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917594870/749aedb3-0ed0-494b-b87e-f2b8de5ad27c.png?auto=compress,format&format=webp align="left")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917628792/8f86a964-a593-4210-9157-54030572badf.png?auto=compress,format&format=webp align="left")
    
* 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.
    
    ```plaintext
      terraform apply
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917661696/079298c5-5b14-47dd-a976-7d7e8acbfeea.png?auto=compress,format&format=webp align="left")
    
* Enter yes to apply the changes.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917692245/95235e4c-77dd-44c4-a849-1f131a41c0ba.png?auto=compress,format&format=webp align="left")
    
* Now the docker container is created for the nginx image.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683917713766/868a6fb9-9375-4335-a18d-bbc955397422.png?auto=compress,format&format=webp align="left")
