Terraform and Docker Day 1.1: Building on Infrastructure as Code Basics
Shrikar Lonkar
DevOps Engineer@HeroVired ??Kubernetes, Docker, Terraform, Ansible ????? | CI/CD with Jenkins ?? | Git & GitHub ???? | AWS Cloud Explorer ??
Recap: Terraform Day 1
In Day 1, we laid the foundation by understanding Terraform basics, including blocks, arguments, and the execution lifecycle. Now, let's seamlessly integrate Docker into our Terraform workflow.
Create a new file:
If you don't have a main.tf file, create a new file and name it main.tf.
Terraform Configuration for Docker??
To begin, we need to inform Terraform about the Docker provider. Update your main.tf file with the following code:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~>2.21.0"
}
}
}
provider docker {}
This simple configuration sets the stage for connecting Terraform with Docker.
Let's break down each line in the given Terraform configuration:
terraform {: This is the start of the Terraform configuration block. Anything inside this block is related to the general configuration of Terraform.
required_providers {: Within the Terraform configuration block, required_providers is used to specify the providers (plugins) that Terraform needs for managing resources. In this case, we are specifying the Docker provider.
docker = {: Here, we are specifying that we need the Docker provider. The content within the curly braces {} is a configuration block specific to the Docker provider.
source = "kreuzwerker/docker": This line tells Terraform where to find the Docker provider. In this case, the source is set to "kreuzwerker/docker," which is shorthand for the Docker provider on the Terraform Registry.
version = "~>2.21.0": This line specifies the version constraint for the Docker provider. It means that Terraform should use any version of the Docker provider that is greater than or equal to 2.21.0 but less than the next major version.
Docker Resources in Terraform
Now, let's define Docker resources using Terraform. We'll create a straightforward configuration to pull an Nginx image and run a container.
resource docker_image "nginx" {
name = "nginx:latest"
keep_locally = "false"
}
resource "docker_image" "nginx" {: This line declares a Docker image resource named "nginx". In Terraform, a resource block represents a piece of infrastructure to be managed. Here, we are managing a Docker image.
name = "nginx:latest": Specifies the name of the Docker image. In this case, it's "nginx:latest", indicating the latest version of the Nginx image.
keep_locally = "false": Indicates whether to keep the Docker image locally. Setting it to "false" means that Terraform won't store a local copy of the image.
}: Closes the docker_image resource block.
let's break it down in simpler terms:
Managing a Docker Image:
Declaration:
- "We're telling Terraform that we want it to handle a Docker image."
- "The name we'll use for this image is 'nginx'."
Image Details:
- "The specific image we want is called 'nginx:latest'. This means we want the latest version of Nginx."
领英推è
Local Copy:
- "We're telling Terraform not to keep a copy of this Docker image on our computer. We'll fetch it when needed."
resource docker_container "nginx" {
image = docker_image.nginx.latest
name = "nginxcontainer"
ports {
internal = 80
external = 80
}
}
resource "docker_container" "nginx" {: Declares a Docker container resource named "nginx". We're now managing a Docker container associated with the "nginx" image.
image = docker_image.nginx.latest: Specifies the Docker image to use for the container. Here, it references the previously defined Docker image named "nginx" using docker_image.nginx.latest.
name = "nginxcontainer": Sets the name of the Docker container to "nginxcontainer".
ports { internal = 80 external = 80 }: Defines the port mapping for the Docker container. It maps the internal port 80 to the external port 80, making the Nginx web server accessible externally.
}: Closes the docker_container resource block.
let's break it down in simpler terms:
Managing a Docker Container:
Declaration:
- "Now, we want Terraform to manage a Docker container, which is like a running instance of the Docker image we just talked about."
- "We'll call this container 'nginxcontainer'."
Image Reference:
- "The container will be based on the 'nginx' image we defined earlier. Think of it as creating a 'live' version from our 'saved' image."
Container Name:
- "The name we've chosen for our running container is 'nginxcontainer'."
Port Connection:
- "We want to make the content inside the container accessible from the outside world."
- "To do this, we're mapping the content's 'exit door' (port 80 inside) to the 'outside world's entrance' (port 80 outside)."
The code as a whole looks like this:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~>2.21.0"
}
}
}
provider docker {}
resource docker_image "nginx" {
name = "nginx:latest"
keep_locally = "false"
}
resource docker_container "nginx" {
image = docker_image.nginx.latest
name = "nginxcontainer"
ports {
internal = 80
external = 80
}
}
Executing the Terraform Script
Before executing the script, ensure Docker is installed on your server. If not, you can install it with:
sudo apt-get install docker.io
sudo docker ps # Check Docker status
Now, run the Terraform commands:
terraform init
terraform plan
terraform apply
Terraform will initialize, generate a plan, and apply the changes. You'll witness Docker pulling the Nginx image and running the container.
Conclusion: Graduating to Docker Integration
Day 1.1 has brought us a step closer to mastering Terraform and Docker integration. We've seamlessly configured Terraform to manage Docker resources, making our infrastructure journey more dynamic.
Stay tuned for more beginner-friendly insights as we continue our exploration into the powerful realms of Terraform and Docker. Happy learning! ?????? #Terraform #Docker #DevOps #InfrastructureAsCode #TechForBeginners