Skip to main content

Command Palette

Search for a command to run...

Day 62 - Terraform and Docker

Updated
2 min read

Task-01: Create a Terraform script with Blocks and Resources

In today's Terraform project, we'll explore how to combine the power of Terraform with Docker to manage containerized applications. Let's get started!

First, we need to define the required provider for Docker in our Terraform script:

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

Task-02 : Create a resource Block for an nginx docker image

Next, let's create a resource block to define an NGINX Docker image and a corresponding container:

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

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

resource "docker_container" "nginx" {
 image = docker_image.nginx.name
 name  = "tutorial"
 ports {
   internal = 80
   external = 80
 }

In this example, we're pulling the latest NGINX image and creating a container named "tutorial" with port mapping from internal port 80 to external port 80.

Before applying the Terraform script, ensure that Docker is installed on your system and run the following command to grant necessary permissions:

sudo chown $USER /var/run/docker.sock

Now, let's run the Terraform commands:

Run terraform plan to preview the changes that Terraform will make.

Run terraform apply to apply the changes and create the Docker container.

Once the process completes, your NGINX Docker image will be up and running, ready to serve your web applications.

That's all for today's Terraform and Docker integration. If you have any questions or need further assistance, feel free to comment below.

#Terraform #Docker #Containerization #InfrastructureAsCode #DevOps #CloudComputing #Automation #CloudDeployment #DevOpsJourney #ContinuousIntegration #ContinuousDelivery #CloudManagement #CloudInfrastructure #InfrastructureAutomation #DevOpsCulture

More from this blog

Ajay Patel

116 posts