Jenkins with Docker Compose and Configuration as Code

Complete Jenkins server setup using Docker Compose with Jenkins Configuration as Code (JCasC), pre-installed plugins, multi-architecture support, and Docker-in-Docker capabilities

Project Banner

Project Overview

This repository demonstrates how to create a Jenkins server using Docker Compose and Jenkins Configuration as Code (JCasC). The setup includes plugins, and supports both ARM and AMD architectures.

Features

Prerequisites

Core Components

Getting Started

Automated Setup (Recommended)

Use the setup-jenkins.sh script for automated setup:

./setup-jenkins.sh

The script will:

Manual Setup

Step 1: Create Secrets

Create the secrets directory and generate secure passwords:

mkdir -p secrets
chmod 700 secrets
openssl rand -base64 32 > secrets/jenkins_admin_password
openssl rand -base64 32 > secrets/jenkins_devops_password
chmod 600 secrets/jenkins_admin_password secrets/jenkins_devops_password

Step 2: Create .env File

Recommended: Use setup-jenkins.sh which automatically detects the Docker socket path.

Alternatively, create a .env file manually:

JENKINS_URL=http://localhost:8080/
JENKINS_ADMIN_EMAIL=admin@example.com
DOCKER_SOCK_PATH=/var/run/docker.sock

Note: The Docker socket path is automatically detected from your active Docker context. For VM-based Docker (Colima/Lima), the setup script handles this automatically.

Step 3: Build and Start

For single architecture (your current platform):

docker compose up -d --build

For multi-architecture support (ARM64 and AMD64):

./build-multiarch.sh
docker compose up -d

Quick Start Commands

Action Command
Start Jenkins docker compose up -d
View logs docker compose logs -f jenkins
Stop Jenkins docker compose down
Restart Jenkins docker compose restart jenkins
Remove volumes (⚠️ deletes data) docker compose down -v

Configuration

Jenkins Configuration as Code

The Jenkins configuration is defined in jenkins.yaml. This file includes:

For detailed explanation: See JENKINS_CONFIG.md

Docker Compose Configuration

The docker-compose.yaml file orchestrates the Jenkins container and includes:

For detailed explanation: See DOCKER_COMPOSE_CONFIG.md

Environment Variables

Variable Source Purpose
JENKINS_ADMIN_PASSWORD Docker Compose secret Admin user password (loaded from secrets/jenkins_admin_password)
JENKINS_DEVOPS_PASSWORD Docker Compose secret DevOps user password (loaded from secrets/jenkins_devops_password)
JENKINS_URL .env file Public URL of Jenkins instance (required, e.g., http://localhost:8080/)
JENKINS_ADMIN_EMAIL .env file Admin email address (required, e.g., admin@example.com)
DOCKER_SOCK_PATH .env file Docker socket path on host (automatically detected by setup-jenkins.sh)

Customizing Configuration

Edit jenkins.yaml to customize:

Important: Passwords are managed through Docker Compose secrets stored in the secrets/ directory. To change passwords, regenerate the secret files and restart Jenkins:

openssl rand -base64 32 > secrets/jenkins_admin_password
openssl rand -base64 32 > secrets/jenkins_devops_password
docker compose restart jenkins

Architecture

Docker Cloud

Docker Cloud is a Jenkins feature that allows Jenkins to dynamically provision Docker containers as build agents on demand, rather than using static build agents.

What it does

Instead of maintaining permanent build agents, Jenkins can:

Configuration

The Docker Cloud is configured in jenkins.yaml with the following settings:

Example Pipeline Usage

pipeline {
    agent {
        label 'docker'
    }
    stages {
        stage('Build') {
            steps {
                sh 'echo "Building in Docker agent"'
            }
        }
    }
}

Docker-in-Docker

This setup includes Docker-in-Docker support, allowing Jenkins pipelines to build Docker images. The Docker socket is mounted from the host, enabling Jenkins to use the host's Docker daemon.

Note: The Jenkins container runs as the jenkins user (non-root) with docker group membership for secure Docker socket access. For production environments, consider using Docker-in-Docker containers or a separate Docker daemon.

Volumes

Ports

Networks

Jenkins runs on a custom Docker network (jenkins-network) for service isolation. This allows for future expansion with additional services (databases, reverse proxies, etc.) that can communicate using service names as hostnames.

Documentation

Core Documentation

README

Project overview, features, quick start guide, and usage instructions

Jenkins Configuration

Detailed explanation of JCasC configuration file and all settings

Docker Compose Configuration

Complete guide to Docker Compose setup, volumes, networks, and secrets

PRD Requirements

Product requirements document with setup instructions and file structure

Testing Documentation

Test Documentation

Comprehensive unit tests for Jenkins configuration, authentication, and permissions

Additional Resources

Changelog

All notable changes to the project, organized by version

License

MIT License - Full license text

Accessing Jenkins

Web Interface

URL: http://localhost:8080

After starting Jenkins with Docker Compose, access the web interface at the configured URL (default: http://localhost:8080).

Default Credentials

Two pre-configured users are available:

Admin User

DevOps User

To view passwords:

cat secrets/jenkins_admin_password
cat secrets/jenkins_devops_password

Blue Ocean

URL: http://localhost:8080/blue

Blue Ocean provides a modern, intuitive user interface for Jenkins pipelines. It should be accessible after installation.

Verification

Security Considerations

Key Security Features

Best Practices

Docker Socket Permissions

The Docker socket should be owned by root:docker with permissions srw-rw---- (typically mode 0660). Check with:

ls -l $DOCKER_SOCK_PATH

Or check your .env file for the detected path.

Contributing & Support

Troubleshooting

Common issues and solutions:

For detailed troubleshooting information, see the Troubleshooting section in the README.

Tests

This repository includes comprehensive unit tests for the Jenkins configuration, covering:

For detailed information about running and maintaining the tests, see TEST_README.md.

Repository

GitHub Repository: docker-compose-and-jenkins-casc

License

This project is licensed under the MIT License.

See the LICENSE file for details.

Copyright (c) 2025 Tal Orlik