Terraform and Docker

Terraform and Docker

How to Create Provider and Resource Blocks for an Nginx Docker Image in Terraform

Terraform is a popular Infrastructure as Code (IaC) tool that allows you to define, provide, and provision infrastructure resources in a descriptive manner. When working with Docker and Terraform, you often deal with two main concepts: the "provider" and the "resource" blocks. In this blog post, we will cover how to define a provider block for Docker and a resource block to create an Nginx Docker container.

1. Setting Up Terraform

Before we start, make sure you have Terraform and Docker installed on your machine. If not, you can download and install them from my recent blogs: https://kshitijaa.hashnode.dev/configuring-terraform-on-an-aws-ec2-ubuntu-instance https://kshitijaa.hashnode.dev/minikube-installation-guide-for-ubuntu

Create file docker_terraform.tf and keep adding below mentioned content

2. Defining the Provider Block

The provider block configures the named provider, in our case "Docker."

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

Here, we're telling Terraform to use the local Docker daemon to manage containers. If you're using a remote Docker host, replace the host value accordingly.

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" {}

3. Creating the Resource Block for the Nginx - Docker 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
 }
}

With the provider set up, we can now define the resource that we want Terraform to manage. In this case, it's an Nginx container.

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

resource "docker_container" "nginx" {
  name  = "nginx_container"
  image = docker_image.nginx.latest

  ports {
    internal = 80
    external = 8080
  }
}

Here’s a breakdown of what's happening:

  • docker_image: This block tells Terraform to ensure that the latest Nginx image is pulled from DockerHub.

  • docker_container: This block tells Terraform to ensure there's a running container named "nginx_container" based on the docker_image.nginx.latest image. We're also mapping port 80 from inside the container to port 8080 on the host.

4. Provisioning the Infrastructure

With the Terraform file ready, you can now initialize and apply the configuration

Use the command terraform init 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 command terraform plan 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 command terraform apply 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

This will prompt you to confirm that you want to create the resources defined in your configuration. Type yes and press enter. Terraform will then reach out to Docker, pull the Nginx image (if it's not already present), and start a new container.

5. Accessing the Nginx Container

docker ps

With the Nginx container up and running, you can access it by navigating to http://localhost:8080 in your web browser. You should see the Nginx welcome page.

Conclusion

Using Terraform with Docker simplifies the process of managing containers as part of your infrastructure. With the provider and resource blocks, you can easily describe and provision an Nginx Docker container, among many other types of resources. Happy Terraforming!

Thanks for reading! πŸ˜ƒπŸ™

You can follow me on LinkedIn for more updates:- https://www.linkedin.com/in/kshitija-bartakke-malwade-39678b141/

Β