Learning Terraform:Launch NGINX using Docker on Windows

508 0

So, this weekend i decide to learn something new is my path to AWS professional certification. The best way to keep yourself up todate with the technology is not only to hear about things but also to have sme hands on things.

I personally been using cloudformation is Aws to automate or setup some web services ressource. These days i see that Terraform is becoming a buzz word and i decide to take a look.

I was then able to launch an nginx server on windows using docker. My next article will be using it in aws.

But First let me define Terraform : “Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.”

This mean terraform help you manage easily and safely your infrastructure.
In this i will be using VS code that you can download from Here

  • Installing terraform on windows
    • Download the windows version from https://www.terraform.io/downloads.html then unzip it to a folder of your choices. I have created my folder under C drive C:\Terraform
    • Open system environment variable and set the path to the location created previously
    • Make sure terrafor is working by running terraform –version
  • Install Docker windows
    Download Docker Desktop for Windows. Start Docker Desktop  after you installed Terraform and Docker on your local machine. Make sure docker is running.
  • create directory for this tutorial with VS code
    • mkdir terraform-docker-nginx
    • cd .\terraform-docker-nginx\

Lets create a terraform file that will specify what we need to do: In vs create a file name hellonginx.th and paste the following :

terraform {
  required_providers {
    docker = {
      source = "terraform-providers/docker"
    }
  }
}

provider "docker" {
  version = "~> 2.7"
  host    = "npipe:////.//pipe//docker_engine"
}

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
  }
}

What you need to understand in this code: Provider in our case docker, resource docker image for nginx and make sure to have the require provider set.
Save the file and we are set.

Go back to visual code terminal and run the following:

terraform init
terraform apply

THen make sure reply to a yes at the question.

You are set. Browse localhost:8000  and you will see a nice website live with NGINX

If you want to stop the container, run terraform destroy

Related Post