Day 62 - Terraform and Docker
Terraform needs to be told which provider to be used 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
Terraform block
Task-01
Create a Terraform script with Blocks and Resources
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.21.0"
}
}
}
This is a Terraform configuration block specifying the required Docker provider and its version.
In this configuration, you are specifying that your Terraform code requires the Docker provider to interact with Docker containers. The source parameter specifies the location of the provider code, which in this case is the kreuzwerker/docker GitHub repository. The version parameter specifies the minimum version of the provider that should be used.
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" {}
Resource
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
resource "docker_image" "nginx" {
name = "nginx:1.19.10"
keep_locally = false
}
Create a resource Block for running a docker container for nginx
resource "docker_container" "nginx" {
image = "nginx:1.19.10"
name = "tutorial"
ports {
internal = 80
external = 80
}
}
After creating a Terraform configuration file (with a .tf extension), use the following Terraform commands to provision and manage your infrastructure:
Terraform init: Initializes a new or existing Terraform working directory by downloading and installing any required providers and modules, initializing the backend, and downloading any necessary plugins.
terraform init
Terraform plan: Generates an execution plan that shows what actions Terraform will take to reach the desired state specified in the configuration file. This command also reports any changes that will be made to the infrastructure.
sudo terraform plan
Terraform apply: Executes the actions proposed in the execution plan generated by terraform plan. This command provisions and configures the infrastructure defined in the configuration file.
sudo terraform apply
In case Docker is not installed use below commands:
sudo apt-get install docker.io
sudo docker ps
sudo chown $USER /var/run/docker.sock
Check docker container is created using below command:
sudo docker ps
Browse public IP address, you can see nginx default page.
Thank you for reading!
Subscribe to my newsletter
Read articles from Nikhil Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nikhil Yadav
Nikhil Yadav
I am a highly motivated and enthusiastic individual completed B.Tech from Savitribai Phule University, Pune . With a strong interest in DevOps and Cloud technologies, I am eager to kick-start my career in this domain. Although I do not have much professional experience, I possess a willingness to learn, excellent problem-solving skills, and a passion for technology. I am committed to contributing my best to any team I work with.