Dockerized Jenkins Setup with Persistent Volumes

This task aims to set up a Jenkins instance running inside a Docker container while ensuring data persistence using Docker volumes. The setup will also include basic security configurations, port mappings, and plugin installations to prepare Jenkins for use in a CI/CD pipeline.

Task Architecture

Technologies Used:

Step-by-Step Implementation

Step 1: Install Required Dependencies

Ensure that Docker and Docker Compose are installed on your system.

Install Docker

sudo apt update
sudo apt install -y docker.io

Enable Docker Service

sudo systemctl enable docker
sudo systemctl start docker

Install Docker Compose

sudo apt install -y docker-compose

Verify Installation

docker --version
docker-compose --version

Step 2: Create a Persistent Volume for Jenkins

Jenkins stores job data, configuration, and plugins in /var/jenkins_home. To ensure persistence, create a Docker volume:

docker volume create jenkins_data

To check the created volume:

docker volume ls

Step 3: Create a Docker Network (Optional)

For better communication between services (e.g., if using a reverse proxy):

docker network create jenkins_network

Step 4: Create a Docker Compose File

Create a directory for Jenkins and navigate into it:

mkdir -p ~/jenkins-docker && cd ~/jenkins-docker
touch docker-compose.yml

Create a

docker-compose.yml
file:

touch docker-compose.yml

Edit the file and add the following configuration:

version: '3.8'

services:
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins_server
    restart: unless-stopped
    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - jenkins_data:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - JAVA_OPTS=-Djenkins.install.runSetupWizard=false
      - JENKINS_OPTS=--prefix=/jenkins
    networks:
      - jenkins_network

volumes:
  jenkins_data:

networks:
  jenkins_network:
    driver: bridge

Explanation:

Step 5: Start Jenkins

Run the following command to start Jenkins:

docker-compose up -d

To check if the container is running:

docker ps

If you need logs:

docker logs -f jenkins_server

Step 6: Retrieve Initial Admin Password

Jenkins requires an initial admin password when accessed for the first time.

Run:

docker exec -it jenkins_server cat /var/jenkins_home/secrets/initialAdminPassword

Copy this password and paste it when prompted in the Jenkins UI.

Step 7: Install Suggested Plugins

  1. Open a browser and go to:
    http://:8080/jenkins
  2. Enter the initial admin password.
  3. Select Install Suggested Plugins.
  4. Create an admin user when prompted.
  5. Finish the setup.

Step 8: Install Required Jenkins Plugins

Navigate to Manage Jenkins → Plugin Manager → Available Plugins and install:

Step 9: Secure Jenkins

  1. Disable Anonymous Access:
    • Go to Manage Jenkins → Configure Global Security.
    • Select Enable Security.
    • Choose Jenkins' own user database.
    • Disable Allow users to sign up.
    • Set Matrix-based security or Role-based access control.
  2. Install and Configure Reverse Proxy (Optional)
    • Use Nginx as a reverse proxy for added security.
    • Ensure Jenkins is accessible only via HTTPS.

Step 10: Automate Dockerized CI/CD Pipeline

  1. Create a New Pipeline Job:
    • Go to New Item → Pipeline.
    • Define the Jenkinsfile for CI/CD automation.
  2. Example Jenkinsfile (Pipeline as Code)
  3. Create a file named Jenkinsfile in your repository:

pipeline {
    agent {
        docker {
            image 'maven:3.8.1'
        }
    }
    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/your-repo/sample-app.git'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Docker Build & Push') {
            steps {
                sh '''
                docker build -t your-dockerhub-user/sample-app .
                docker login -u your-dockerhub-user -p your-password
                docker push your-dockerhub-user/sample-app
                '''
            }
        }
        stage('Deploy') {
            steps {
                sh 'docker run -d -p 8081:8080 your-dockerhub-user/sample-app'
            }
        }
    }
}

Step 11: Run the CI/CD Pipeline

Conclusion

You have now successfully set up Jenkins in a Docker container with persistent storage. This setup ensures that:

Next Steps: