# Install jenkins docker image with Terraform

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

```
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:

```bash
sudo nano main.tf
```

```bash
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
  }
}
```

<figure><img src="https://3155059101-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ftpxp8dhlubc3j58Iygn5%2Fuploads%2Fmoxpu59bKM4pjtpmWN2s%2Fimage.png?alt=media&#x26;token=f4bb4887-445a-4536-b625-4fbeb3f76ab5" alt=""><figcaption></figcaption></figure>

#### Now, init the terraform:&#x20;

```bash
terraform init
```

<figure><img src="https://3155059101-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ftpxp8dhlubc3j58Iygn5%2Fuploads%2FOVdHmbXRN6lM916dNZWi%2Fimage.png?alt=media&#x26;token=edb07164-96a4-496d-9a30-50b3941c09a2" alt=""><figcaption></figcaption></figure>

#### Format and validate the configuration

```bash
terraform fmt
terraform validate
```

#### Now, check plan and apply

```bash
terraform plan
terraform apply 
# yes if all plan are checked correctly
```

<figure><img src="https://3155059101-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ftpxp8dhlubc3j58Iygn5%2Fuploads%2FNWic8HN6zcbP1vEvBNC1%2Fimage.png?alt=media&#x26;token=9b6b410d-8380-4351-bded-c6c2d009af87" alt=""><figcaption></figcaption></figure>

```
terraform apply
```

<figure><img src="https://3155059101-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ftpxp8dhlubc3j58Iygn5%2Fuploads%2FuFW39zorTfZp5WZiQzza%2Fimage.png?alt=media&#x26;token=04025ca4-2672-4fe9-b3b1-c92d13b71d83" alt=""><figcaption></figcaption></figure>

* Prompt **yes** only your configuration is correct: \\

<figure><img src="https://3155059101-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ftpxp8dhlubc3j58Iygn5%2Fuploads%2FUOQhDKuj9lSggv3ayyBz%2Fimage.png?alt=media&#x26;token=7cbdcb7b-3512-4d56-870b-0073396dd020" alt=""><figcaption></figcaption></figure>

* If you're using localhost, you can try: <http://localhost:8080>
* Otherwise you can also try ngrok as I already mention on the [terraform-installation](https://abisec.gitbook.io/devsecops/terraform/terraform-installation "mention")

#### Tmux will be more helpful to you for testing:&#x20;

```bash
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.&#x20;

Now access the public URL and here it goes:&#x20;

<figure><img src="https://3155059101-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ftpxp8dhlubc3j58Iygn5%2Fuploads%2FeJmpl7OH65K0FPLLJQwi%2Fimage.png?alt=media&#x26;token=1a709b4b-f525-4ee7-9f6a-f7eea90a2ea7" alt=""><figcaption></figcaption></figure>

Now, get the password; In my case:&#x20;

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

```

<figure><img src="https://3155059101-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ftpxp8dhlubc3j58Iygn5%2Fuploads%2FguWvqlXKcTw6cQ5b4cDo%2Fimage.png?alt=media&#x26;token=9182858b-1386-40c9-a3c6-664ef37af5df" alt=""><figcaption></figcaption></figure>

Login this admin credentials:&#x20;

<figure><img src="https://3155059101-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ftpxp8dhlubc3j58Iygn5%2Fuploads%2Ff15FVctaTprC54NeZfog%2Fimage.png?alt=media&#x26;token=d13753b1-8fd6-49a3-8abe-16ea47b8205f" alt=""><figcaption></figcaption></figure>
