Day 62 - Automating Docker Containers with Terraform!
What You’ll Learn
Understanding Terraform Blocks and Resources.
Using Terraform to pull an Nginx Docker image and deploy a Docker container.
Running the setup step-by-step with appropriate commands.
Step-by-Step Tasks
Task 01: Set Up Terraform with the Docker Provider
Terraform needs to know which provider to use. This is specified using the required_providers block in your main.tf
file.
Create a
main.tf
File
Start by creating a Terraform configuration file:mkdir terraform-docker-setup cd terraform-docker-setup touch main.tf
Add the Terraform Block
This block tells Terraform to use the Docker provider plugin:terraform { required_providers { docker = { source = "kreuzwerker/docker" # Specify provider source version = "~> 2.21.0" # Compatible provider version } } }
Note:
kreuzwerker/docker
refers to the plugin location on Terraform’s registry.The version
~> 2.21.0
ensures compatibility with the latest versions of the Docker provider plugin.
Add the Provider Block
The provider block configures the Docker provider:provider "docker" {}
Run Terraform Init
Initialize the Terraform working directory to download the required provider:terraform init
Expected Output:
- Terraform will download the Docker provider plugin.
You can use this script to run nginx: Summarized
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker" # Specify provider source
version = "~> 2.21.0" # Compatible provider version
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest" # Specify the image name
keep_locally = false # Do not keep the image locally if not in use
}
resource "docker_container" "nginx" {
image = docker_image.nginx.image_id # Use the correct attribute for the image ID
name = "tutorial"
ports {
internal = 80
external = 80
}
}
This script will make you run nginx in your localhost:80 web browser
Commands used for execution: terraform init
terraform plan
terraform apply
Task 02: Create Resource Blocks for Nginx
Now you will define two resource blocks:
One for pulling the Nginx Docker image.
Another for running the Nginx container.
Step 1: Pull the Nginx Docker Image
Add the following resource block to your main.tf
file:
resource "docker_image" "nginx" {
name = "nginx:latest" # Specify the image name
keep_locally = false # Do not keep the image locally if not in use
}
Explanation:
docker_image
is the resource type for Docker images.nginx
is the logical name you give to this resource.name
specifies the image to pull from Docker Hub.keep_locally = false
ensures that unused images are removed.
Step 2: Create a Docker Container for Nginx
Now add this block to create an Nginx container:
resource "docker_container" "nginx" {
image = docker_image.nginx.latest # Use the pulled Nginx image
name = "tutorial" # Container name
ports {
internal = 80 # Port exposed inside the container
external = 80 # Port mapped to your host machine
}
}
Explanation:
docker_container
is the resource type for containers.nginx
is the logical name of the resource.image
references the latest Nginx image you just pulled.ports
maps port80
from the container to port80
on your local machine.
Task 03: Apply Terraform Configuration
Run Terraform Plan
Verify the configuration and preview the changes:terraform plan
Apply the Configuration
Deploy the Nginx container using Terraform:terraform apply
Expected Behavior:
Terraform will pull the Nginx image.
Terraform will run a container named
tutorial
exposing port80
.
Verify the Container
Check if the container is running:sudo docker ps
Expected Output:
You should see the container with the nametutorial
running and mapped to port80
.Test the Nginx Setup
Open a browser and visithttp://localhost:80
. You should see the default Nginx welcome page. 🎉
Optional: Docker Installation Commands
If Docker is not installed on your machine, run the following: this is for linux ubuntu, for windows you can follow different steps.
sudo apt-get update
sudo apt-get install docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo chown $USER /var/run/docker.sock # Grant Docker permissions to your user
Verify the installation: You can verify in your VS-code terminal also.
docker --version
docker ps