Terraform: Build Infrastructure with Docker

Gani Çalışkan
Turk Telekom Bulut Teknolojileri
4 min readSep 24, 2023

What is Terraform?

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It is designed to automate and manage the provisioning and configuration of infrastructure resources, such as virtual machines, storage accounts, networking components, and more, in a declarative and version-controlled manner.

How to install Terraform for Windows?

First, you need to check your computer’s OS and then can able to take an action for installation. You can see the image below that provided with an example. You can download both versions from Binary download section.

Creating folder and file for Terraform

First, you need to open new folder on your desktop. You can see the example of the created folder on my desktop.

Important note !!! : Don’t forget to check your pathway of your directory on desktop. Cause you are going to enter commands considering your files. Secondly, after you download your binary versions for terraform, don’t forget to extract your files and copy the file. After you copy that file, you need to import on folder which you have created recently.

Check the version and access to Terraform

Here is the example of file that Terraform installed. Then you can check the version on cmd. You can see the latest version and windows one. Here is the example how terraform version can be demonstrated.

Define your Infrastructure

You need to download Visual studio code to design your code for Infrastructure and install extension for terraform file.

We created file with .tf format after open folder that we created earlier to apply terraform extension for code design. After you define the infrastructure, don’t forget to save the file and make sure that file is in correct folder.

Initialize the directory

Before you start your application, you need to make sure that your main.tf file in correct directory. You can use “dir” command to check the file.

Now, you can start to initialize with terraform init command.

Format and validate the configuration

Use “terraform fmt” and “terraform validate” commands to format and validate files respectfully.

Create infrastructure

Apply the configuration now with the “terraform apply” command. You will see that docker image and container created on your docker hub for nginx.

Inspect state

Use “terraform show” command to inspect the current state of infrastructure. You can check the image id of the container and observe on docker hub.

Manually Managing State

Terraform has a built-in command called “terraform state” for advanced state management. Use the “list” subcommand to list of the resources in your project's state.

Open the nginx web server

Let’s open the docker hub and click the port of our container. After you click the port(8000:80), if you see “Welcome to nginx!” page you have succesfully completed your task.

Thank you for reading my article :)

Resources

--

--