# Day 62 - Terraform and Docker 🔥

![Automate your Docker Container Deployment with Terraform [For Mac OS] | by  Tharindu Abeysekara | Medium](https://miro.medium.com/v2/resize:fit:1400/1*_t3aveXd1dpE_d4_JxPN7w.png align="left")

## **Introduction**

Terraform, an open-source Infrastructure as Code (IaC) tool, empowers users to define and provision infrastructure using a declarative configuration language. In this guide, we will walk through creating a Terraform script to efficiently manage Docker containers and images, with a specific focus on setting up and running an Nginx container.

## **Task-01: Provider Block**

The initial step is to configure the Docker provider using the `provider` block in the Terraform script. This block specifies the plugin and its version that Terraform should utilize for managing resources.

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

provider "docker" {}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706170476403/307be419-28c0-464d-b8f6-44a8c8c96731.png align="center")

In this script snippet, we declare that the Terraform script necessitates the Docker provider from the specified source and version. The `provider` block configures the Docker provider for managing Docker resources.

## **Task-02: Resource Blocks for Nginx Docker Image and Container**

Next, let's create resource blocks to define the Nginx Docker image and the container that will run the Nginx application.

### **Docker Image Resource Block**

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706174239247/3a4d57f3-59b2-4cf9-9579-6669ee9e35c2.png align="center")

In this resource block, we specify that a Docker image named "nginx:latest" should be utilized. The `keep_locally` attribute is set to `false`, indicating that the image should not be kept locally.

### **Docker Container Resource Block**

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706170563981/290e00e0-6424-42e1-9fc5-983695871c31.png align="center")

Here, we define a Docker container named "tutorial" based on the Nginx image specified earlier. Additionally, we specify that the container should map port 80 from the internal network to port 80 externally.

## **Task-03: Docker Installation (If not installed)**

In case Docker is not installed on the system where Terraform will be executed, the following commands can be used to install Docker:

```plaintext
bashCopy codesudo apt-get install docker.io
sudo docker ps
sudo chown $USER /var/run/docker.sock
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706170813535/a8d9588e-8874-43e7-a005-ab1c3e599eff.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706174083558/da786f8e-c957-4ff6-b37d-12478c8c1dce.png align="center")

These commands install Docker, verify running Docker processes, and adjust permissions for the Docker socket.

## **Conclusion**

With the provided Terraform script, managing Docker resources for an Nginx container becomes straightforward. The script ensures the Docker provider is configured correctly, the Nginx Docker image is defined, and a container based on that image is set up with the specified configurations. Customize the script as needed to meet your specific infrastructure requirements.

Happy Terraforming and Dockering!

Lokesh Telange ✨✨
