Day 62 of #90daysofdevops
Hey Techies! Welcome to this blog
In this blog, we are going to start with Docker Provider in Terraform.
Introduction
Modern infrastructure management involves the use of Infrastructure as Code (IaC) tools, providing a streamlined and automated approach to deploying and managing resources. Terraform, one such tool, enables users to define and provision infrastructure using a declarative configuration language. In this guide, we'll walk through the process of using Terraform to pull an NGINX Docker image and run a container, showcasing how Terraform simplifies the deployment of Dockerized applications.
Task-01: Create a Terraform Script with Blocks and Resources
1.1. Install Terraform
Begin by ensuring that Terraform is installed on your machine. You can download the latest version from the official Terraform website.
1.2. Create a Directory
Set up a new directory for your Terraform project.
mkdir TerraformDocker
cd TerraformDocker
1.3. Create Terraform Script
Create a new file named main.tf
within the directory and add the following content:
# main.tf
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.2"
}
}
}
provider "docker" {}
This script sets up the Terraform configuration with the necessary Docker provider.
Task-02: Create Resource Blocks for NGINX Docker Image and Container
2.1. NGINX Docker Image Resource Block
Add the following code to main.tf
to define the Docker image resource block:
# main.tf
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.2"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
This resource block specifies the NGINX Docker image with the latest tag.
2.2. NGINX Docker Container Resource Block
Now, add the resource block for running the NGINX Docker container:
# main.tf
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.2"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx"{
image = docker_image.nginx.latest
name = "ImageNginx"
ports {
internal = 80
external = 80
}
}
This resource block defines a Docker container based on the NGINX image, named "tutorial," with port mapping from internal (container) port 80 to external (host) port 80.
3- Execution
3.1. Initialize Terraform
Run the following command to initialize Terraform within your project directory:
terraform init
3.2. Apply Changes
Apply the changes to create the NGINX Docker image and container:
terraform apply -auto-approve
Terraform will prompt for confirmation; type yes
and press Enter.
3.3. Verify
Verify that the NGINX Docker container is running by accessing it through a web browser or using the following command:
docker ps
You should see a running container named "ImageDocker" with NGINX.
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)
Connect with me: LinkedIn