CI/CD as Code Part I – Introduce a stateful Jenkins Docker Container

Stateless Jenkins Container with Docker

The purpose of this sample series is to create a simple set of examples, which showcases CI/CD as Code, using Jenkins. The primary goal is to create a stateless CI/CD setup, which can be bootstrapped from a set of configuration files and scripts so that many problems related to maintenance of the infrastructure and other operational issues are reduced.

We are going to follow a step-by-step approach in this series. You can reach each step using the links below.

Sections of the article series -Fully Automated stateless Jenkins Docker container

  1. Introduce a stateful containerized Jenkins
  2. Installing and Setting up Jenkins Programmatically: The goal of this step is to complete the basic setup of Jenkins completely programmatically.
  3. First steps to ‘job as code’: In this step, we are going to head towards configuring jobs programmatically.
  4. Advanced jobs as code: In this part, we are going to finalize the setup and define some example jobs.

Introduce a stateful Jenkins Docker Container

You can checkout the full sample from here.

This initial form tackles only with a containerized version of Jenkins. The goal is to head towards a stateless form of a Jenkins setup. In this initial version, we will only have a dockerized Jenkins instance, the state of which we will map to static folders in the host machine.

The advantage of this setup is to have the state, which includes history, configurations, etc, preserved in the host machine and it’s not that different from having Jenkins installed on a physical/virtual machine. You still need to set up the installation via Jenkins initial setup interface and configure your jobs, pipelines, etc manually. Nevertheless, it’s the initial step towards the “… as a code” approach.

Container Configuration

The docker-compose.yml maps

  • Jenkins’ default HTTP port 8080 to 7080 on the host
    machine.
  • Jenkins’ default JNLP port 50000 to 50005 on the host machine.

Checkout docker-compose.yml for additional configuration.

How to run the sample

  • First, go to ci_cd root directory and run docker-compose up.
    The docker-compose.yml file maps jenkins_home directory to the directory ./jenkins/jenkins_home on the host machine. Thus, as soon as your container gets up and running, you will be able to see Jenkins-related files in this directory. In addition, note that for such kind of setups Jenkins creates a temporary admin password in the file jenkins/jenkins_home/secrets/initialAdminPassword.
  • Next, navigate to http://localhost:7080 and use the temporary password defined in jenkins/jenkins_home/secrets/initialAdminPassword to start configuring your Jenkins instance.
  • Lastly, follow the steps defined in the setup interface of Jenkins.