Skip to main content

Deploying Docker Images Using Terraform

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Beginner

Our team is looking to use Terraform to manage infrastructure. We’ve been tasked with creating a demonstration to illustrate the usefulness of the technology. We're going to create a Terraform file that deploys a Nginx Docker image.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Deploying Docker Images Using Terraform

The Scenario

Our team is looking to use Terraform to manage infrastructure. We’ve been tasked with creating a demonstration to illustrate the usefulness of the technology.

We're going to create a Terraform file that deploys a Nginx Docker image.

Logging In

Use the IP address and credentials provided on the hands-on lab overview page, and log in with SSH to the server.

Create a Terraform File

Create main.tf:

[cloud_user@host]$ vi main.tf

The contents of the main.tf should be:

resource "docker_image" "nginx_image" {
  name = "nginx:latest"
}

Initialize Terraform

[cloud_user@host]$ terraform init

Plan the Deployment

Execute terraform plan:

[cloud_user@host]$ terraform plan -out=tf_image_plan

Apply the Terraform Plan

Apply the plan:

[cloud_user@host]$ terraform apply tf_image_plan

Conclusion

All we need to do, to confirm whether things worked or not, is either run docker image ls or a terraform show. Either will confirm that we've completed the task. Congratulations!