Jenkins is an open source, Java-based automation server that offers an easy way to set up a continuous integration and continuous delivery (CI/CD) pipeline.
Continuous Integration (CI) is the process of automating the build and testing of code every time a team member commits changes to version control. Continuous Delivery (CD) is the process to build, test, configure and deploy from a build to a production environment.
In this tutorial, we are going to show you how to build your jenkins Docker image with docker Engine installed, pre-installed plugins and with a default admin user and password.
Step 1/ Create Dockerfile
– First, let’s create a file named Dockerfile by extending the official Jenkins image as below:
FROM jenkins/jenkins:lts-centos ENV JENKINS_USER admin ENV JENKINS_PASS admin
– Second, we will install Docker engine in the container and add the default jenkins user into the docker security group:
USER root RUN dnf update -y && dnf install -y 'dnf-command(config-manager)' && \ dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo && \ dnf list docker-ce && \ dnf update && \ dnf install docker-ce --nobest -y && \ dnf clean all RUN usermod -a -G docker jenkins
– Next, we will disable the welcome page called SetupWizard
USER jenkins # Skip initial setup ENV JAVA_OPTS="-Djenkins.install.runSetupWizard=false"
– Now, we gonna copy default-user.groovy under the init.groovy.d directory where will setup the default admin user and password:
# COPY default-user.groovy /usr/share/jenkins/ref/init.groovy.d/ COPY default-user.groovy /usr/share/jenkins/ref/init.groovy.d/
– Finally, let’s pass a set of plugins that will be automatically installed:
# COPY and install the plugins COPY plugins.txt /usr/share/jenkins/ref/ RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt
You can find the final Dockerfile in our git repository, checkout out this Link Jenkins Build
Step 2/ Jenkins admin user setup
In order to setup a default admin user and password, we have to create a Groovy script file called default-user.groovy as below.
import jenkins.model.*
import hudson.security.*
def env = System.getenv()
def jenkins = Jenkins.getInstance()
if(!(jenkins.getSecurityRealm() instanceof HudsonPrivateSecurityRealm))
jenkins.setSecurityRealm(new HudsonPrivateSecurityRealm(false))
if(!(jenkins.getAuthorizationStrategy() instanceof GlobalMatrixAuthorizationStrategy))
jenkins.setAuthorizationStrategy(new GlobalMatrixAuthorizationStrategy())
def user = jenkins.getSecurityRealm().createAccount(env.JENKINS_USER, env.JENKINS_PASS)
user.save()
jenkins.getAuthorizationStrategy().add(Jenkins.ADMINISTER, env.JENKINS_USER)
jenkins.save()
Step 3/ Adding Plugins
Furthermore, to auto install some selected plugins, we will create a file called plugin.txt. Consequently, it will be loaded by the script install-plugins.sh. You can use the file that we have already prepared as below.
$ wget https://raw.githubusercontent.com/faudeltn/Jenkins/master/build/centos/plugins.txt
Step 4/ Build Your Custom Jenkins Image
docker build . -t yallalabs/jenkins:centos
Conclusion
In this tutorial, you have learned how to build your custom Jenkins Docker Image with Docker Engine, pre-installed plugin and with a default admin user. What’s Next? You might want to check the following guides:


