Configuring Docker Container in Local Environment Using Terraform

Configuring Docker Container in Local Environment Using Terraform

Introduction

In this blog, we will explore how to configure a Docker container in our local environment using Terraform. As Docker has revolutionized the development, shipping, and running of applications, it provides a consistent environment across the platforms.

What is Terraform?

As we discussed in IaC: Automation and Scaling the Infrastructure

Terraform is an infrastructure as a code tool that lets you build, change, and version infrastructure safely and efficiently. This includes low-level components like compute instances, storage, and networking; as well as high-level components like DNS entries and SaaS features.

Prerequisites

  1. Docker: To run and manage containers on your local machine.

  2. Terraform: An executable binary to execute Terraform commands.

Step 1: Create a Docker Configuration File

First, let's create a Docker configuration file to define the desired state of our container. Create a file named docker.tf and add the following code:

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

resource "docker_container" "my_container" {
  name  = "mycontainer"
  image = "nginx:latest"
  ports {
    internal = 80
    external = 8080
  }
}

In this configuration, we are using the Docker provider to manage our container. We define a resource docker_container with a name, image, and port configuration. You can customize with your configuration.

Step 2: Initialize Terraform

Next, initialize Terraform in the directory containing your Docker configuration file. Open your terminal and run the following command:

terraform init

This command will download the necessary provider plugins and prepare your workspace for provisioning.

Step 3: Plan the Infrastructure

Now, let's generate an execution plan to see what changes Terraform will apply. Run the following command:

terraform plan

Terraform will analyze your configuration and display a summary of the changes it will make. Make sure everything looks correct before proceeding.

Step 4: Apply the Changes

Finally, it's time to apply the changes and create our Docker container. Run the following command:

terraform apply

Terraform will prompt you to confirm the execution plan from the previous step. Review the changes and type 'yes' to proceed.

Once the command completes, Terraform will provision the Docker container according to your configuration.

Step 5: Verify the Container

To verify that the container is running successfully, Type the following docker command to view the running container:

docker ps

Step 6: Clean Up

After you have finished experimenting with the Docker container, it's good practice to clean up your resources. Run the following command to destroy the container:

terraform destroy

Terraform will remove the Docker container and free up any resources associated with it.

Conclusion

We have learned how to configure a Docker container in our local environment using Terraform. We started by creating a Docker configuration file, then initialized Terraform, planned and applied the changes, verified the container, and finally cleaned up our resources.

Terraform provides a convenient and scalable way to manage your containerized applications.

Resources

Docker Provider by Terraform-docs

Various Providers by Various Companies like AWS, Azure, GCP, Helm

Did you find this article valuable?

Support Jatin Chourasia by becoming a sponsor. Any amount is appreciated!