Jenkins Server Setup: Complete Docker Installation Guide

๐Ÿš€ Jenkins Server Setup Overview

In this comprehensive guide, weโ€™ll walk you through setting up a Jenkins server using Docker containers. This approach is not only simple and fast but also ensures environment consistency across different deployments.

What Youโ€™ll Learn:

  • ๐Ÿณ Docker-based Jenkins installation for consistency
  • ๐Ÿ”ง Step-by-step setup process with detailed instructions
  • ๐Ÿ“ฑ Android build environment integration
  • ๐Ÿ›ก๏ธ Best practices for production deployment
  • ๐Ÿ” Troubleshooting common setup issues

๐ŸŽฏ Why Use Docker for Jenkins Setup?

Benefits of Docker-Based Jenkins:

  • โœ… Environment Consistency - Same setup across development, staging, and production
  • โœ… Easy Deployment - Quick setup and teardown
  • โœ… Version Control - Specific Jenkins versions for different projects
  • โœ… Resource Isolation - No conflicts with other applications
  • โœ… Scalability - Easy to replicate for multiple environments

Available Jenkins Images:

  • Standard Jenkins: jenkins/jenkins:lts-jdk17
  • Android-Enabled Jenkins: ghcr.io/nickhuangcyh/docker-jenkins-and-android-env:v1.0.0-jdk17

๐Ÿ› ๏ธ Step-by-Step Jenkins Setup

Step 1: Pull Docker Image

First, we need to pull the Jenkins Docker image from GitHub Container Registry. Open your terminal and execute one of the following commands:

Option A: Standard Jenkins Environment

docker pull jenkins/jenkins:lts-jdk17

Option B: Jenkins with Android Build Environment

docker pull ghcr.io/nickhuangcyh/docker-jenkins-and-android-env:v1.0.0-jdk17

Image Comparison: | Image | Description | Use Case | |โ€”โ€”-|โ€”โ€”โ€”โ€”-|โ€”โ€”โ€”-| | jenkins/jenkins:lts-jdk17 | Standard Jenkins with JDK 17 | General CI/CD pipelines | | ghcr.io/nickhuangcyh/docker-jenkins-and-android-env:v1.0.0-jdk17 | Jenkins + Android SDK + Build tools | Android app development |

Step 2: Run Jenkins Container

Next, weโ€™ll run the Jenkins container. Make sure to replace ${volume_path} with your desired local path for Jenkins data storage.

Option A: Standard Jenkins Container

docker run -d \
  --name jenkins-server \
  -v ${volume_path}:/var/jenkins_home \
  -p 8080:8080 \
  -p 50000:50000 \
  jenkins/jenkins:lts-jdk17

Option B: Android-Enabled Jenkins Container

docker run -d \
  --name jenkins-android-server \
  -v ${volume_path}:/var/jenkins_home \
  -p 8080:8080 \
  -p 50000:50000 \
  ghcr.io/nickhuangcyh/docker-jenkins-and-android-env:v1.0.0-jdk17

Command Explanation:

  • -d: Run container in detached mode (background)
  • --name: Assign a name to the container for easy management
  • -v: Mount local directory to containerโ€™s Jenkins home
  • -p 8080:8080: Map container port 8080 to host port 8080 (Web UI)
  • -p 50000:50000: Map container port 50000 to host port 50000 (Agent communication)

Step 3: Access Jenkins Web Interface

After the container starts, you can access Jenkins through your browser at http://localhost:8080. On first access, youโ€™ll be prompted to enter the initial administrator password.

Finding the Initial Password

๐Ÿ’ก Pro Tip: Remember the ${volume_path} we set during container creation? You can find the initial password at:

cat ${volume_path}/secrets/initialAdminPassword

Alternative Methods to Get Password:

# Method 1: Check container logs
docker logs jenkins-server

# Method 2: Execute command inside container
docker exec jenkins-server cat /var/jenkins_home/secrets/initialAdminPassword

# Method 3: Direct file access (if volume is mounted)
cat ${volume_path}/secrets/initialAdminPassword

