Navigation

'Hello World!' web page using Terraform

Terraform is a widely used Infrastructure as Code tool for provisioning infrastructure. WitCentre is taking some steps into delivering useful contents on Terraform from the beginning. With that vision, this particular blog will simply explain how to do some deployments through Terraform. Similar to all other places, 'Hello World!' sample will be a good start and will be quite different from other situations. Make sure to read all steps to have some joy with it!

Prerequisites

If you are practicing the content of this blog, make sure to meet following prerequisites beforehand. Trust me, this will be a fun of leaning!

  • Docker
  • Terraform

NOTE: Following steps are dedicated for a Ubunutu environment. Make sure to adjust them for your environment. 

What will be doing?

This blog provides steps to create a Docker container locally through Terraform. The container will hold a simple 'Hello World!' web page which will be served through a NGINX web server. Thus, you will be accessing localhost:8003 (Click on the link now to see whether the port is available. If not, please use an available port in the step 2 .tf file below) through your browser to see the page.

Let's jump in!

Step 1: Build a Docker image locally

We will be using an existing local image to create the Docker container through Terraform. Therefore, let's create that Docker image first. Just follow following steps.

1. Create a new directory as witcentre-terraform-1
mkdir witcentre-terraform-1
2. Create another new directory inside it as docker 
cd witcentre-terraform-1 && mkdir docker
3. Create three new files as Dockerfile, index.html, and witcentre.com.conf inside the 'docker' directory with following contents respectively.

# Dockerfile
FROM nginx
COPY index.html /usr/share/nginx/witcentre/index.html

COPY witcentre.com.conf /etc/nginx/conf.d/witcentre.com.conf
# index.html
<!DOCTYPE html>
  <html>
    <head>
      <h1>Hello World!</h1>
  </head>
</html>
# witcentre.com.config
server {
  listen 8003;
  location / {
    root /usr/share/nginx/witcentre;
  index index.html index.html;
  }
}

4. Build the docker image using the following command. Use it inside the same 'docker' directory.

docker build -t hello-world-nginx .

Make sure the built docker image is available locally and continue with the step 2 for creating the container through Terraform.

Step 2: Create a docker container through Terraform

1. Create another directory as terraform inside 'witcentre-terraform-1' created above or at any place. Then create a hello-world-nginx.tf file inside that with the following content.
terraform {
    required_providers {
        docker = {
            source = "kreuzwerker/docker"
            version = "~> 3.0.1"
       }
    }
}

data "docker_image" "nginx" {
    name = "hello-world-nginx"
}

resource "docker_container" "nginx" {
    image = data.docker_image.nginx.id
    name = "witcentre-tutorial"

    ports {
        internal = 8003
        external = 8003
    }
}
This is the required Terraform resource and let's create it through just 2 Terraform commands.

2. Execute the following command to initialize Terraform configuration while being inside the 'terraform' directory.
terraform init

3. Excute following command to continue with creating the required container.

terraform apply 

This will show you the Terraform diff and ask whether every thing looks good to continue. Following is a similar output for this.
Terraform will perform the following actions:

  # docker_container.nginx will be created
  + resource "docker_container" "nginx" {
      + attach                                      = false
      + bridge                                      = (known after apply)
      + command                                     = (known after apply)
      + container_logs                              = (known after apply)
      + container_read_refresh_timeout_milliseconds = 15000
      + entrypoint                                  = (known after apply)
      + env                                         = (known after apply)
      + exit_code                                   = (known after apply)
      + hostname                                    = (known after apply)
      + id                                          = (known after apply)
      + image                                       = "sha256:bb986769a8f8b7c7d72a0aaba3894c74265a031fb81e6504debedc0ee14f8619"
      + init                                        = (known after apply)
      + ipc_mode                                    = (known after apply)
      + log_driver                                  = (known after apply)
      + logs                                        = false
      + must_run                                    = true
      + name                                        = "witcentre-tutorial"
      + network_data                                = (known after apply)
      + read_only                                   = false
      + remove_volumes                              = true
      + restart                                     = "no"
      + rm                                          = false
      + runtime                                     = (known after apply)
      + security_opts                               = (known after apply)
      + shm_size                                    = (known after apply)
      + start                                       = true
      + stdin_open                                  = false
      + stop_signal                                 = (known after apply)
      + stop_timeout                                = (known after apply)
      + tty                                         = false
      + wait                                        = false
      + wait_timeout                                = 60

      + ports {
          + external = 8003
          + internal = 8003
          + ip       = "0.0.0.0"
          + protocol = "tcp"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value:
Enter yes to continue with and it will create the container. If it is successful, a similar output as below will be appeared.

Step 3: Access localhost:8003 to see the message

Enter http://localhost:8003 in your browser and it should show 'Hello World!' page as below.

Conclusion

We have created Docker container locally through Terraform where it contains a NGINX web server with a 'Hello World!' web page. Terraform resources have been declared using HCL declarative language which is aiming for Terraform 0.13 or a above version. Docker Terraform provider from the kreuzwerker publisher has been used in this sample. Two mostly used Terraform object types such as data and resource were used. 

We are in the process of publishing useful Terraform contents here from WitCentre. Beginners are welcome to keep in touch with initial blogs. Later parts will be hopefully useful for anyone! If you have any specific requirements, do not hesitate to contact us through the Contact Us form, and leave a comment to this post.
Share

WitCentre

Post A Comment:

0 comments: