<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rjshk013</title>
    <description>The latest articles on DEV Community by rjshk013 (@ninztec).</description>
    <link>https://dev.to/ninztec</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1408598%2Fc04aa1ec-740c-488b-b249-eb3e040b496e.png</url>
      <title>DEV Community: rjshk013</title>
      <link>https://dev.to/ninztec</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ninztec"/>
    <language>en</language>
    <item>
      <title>deploy Jenkins using docker compose with production ready</title>
      <dc:creator>rjshk013</dc:creator>
      <pubDate>Fri, 13 Dec 2024 17:29:17 +0000</pubDate>
      <link>https://dev.to/ninztec/deploy-jenkins-using-docker-compose-with-production-ready-155f</link>
      <guid>https://dev.to/ninztec/deploy-jenkins-using-docker-compose-with-production-ready-155f</guid>
      <description>&lt;p&gt;Jenkins is a powerful automation tool widely used for continuous integration and deployment. Setting up Jenkins in a production environment can be challenging, but with Docker Compose, you can simplify the deployment process while ensuring a secure and efficient configuration. This guide also includes setting up Jenkins Agents for distributed builds using Docker Compose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Compose File for Jenkins and Jenkins Agent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is the Docker Compose file used to deploy Jenkins Master and Jenkins Agent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;``services:
  jenkins-master:
    image: jenkins/jenkins:lts-jdk17
    container_name: jenkins
    restart: unless-stopped
    user: 1000:1000 # Explicit user ID mapping
    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - jenkins_home:/var/jenkins_home:rw
    environment:
      - JAVA_OPTS=-Dhudson.security.csrf.GlobalCrumbIssuerStrategy=true -Djenkins.security.SystemReadPermission=true
    networks:
      - jenkins_network
    security_opt:
      - no-new-privileges:true
    read_only: true # Use a read-only filesystem
    tmpfs:
      - /tmp:size=2G # Use tmpfs for temporary storage
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8080/login || exit 1"]
      interval: 1m30s
      timeout: 10s
      retries: 3
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2G
        reservations:
          cpus: '1'
          memory: 1G
  jenkins-agent:
    image: jenkins/ssh-agent
    container_name: jenkins-agent
    restart: unless-stopped
    expose:
      - "22"
    volumes:
      - jenkins_agent:/home/jenkins/agent:rw
      - type: bind
        source: ./jenkins_agent_keys
        target: /home/jenkins/.ssh
        read_only: true
    environment:
      - SSH_PUBLIC_KEY_DIR=/home/jenkins/.ssh
    networks:
      - jenkins_network
    security_opt:
      - no-new-privileges:true
    tmpfs:
      - /tmp:size=2G # Use tmpfs for temporary storage
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 1G
        reservations:
          cpus: '0.5'
          memory: 512M
networks:
  jenkins_network:
    driver: bridge
volumes:
  jenkins_home:
    driver: local
  jenkins_agent:
    driver: local``
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** ## Advantages of This Setup**&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Simplified Deployment:
    Using Docker Compose streamlines the deployment process, making it easy to manage Jenkins and its agents.
Agent Integration:
    Automatically sets up Jenkins Agents to distribute builds across multiple nodes, increasing scalability.
Security Best Practices:
    Read-only file systems and restricted privileges (no-new-privileges) enhance security.
    CSRF protection (GlobalCrumbIssuerStrategy) and system read permissions ensure a secure environment.
Resource Management:
    Defined resource limits and reservations prevent resource contention in production.
Efficient Storage Management:
    Volumes ensure persistence for Jenkins data.
    Temporary storage with tmpfs avoids unnecessary disk I/O.
Health Monitoring:
    A health check ensures the Jenkins master container is running as expected.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;## Importance of Main Components&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Jenkins Master&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The jenkins-master service is the central component, responsible for orchestrating builds and managing jobs. Key configurations include:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Volumes: Ensure Jenkins data persists across container restarts.
Health Check: Monitors the service’s availability.
Resource Limits: Prevents overutilization of system resources.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;Jenkins Agent&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The jenkins-agent service provides scalable build execution. Key configurations include:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bind Mount for SSH Keys: Securely connects the agent to the master.
Exposed Port: Allows the master to communicate with the agent over SSH.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Volumes&lt;/p&gt;

&lt;p&gt;jenkins_home: Stores all Jenkins configurations, jobs, and plugins persistently.&lt;br&gt;
jenkins_agent: Maintains the workspace and data for builds.&lt;br&gt;
Bind Mount for SSH Keys: Ensures secure agent communication.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Startup Script for Manual Setup
&lt;/h2&gt;

