Terraform and Docker
Welcome to our blog, where we dive into the fascinating world of Terraform! In this space, we will explore Blocks and Resources in Terraform, offering insights and practical guidance to help you harness the full power of this infrastructure as code tool.
In our first task, we’ll guide you through creating a Terraform script with Blocks and Resources, including the essential Provider Block, as well as a Terraform Docker script.
Then, in our second task, we’ll focus on crafting a Resource Block for an Nginx Docker image, providing you with a hands-on approach to understanding Terraform’s capabilities. Join us as we embark on this Terraform journey, and let’s learn and grow together!
Terraform needs to be told which provider to use 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
Blocks and Resources in Terraform
A block is a section within a configuration file that defines a specific object or behavior. Blocks are used to organize and configure resources, providers, variables, outputs, and other components of a Terraform configuration.
A resource block is a specific type of block used to define and manage a resource within an infrastructure provider. It describes the desired state of a resource and instructs Terraform on how to create, update, or destroy that resource. A resource block typically includes properties such as the resource type, name, and configuration options specific to the provider.
Here’s an example of a resource block in a Terraform configuration file defining an Nginx image:
resource "docker_image" "my_nginx" {
name = "nginx:latest"
keep_locally = false
}
Task-01: Create a Terraform script with Blocks and Resources
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.
provider "docker" {}
Create a Terraform Docker script with Blocks and Resources
- Create a
terra-docker.tf
and pass the docker provider.
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.21.0"
}
}
}
- Note: kreuzwerker/docker, is a shorthand Docker installation. kreuzwerker docker registry
Resource Block
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
- Create a resource Block for an nginx docker image
resource "docker_image" "my_nginx" {
name = "nginx:latest"
keep_locally = false
}
- Create a resource Block for running a docker container for Nginx
resource "docker_container" "my_nginx_container" {
image = docker_image.my_nginx.name
name = "nginx-container"
ports {
internal = 80
external = 80
}
}
- Initializes a new or existing Terraform working directory by downloading the necessary provider plugins using
terraform init
command.
terraform init
- Now execute the
terraform plan
command which will create an execution plan by comparing the desired state in the configuration to the current state of the infrastructure.
terraform plan
- Execute the
terraform apply
command so all the configurations get executed.It creates, modifies, or deletes resources as necessary to achieve the desired state, based on the execution plan generated byterraform plan
.
terraform apply
- In case Docker is not installed use the below commands:
sudo apt-get install docker.io -y
sudo chown $USER /var/run/docker.sock
docker --version
- Check docker container is created using the below command:
docker ps
- Browse public IP address, you can see the Nginx default page.
- Execute the
terraform destroy
so it will prompt for confirmation and then proceeds to delete the resources, reverting the infrastructure to its pre-Terraform state.
In Day 62 of my ’90 Days of DevOps’ journey, I delved into the exciting world of Terraform Blocks and Resources. I acquired knowledge about the crucial elements that make up Terraform scripts:
- The Provider Block, which serves as the gateway to define the cloud or infrastructure provider for our resources. It’s the starting point that sets the stage for our infrastructure management.
- The Resource Block, which I explored in depth. This component allows us to create, configure, and manage specific infrastructure resources. In this task, I put this knowledge to use by creating a Resource Block to provision an Nginx Docker image, covering key details like image name, ports, and other configurations.
I’m thrilled to share my learning journey with you, and I’m always open to questions, discussions, and insights related to DevOps, Infrastructure as Code (IAC), or infrastructure management. If you’d like to connect with me and explore these topics further, please find me on (https://www.linkedin.com/in/mudit--mathur/). Your feedback and contributions are invaluable as we all strive to enhance our DevOps and infrastructure skills.
Subscribe to my newsletter
Read articles from Mudit Mathur directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mudit Mathur
Mudit Mathur
Mudit Mathur is an experienced DevOps Engineer with expertise in AWS, CI/CD, and automation. He delivers tangible results by streamlining processes and fostering collaboration. Reach out to him at linkedin.com/in/mudit-mathur-535786146 or muditmathur121@gmail.com for efficient DevOps solutions.