Step 4: Complete Initial Setup

  1. Enter the initial password from the previous step
  2. Install recommended plugins or choose custom installation
  3. Create admin user with secure credentials
  4. Configure Jenkins URL (use http://localhost:8080 for local setup)
  5. Start using Jenkins! ๐ŸŽ‰

๐Ÿ”ง Advanced Configuration Options

Custom Docker Run Command with Additional Options

docker run -d \
  --name jenkins-server \
  --restart unless-stopped \
  -v jenkins_home:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /usr/bin/docker:/usr/bin/docker \
  -p 8080:8080 \
  -p 50000:50000 \
  -e JAVA_OPTS="-Djenkins.install.runSetupWizard=false" \
  -e JENKINS_OPTS="--prefix=/jenkins" \
  jenkins/jenkins:lts-jdk17

Advanced Options Explained:

  • --restart unless-stopped: Automatically restart container on system reboot
  • -v /var/run/docker.sock:/var/run/docker.sock: Enable Docker-in-Docker capabilities
  • -e JAVA_OPTS: Customize JVM options
  • -e JENKINS_OPTS: Customize Jenkins startup options

Docker Compose Setup

Create a docker-compose.yml file for easier management:

version: '3.8'
services:
  jenkins:
    image: jenkins/jenkins:lts-jdk17
    container_name: jenkins-server
    restart: unless-stopped
    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - JAVA_OPTS=-Djenkins.install.runSetupWizard=false
    networks:
      - jenkins-network

volumes:
  jenkins_home:

networks:
  jenkins-network:
    driver: bridge

Start with Docker Compose:

docker-compose up -d

๐Ÿšจ Troubleshooting Common Issues

Issue 1: Container Wonโ€™t Start

# Check container status
docker ps -a

# View container logs
docker logs jenkins-server

# Common solutions:
# 1. Port already in use
sudo lsof -i :8080
# 2. Permission issues with volume
sudo chown -R 1000:1000 ${volume_path}

Issue 2: Canโ€™t Access Jenkins Web UI

# Check if container is running
docker ps

# Verify port mapping
docker port jenkins-server

# Test connectivity
curl http://localhost:8080

Issue 3: Permission Denied Errors

# Fix volume permissions
sudo chown -R 1000:1000 ${volume_path}

# Or run container with different user
docker run -d \
  --name jenkins-server \
  -v ${volume_path}:/var/jenkins_home \
  -p 8080:8080 \
  -p 50000:50000 \
  --user root \
  jenkins/jenkins:lts-jdk17

Issue 4: Out of Memory Errors

# Increase container memory limit
docker run -d \
  --name jenkins-server \
  --memory=2g \
  --memory-swap=4g \
  -v ${volume_path}:/var/jenkins_home \
  -p 8080:8080 \
  -p 50000:50000 \
  jenkins/jenkins:lts-jdk17

๐Ÿ“Š Performance Optimization

Resource Recommendations

Environment CPU Memory Storage
Development 1 core 2GB 10GB
Testing 2 cores 4GB 20GB
Production 4+ cores 8GB+ 50GB+

JVM Tuning

# Optimize JVM settings for production
docker run -d \
  --name jenkins-server \
  -v ${volume_path}:/var/jenkins_home \
  -p 8080:8080 \
  -p 50000:50000 \
  -e JAVA_OPTS="-Xmx4g -Xms2g -XX:+UseG1GC" \
  jenkins/jenkins:lts-jdk17

๐Ÿ”’ Security Best Practices

1. Use HTTPS in Production

# Mount SSL certificates
docker run -d \
  --name jenkins-server \
  -v ${volume_path}:/var/jenkins_home \
  -v /path/to/ssl:/var/jenkins_ssl \
  -p 443:8080 \
  -e JENKINS_OPTS="--httpPort=-1 --httpsPort=8080 --httpsCertificate=/var/jenkins_ssl/cert.pem --httpsPrivateKey=/var/jenkins_ssl/key.pem" \
  jenkins/jenkins:lts-jdk17

2. Implement Authentication

  • Use LDAP/Active Directory integration
  • Enable Jenkins security features
  • Regular password updates
  • Two-factor authentication (if available)

3. Network Security

# Use custom network with firewall rules
docker network create --driver bridge --subnet=172.20.0.0/16 jenkins-network

docker run -d \
  --name jenkins-server \
  --network jenkins-network \
  --ip 172.20.0.2 \
  -v ${volume_path}:/var/jenkins_home \
  -p 8080:8080 \
  jenkins/jenkins:lts-jdk17

๐Ÿ“ˆ Monitoring and Maintenance

Health Checks

# Add health check to container
docker run -d \
  --name jenkins-server \
  --health-cmd="curl -f http://localhost:8080 || exit 1" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  -v ${volume_path}:/var/jenkins_home \
  -p 8080:8080 \
  -p 50000:50000 \
  jenkins/jenkins:lts-jdk17

Backup Strategy

# Create backup script
#!/bin/bash
BACKUP_DIR="/backup/jenkins"
DATE=$(date +%Y%m%d_%H%M%S)

# Stop Jenkins container
docker stop jenkins-server

# Create backup
tar -czf "${BACKUP_DIR}/jenkins_backup_${DATE}.tar.gz" -C ${volume_path} .

# Start Jenkins container
docker start jenkins-server

echo "Backup completed: jenkins_backup_${DATE}.tar.gz"


โœ… Conclusion

Congratulations! Youโ€™ve successfully set up a Jenkins server using Docker. This approach provides:

Key Benefits Achieved:

  • ๐Ÿš€ Quick deployment with Docker containers
  • ๐Ÿ”ง Environment consistency across different setups
  • ๐Ÿ“ฑ Android development support (with custom image)
  • ๐Ÿ›ก๏ธ Easy backup and restore capabilities
  • ๐Ÿ“ˆ Scalable architecture for team growth

Next Steps:

  1. Configure your first pipeline in Jenkins
  2. Set up build agents for distributed builds
  3. Integrate with version control systems
  4. Implement security measures for production use
  5. Set up monitoring and alerting

๐Ÿ’ก Pro Tip: Use Docker Compose for easier Jenkins management in development environments, and consider implementing automated backups for production deployments.


๐Ÿ’ก Pro Tip: Consider using Jenkins Blue Ocean plugin for a more modern and intuitive pipeline visualization experience.

๐Ÿ”” Stay Updated: Follow our DevOps series for more CI/CD and automation insights!


๐Ÿ“š Additional Resources:




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • How to Use Multiple GitHub Accounts on One Computer: Complete SSH Setup Guide
  • Excalidraw AI: Create Professional Diagrams with Text Commands - Complete Guide
  • Complete macOS Development Environment Setup Guide for 2024
  • Design Pattern 28: Interpreter Pattern - Complete Guide with Examples
  • Design Pattern 27: Visitor Pattern - Complete Guide with Real-World IoT Examples