&lt;p&gt;Use the following script to create the required folders and set permissions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`#!/bin/bash
# Exit script on error
set -e
echo "Preparing environment for Jenkins deployment..."
# Define SSH keys directory
JENKINS_AGENT_KEYS_DIR="./jenkins_agent_keys"
# Create SSH keys directory
echo "Creating SSH keys directory for Jenkins Agent..."
mkdir -p "$JENKINS_AGENT_KEYS_DIR"
# Generate SSH keys for Jenkins Agent
echo "Generating SSH keys for Jenkins Agent..."
if [ ! -f "$JENKINS_AGENT_KEYS_DIR/id_rsa" ]; then
  ssh-keygen -t rsa -b 4096 -f "$JENKINS_AGENT_KEYS_DIR/id_rsa" -N ""
  chmod 600 "$JENKINS_AGENT_KEYS_DIR/id_rsa"
  chmod 644 "$JENKINS_AGENT_KEYS_DIR/id_rsa.pub"
else
  echo "SSH keys already exist. Skipping key generation."
fi
# Copy public key to authorized_keys
echo "Configuring authorized_keys for Jenkins Agent..."
cp "$JENKINS_AGENT_KEYS_DIR/id_rsa.pub" "$JENKINS_AGENT_KEYS_DIR/authorized_keys"
chmod 644 "$JENKINS_AGENT_KEYS_DIR/authorized_keys"
# Set permissions for SSH keys directory
chmod -R 700 "$JENKINS_AGENT_KEYS_DIR"
# Ensure everything is ready
echo "Environment setup is complete!"
echo "You can now run 'docker-compose up -d' to start Jenkins."`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;1.Prepare the Host Machine&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ensure Docker and Docker Compose are installed on your machine.
Check for sufficient disk space and memory (at least 2GB RAM and 10GB free disk space).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;2.Create Required Directories&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Run the setup.sh script provided to create the necessary directories and generate SSH keys for agent communication.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgjnvjrl58wdy35wcfifx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgjnvjrl58wdy35wcfifx.png" alt="running the startup script" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3.Set Permissions&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The setup.sh script will automatically set the required permissions for the jenkins_agent_keys directory.
Verify permissions by running:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls -ld ./jenkins_agent_keys
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fomy6r74oyfdllx7tr0me.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fomy6r74oyfdllx7tr0me.png" alt="folder permissions" width="800" height="180"&gt;&lt;/a&gt;&lt;br&gt;
4.Start Jenkins Services&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Run the following command to start the Jenkins master and agent containers:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnz2y4s8ufnchyqx2p7i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnz2y4s8ufnchyqx2p7i.png" alt="docker commands" width="800" height="150"&gt;&lt;/a&gt;&lt;br&gt;
5.Check containers status&lt;/p&gt;

&lt;p&gt;Run the following command to check containers are running or not&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1bkn5pe2bl1u8e9vca4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1bkn5pe2bl1u8e9vca4.png" alt="container status" width="800" height="71"&gt;&lt;/a&gt;&lt;br&gt;
6.Access Jenkins&lt;/p&gt;

&lt;p&gt;Open your web browser and navigate to http://:8080 to access the Jenkins web interface.Follow the setup wizard to configure Jenkins.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8p87m89qsxqreqc3wcnr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8p87m89qsxqreqc3wcnr.png" alt="jenkins gui" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
To retrieve the initial admin password for login run the below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete the initial setup like installing  plugins &amp;amp; configure admin user&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure SSH Credentials in Jenkins
&lt;/h2&gt;

&lt;p&gt;Add the private key (id_rsa) from the jenkins_agent_keys folder as a credential in Jenkins.&lt;/p&gt;

&lt;p&gt;Navigate to Manage Jenkins &amp;gt; Manage Credentials and add a new SSH key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyxcjy5xs76qck72idgu9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyxcjy5xs76qck72idgu9.png" alt="Configure SSH Credentials in Jenkins&amp;lt;br&amp;gt;
" width="800" height="431"&gt;&lt;/a&gt;&lt;br&gt;
Verify Agent Connection&lt;/p&gt;

&lt;p&gt;Go to Manage Jenkins:&lt;/p&gt;

&lt;p&gt;From the dashboard, click Manage Jenkins.&lt;/p&gt;

&lt;p&gt;Then, click Manage Nodes and Clouds.&lt;/p&gt;

&lt;p&gt;Add a New Node:&lt;/p&gt;

&lt;p&gt;Click on New Node.&lt;/p&gt;

&lt;p&gt;Enter a name for your node, such as agent.&lt;/p&gt;

&lt;p&gt;Select Permanent Agent, and click OK.&lt;br&gt;
Configure the Node:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Remote Root Directory: Set this to /home/jenkins/agent (the volume mounted for the agent container).
Labels: Add appropriate labels like agent for categorizing the node.
Launch Method:
    Select Launch agent via SSH.
    Fill in the following:
        Host: Enter the IP address of the agent container (will get from docker inspect jenkins-agent).
        Credentials:
            Click Add, and select Jenkins.
            Choose SSH Username with Private Key.
            Enter jenkins as the username.
            Paste the private key (id_rsa) contents into the key field.
        Host Key Verification Strategy:
            Select Non-verifying Verification Strategy for simplicity. For production, consider verifying host keys.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Save the Node Configuration: Click Save to complete the configuration&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2aovrn4a9y7vvzi2w3qb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2aovrn4a9y7vvzi2w3qb.png" alt="Configure the Node" width="735" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh946xhessqt99pd3km1q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh946xhessqt99pd3km1q.png" alt="Configure the Node" width="660" height="655"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify Connection:
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Jenkins will attempt to connect to the agent. If successful, the agent’s status will show as Connected.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn521s1i8vrarddh514j6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn521s1i8vrarddh514j6.png" alt="Verify Connection" width="800" height="240"&gt;&lt;/a&gt;&lt;br&gt;
Test the CI/CD Pipeline&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Create a simple freestyle or pipeline job to ensure the agent can execute builds.
Use a basic script like:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo "Running on $(uname -a)"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Firk9xozglndg1fin49qr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Firk9xozglndg1fin49qr.png" alt="Verify Connection" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This Docker Compose setup for Jenkins provides a secure, efficient, and scalable solution for production use. By following best practices, such as using Docker volumes, enabling health checks, and securing communication between the master and agent, you can ensure a robust CI/CD pipeline.&lt;/p&gt;

&lt;p&gt;Deploy Jenkins using this setup to streamline your development workflows and enhance productivity.&lt;/p&gt;

&lt;p&gt;Follow me on linkdin: &lt;a href="http://www.linkedin.com/in/rajesh-k-1b290498" rel="noopener noreferrer"&gt;www.linkedin.com/in/rajesh-k-1b290498&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jenkins</category>
      <category>docker</category>
      <category>dockercompose</category>
      <category>cicd</category>
    </item>
  </channel>
</rss>
