DEV Community

Cover image for Set Up Terraform in your Jenkins Docker Container
Paschal Ogu
Paschal Ogu

Posted on • Originally published at blog.paschalogu.com

Set Up Terraform in your Jenkins Docker Container

Organizations can automate their infrastructure as code (IaC) and streamline deployment processes by configuring a continuous integration and continuous development (CI/CD) pipeline to integrate Terraform within their Jenkins Docker container.

Terms:

Jenkins is an automation server widely used to automate various aspects of software development, including building, testing, and deploying applications.

Docker is an open-source platform that enables developers to build, deploy, run, update and manage applications in a container.

Containers are isolated environments that package an application and its dependencies together, allowing it to run consistently across different computing environments.

Terraform is a declarative open-source “Infrastructure as Code” tool, created by HashiCorp that enables the definition of on-prem and cloud resources in human-readable configuration files that can be versioned, reused, and shared as code.

In this artcle, we are going to set up terraform in a jenkins server that runs on a docker container. Let's get into it 🧑🏽‍💻

Step 1: Pull and start the Jenkins Docker container:

The Jenkins server used in this project was installed using the documentation. See more

docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 --restart=on-failure jenkins/jenkins:lts-jdk11
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Terraform on your Docker Container

To install terraform on the container, find your Jenkins container ID and log into the container as root user:

docker ps
docker exec -u 0 -it <container_id> /bin/bash
Enter fullscreen mode Exit fullscreen mode

Change Directory to /usr/bin directory

cd /usr/bin
Enter fullscreen mode Exit fullscreen mode

As the root user, install terraform in the docker container: Reference the official documentation for the installation guide.

wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Enter fullscreen mode Exit fullscreen mode

💡 If sudo and wget above are not found, they can be installed by running the below commands:

apt-get install sudo
apt-get install wget
Enter fullscreen mode Exit fullscreen mode

Confirm that terraform has been installed by running the command to view its version:

terraform --version
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Terraform Plugin

From your Jenkins dashboard navigate to Manage Jenkins >> Manage Plugins and select the Available tab. Locate this plugin by searching for Terraform and clicking on install.

Terraform-plugin

After installation of this plugin, ensure that it is enabled.

Step 4: Configure Terraform Path

Navigate to Jenkins UI and update the path to the terraform installation path:

Dashboard >> Manage Jenkins >> Global Tool Configuration >> Scroll down to Terraform and click on Terraform Installations.

Type in the installation path >> uncheck the install automatically >> apply changes and save.

Terraform-Installation

That's it! You are now ready to write a CI/CD pipeline script to integrate terraform to automate infrastructure as code (IaC) in your Jenkins Docker container.

💡 This article was first published at blog.paschalogu.com. If you found it useful, follow me on Twitter and also on LinkedIn.

Top comments (1)

Collapse
 
Sloan, the sloth mascot
Comment deleted