public

Deploying Nginx Docker containers with Terraform on MacOS

Terraform is an infrastructure as code tool that is open-source and provides capabilities to deploy resources on many cloud providers. By using declarative configuration files, state tracking, and a powerful CLI, Terraform allows for consistent and reliable management of cloud resources.

2 months ago

Latest Post NEW - ByteUnits Membership by Tyler Moon public

Terraform is an infrastructure as code tool that is open-source and provides capabilities to deploy resources on many cloud providers. By using declarative configuration files, state tracking, and a powerful CLI, Terraform allows for consistent and reliable management of cloud resources.

Docker is a container solution for quickly deploying tiny virtual machines. These small VMs contain only the needed dependencies to run a single application. Nginx is a reverse-proxy web server which is very effectively deployed as a Docker container.

In this tutorial we will run through how to install Terraform on MacOS (even though it is also available on Windows and Linux) and use it to deploy an Nginx container with a custom HTML site to a local Docker container

Prerequisites

Installing Terraform

To install Terraform on MacOS we will use the brew package management system. First make sure that the local brew install is up to date:

brew update

Depending on the number of dependencies you have installed this process may take a few minutes.

Then add the tap for Hashicorp (the makers of Terraform):

brew tap hashicorp/tap

After thats setup we can now install the Terraform CLI with:

brew install hashicorp/tap/terraform

After that run terraform version to verify the CLI was installed correctly

Docker Terraform File

Terraform runs off of declarative files to tell it what the expected state of the infrastructure should be. Using the Hashicorp specific language HCL, these files are a good mix of function and readability. In a new directory add the following Terraform file:

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

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
  }
  upload {
    source = "src/hello_world.html"
    file = "/usr/share/nginx/html/index.html"
  }
}

The top block terraform defines a docker source for the next block provider. The provider defines what source system you are wanting to deploy too. In this instance its "docker" but it could also be AWS, Google Cloud, Azure, or a whole list of other providers.

The next block resource "docker_image" "nginx" defines a new docker image called "nginx". The name property defines this image off the base Nginx image. This is like pulling a pre-built model off the shelf.

The final resource block defines the running container based on the image previously defined. The image defines what to run but the container is the actual running VM with the Nginx server running in it. The upload property allows for the uploading of a file from your local file system into the Docker container on startup. In order to display a simple "Hello World" HTML page add a sub directory src and then create a new file hello_world.html inside it.

<!-- /src/hello_world.html -->
<h1>Hello World</h1>

And now that we have the files setup in the last section we will look into running Terraform to actually create this Docker container.

Running Terraform

One of the major benefits to Terraform is the ability to plan out changes without making them. This takes advantage of the fact that Terraform will keep track of the state of the deployed instances, so that it can determine any differences between runs.

Run: terraform plan -out o.tfplan to create a plan and save it to a file.

After a plan is created then it can be applied and Terraform will make the changes to the infrastructure.

Run: terraform apply o.tfplan to apply the previously created plan file.

And with all luck the Docker Nginx container should be up and running on localhost:8000

While this is a simple example it lays the groundwork for more complex infrastructure deployments!

Summary

In this article, we saw how to install Terraform on MacOS, create a TF file and deploy a Docker container using Terraform.

Tyler Moon

Published 2 months ago