Terraform and Docker

Terraform and Docker

·

3 min read

Hello everyone welcome to another blog. We will now continue from where we left off. We have already installed Terraform on our system. Now we will see how do we actually use it.
Before proceeding make sure to install docker on your terminal.

sudo apt-get install docker.io 
sudo docker ps 
sudo chown $USER /var/run/docker.sock

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 block of Terraform configuration sets up the Docker provider and specifies its version. In simple terms, it tells Terraform that your code needs the Docker provider to work with Docker containers. The "source" parameter indicates where the provider code is located, which, in this case, is the kreuzwerker/docker GitHub repository. The "version" parameter sets the minimum version of the provider to 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: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
 }
}

Once you've crafted your Terraform configuration file (saved with a .tf extension), employ the following Terraform commands to set up and manage your infrastructure:

  1. Initialize Terraform:

     terraform init
    

    This command kickstarts a new or existing Terraform working directory. It downloads and installs necessary providers and modules, initializes the backend, and fetches any required plugins.

  2. Generate Execution Plan:

     terraform plan
    

    This command creates an execution plan detailing the actions Terraform will take to achieve the desired state outlined in the configuration file. It also highlights any changes to be made to the infrastructure.

  3. Apply Changes:

     terraform apply
    

    Execute the proposed actions outlined in the plan generated by terraform plan. This command handles the provisioning and configuration of the infrastructure defined in the configuration file.

Verify that a Docker container is created:

docker ps

Finally, by navigating to the public IP address, you'll be able to observe the default page of nginx.


Thanks for reading until here. See you in the next one. Happy Automating!