Install jenkins docker image with Terraform

Today we are going to install jenkins docker image with the help of terraform. Let's do it:

mkdir learn-terraform-docker-container
cd mkdir learn-terraform-docker-container
sudo nano main.tf

Let's create a terraform file to create the docker image of jenkins:

sudo nano main.tf
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0.1"
    }
  }
}

provider "docker" {}

resource "docker_image" "jenkins" {
  name = "jenkins/jenkins:lts-jdk17"
}

resource "docker_container" "jenkins" {
  image = docker_image.jenkins.name
  name  = "jenkins_server"
  ports {
    internal = 8080
    external = 8080
  }
}

Now, init the terraform:

terraform init

Format and validate the configuration

terraform fmt
terraform validate

Now, check plan and apply

terraform plan
terraform apply 
# yes if all plan are checked correctly
terraform apply
  • Prompt yes only your configuration is correct:

Tmux will be more helpful to you for testing:

tmux -s new jenkins
ngrok http 8080
  • ctrl + b and then press d to disattach the session

  • Later, you can also use

tmux attach -t jenkins # to attach the session

Your session will run on background.

Now access the public URL and here it goes:

Now, get the password; In my case:

docker ps
docker exec -it a1c2cbf17f72 /bin/bash
cat /var/jenkins_home/secrets/initialAdminPassword

Login this admin credentials:

Last updated