Terraform to provision Nginx Docker container

0 / 268
IAC terraform docker



In this post –  “Infrastructure as Code – Orchestration, Provisioning & Configuration Management (Ansible & Terraform) ” we learned what is Infrastructure as Code & what is terraform. 


In this post – Install DevOps IaC tool “Terraform” on Centos 7 we learned how to install Terraform on Centos 7, having Linux OS.


In this article we will provision an Nginx container using docker on localhost Centos 7, having Linux OS.


IAC terraform docker

IAC terraform docker


Pre-requisites :

1. VM with Centos7 and docker installed.


Check if docker is installed and if not installed install it and restart it :

[root@devops ~]# hostname
[root@devops ~]# yum -y install docker ; systemctl enable docker ; systemctl restart docker


Create main.tf in terraform-nginx-local directory


[root@devops ~]# hostname
[root@devops ~]# mkdir terraform-nginx-local
[root@devops ~]# cd terraform-nginx-local
[root@devops terraform-nginx-local]# pwd

[root@devops terraform-nginx-local]# cat main.tf

resource “docker_image” “nginx” {
name = “docker.io/nginx”

resource “docker_container” “nginx” {
image = “docker.io/nginx:latest”
name = “nginx-tf”
ports {
internal = 80
external = 80

[root@devops terraform-nginx-local]#


Now we initialize the project using “terraform init”


[root@devops terraform-nginx-local]# terraform init


When initilization is completed you can see output as given below :


Terraform init

Terraform init


Next we provision the Nginx container using “terraform apply”. When prompted to confirm, type yes & press ENTER.


[root@devops terraform-nginx-local]# terraform apply


An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create


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)
+ entrypoint = (known after apply)
+ env = (known after apply)
+ exit_code = (known after apply)
+ gateway = (known after apply)
+ hostname = (known after apply)
+ id = (known after apply)
+ image = (known after apply)
+ ip_address = (known after apply)
+ ip_prefix_length = (known after apply)
+ ipc_mode = (known after apply)
+ log_driver = “json-file”
+ logs = false
+ must_run = true
+ name = “tutorial”
+ network_data = (known after apply)
+ read_only = false
+ restart = “no”
+ rm = false
+ shm_size = (known after apply)
+ start = true

+ labels {
+ label = (known after apply)
+ value = (known after apply)

+ ports {
+ external = 80
+ internal = 80
+ ip = “”
+ protocol = “tcp”

# docker_image.nginx will be created
+ resource “docker_image” “nginx” {
+ id = (known after apply)
+ latest = (known after apply)
+ name = “nginx:latest”

Plan: 2 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: yes


Check that nginx docker container is running :

[root@devops terraform-nginx-local]# docker ps
b3fda24188a8 docker.io/nginx:latest “nginx -g ‘daemon …” 2 minutes ago Up 2 minutes>80/tcp nginx-tf


User curl to load nginx defualt website :
[root@devops terraform-nginx-local]# curl http://localhost
<!DOCTYPE html>
<title>Welcome to nginx!</title>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>


<p>For online documentation and support please refer to
<a href=”http://nginx.org/”>nginx.org</a>.<br/>
Commercial support is available at
<a href=”http://nginx.com/”>nginx.com</a>.</p>


<p><em>Thank you for using nginx.</em></p>
[root@devops terraform-nginx-local]#


we can also browse the IP of our localhost and check that defualt ngnix website is loading fine:


terraform nginx

terraform nginx


At last we can destroy nginx container using terraform destroy

[root@devops terraform-nginx-local]# terraform destroy


When image is destroyed we can see message :


Destroy complete! Resources: 1 destroyed.



Subscribe and follow Golibrary on Facebook and Linkedin to get all the updates.





***Linux, Cloud & Devops Architect & Technical Content Writer*** I am a Linux Enthusiast and Supporter/Promoter of Open Source Technology with over 12+ years of experience in Linux, Cloud and Devops. I am A Technical Content writer for various sites like : Hostbread & Golibrary

Related Posts