Create Terraform Infrastructure with Docker
Tags: infrastructureascode, iac, terraform, docker
Published: 2022-09-20 18:24:25

### Objective 

This is a very simple lab to help you learn the Terraform workflow by deploying an NGINX Docker container and destroying it.

To use Terraform you will need to install it. HashiCorp distributes Terraform as a binary package. You can also install Terraform using popular package managers.

There are several ways of installing Terraform depending on your OS but we would only go through the manual method for windows but another way to install Terraform on Windows is using Chocolatey.
Chocolatey is a free and open-source package management system for Windows. It helps to Install the Terraform package from the command-line.

### Manual Installation

Retrieve the terraform binary by downloading a pre-compiled binary or compiling it from source.

You could either install from a precompiled binary or compile it yourself from source by cloning the [HashiCorp Terraform repository](https://github.com/hashicorp/terraform).

#### Pre-compiled binary

To install Terraform, find the [appropriate package](https://www.terraform.io/downloads.html?_gl=1*1ydoehw*_ga*MTEyNTk4NDkyNC4xNjYzMjA1MDgx*_ga_P7S46ZYEKW*MTY2MzIzOTgzNy41LjEuMTY2MzIzOTk0MC4wLjAuMA..) for your system and download it as a zip archive.

After downloading Terraform, unzip the package. Terraform runs as a single binary named terraform. Any other files in the package can be safely removed and Terraform will still function.

Finally, make sure that the terraform binary is available on your PATH. This process will differ depending on your operating system.

[This Stack Overflow article](https://stackoverflow.com/questions/1618280/where-can-i-set-path-to-make-exe-on-windows) contains instructions for setting the PATH on Windows through the user interface.

#### Verify the installation

Verify that the installation worked by opening a new terminal session and listing Terraform's available subcommands.

Use 

```
terraform -help
```

Having installed Terraform, we will provision an NGINX server in less than a minute using Docker on Mac, Windows, or Linux. If you don't have Docker for desktop installed on your system please follow this quick [installation guide](https://docs.docker.com/docker-for-windows/install)

Choose the guide relevant to your operating system.

### Infrastructure Creation

After you install Terraform and Docker on your local machine, start Docker Desktop.

Create a directory named learn-terraform-docker-container

```
mkdir learn-terraform-docker-container
```
Navigate into it

```
cd learn-terraform-docker-container
```

Create a file main.tf and using any file editor of your choice open the main.tf file.

```
touch main.tf
```

```
nano main.tf
```

Copy and paste the following configuration the file and save your changes.

```hcl
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.15.0"
    }
  }
}

provider "docker" {}

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

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

```
In the "Terminal" tab, initialize the project, which downloads a plugin that allows Terraform to interact with Docker. run the below command:

```
terraform init
```

![Terraform Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plwg7vxivdl1si84zxnm.png)

Provision the NGINX server container with apply. When Terraform asks you to confirm, type yes and press ENTER. Run the command:

```
terraform apply
```

![Terraform Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqn3ichsqkeyzc7cc6ld.png)

### Verify NGINX instance

To verify the existence of the NGINX container you could either visit localhost:8000 in your web browser or run docker ps to view the NGINX container running in Docker via Terraform.

```
docker ps
```

![Terraform Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjp3jwv7t3y946og86k3.png)

### Destroy resources

To stop the container and destroy the resources created in this tutorial, run terraform destroy. When Terraform asks you to confirm, type yes and press ENTER.

```
terraform destroy
```

![Terraform Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8dfb6g325vq5j6pdasm.png)

You have now provisioned and destroyed an NGINX webserver with Terraform.