Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 20, 2022 06:24 pm GMT

Create Terraform Infrastructure with Docker

Objective

This is a very simple lab to help you learn the Terraform workflow by deploying an NGINX Docker container and destroying it.

To use Terraform you will need to install it. HashiCorp distributes Terraform as a binary package. You can also install Terraform using popular package managers.

There are several ways of installing Terraform depending on your OS but we would only go through the manual method for windows but another way to install Terraform on Windows is using Chocolatey.
Chocolatey is a free and open-source package management system for Windows. It helps to Install the Terraform package from the command-line.

Manual Installation

Retrieve the terraform binary by downloading a pre-compiled binary or compiling it from source.

You could either install from a precompiled binary or compile it yourself from source by cloning the HashiCorp Terraform repository.

Pre-compiled binary

To install Terraform, find the appropriate package for your system and download it as a zip archive.

After downloading Terraform, unzip the package. Terraform runs as a single binary named terraform. Any other files in the package can be safely removed and Terraform will still function.

Finally, make sure that the terraform binary is available on your PATH. This process will differ depending on your operating system.

This Stack Overflow article contains instructions for setting the PATH on Windows through the user interface.

Verify the installation

Verify that the installation worked by opening a new terminal session and listing Terraform's available subcommands.

Use

terraform -help

Having installed Terraform, we will provision an NGINX server in less than a minute using Docker on Mac, Windows, or Linux. If you don't have Docker for desktop installed on your system please follow this quick installation guide

Choose the guide relevant to your operating system.

Infrastructure Creation

After you install Terraform and Docker on your local machine, start Docker Desktop.

Create a directory named learn-terraform-docker-container

mkdir learn-terraform-docker-container

Navigate into it

cd learn-terraform-docker-container

Create a file main.tf and using any file editor of your choice open the main.tf file.

touch main.tf
nano main.tf

Copy and paste the following configuration the file and save your changes.

terraform {  required_providers {    docker = {      source  = "kreuzwerker/docker"      version = "~> 2.15.0"    }  }}provider "docker" {}resource "docker_image" "nginx" {  name         = "nginx:latest"  keep_locally = false}resource "docker_container" "nginx" {  image = docker_image.nginx.latest  name  = "tutorial"  ports {    internal = 80    external = 8000  }}

In the "Terminal" tab, initialize the project, which downloads a plugin that allows Terraform to interact with Docker. run the below command:

terraform init

Terraform Docker

Provision the NGINX server container with apply. When Terraform asks you to confirm, type yes and press ENTER. Run the command:

terraform apply

Terraform Docker

Verify NGINX instance

To verify the existence of the NGINX container you could either visit localhost:8000 in your web browser or run docker ps to view the NGINX container running in Docker via Terraform.

docker ps

Terraform Docker

Destroy resources

To stop the container and destroy the resources created in this tutorial, run terraform destroy. When Terraform asks you to confirm, type yes and press ENTER.

terraform destroy

Terraform Docker

You have now provisioned and destroyed an NGINX webserver with Terraform.


Original Link: https://dev.to/securityminded/create-terraform-infrastructure-with-docker-3n8j

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To