Note: This is part 2 of the Jenkins docker series. Click here for part 1.

Problem

You want to spawn a Jenkins Docker container that already has some default plugins of your choice pre-installed so that you don't have to install those manually.

Solution

TLDR: Bitbucket code here.

Jenkins provides a shell script that is meant exactly for that use-case. It's called install-plugins.sh and it is already part of the jenkins/jenkins:lts image.

So the easiest way is to utilize that script by feeding it a text file that has all required default plugins listed up. This will be the approach taken in this post.

We will also make use of the REF environmental variable that is set up by default and points to /usr/share/jenkins/ref.

Continuing with the Dockerfile from Part 1 of this tutorial series, we will extend it by

  1. chmoding $REF (since we changed the jenkins user ID of the default image),
  2. copying a plugins.txt file from the build environment to $REF/init.groovy.d (which is also already set up by Jenkins image),
  3. executing the install-plugins.sh script with the plugins.txt as input.

These changes lead to the following Dockerfile:

FROM jenkins/jenkins:lts

USER root

ARG USER_GROUP_ID
ARG USER_ID

# RUN groupmod -g ${USER_GROUP_ID} jenkins
RUN usermod -u ${USER_ID} -g ${USER_GROUP_ID} jenkins \ 
    # chown ${REF} since we changed the UID
    && chown -R jenkins ${REF}

USER jenkins

# copy plugins.txt to the $REF/init.groovy.d directory 
# that is already set up by the base jenkins image
COPY plugins.txt ${REF}/init.groovy.d/plugins.txt
# install all plugins listed up there
RUN install-plugins.sh < ${REF}/init.groovy.d/plugins.txt

WORKDIR $JENKINS_HOME

For demonstration purpose, we will use a simple plugins.txt file that only contains two plugins, Configuration as Code and Role-based Authorization Strategy:

configuration-as-code:1.35
role-strategy:2.16

Build the service

Running our helper shell script from last time which makes use of docker-compose, it leads to the following output that displays the installed plugins at the end of the image build:

apoehlmann:~/workspace/blog/jenkins$ ./docker_run.sh up --build
Building jenkins
Step 1/9 : FROM jenkins/jenkins:lts
 ---> a3f949e5ebfd
Step 2/9 : USER root
 ---> Using cache
 ---> d6391fcb8ed0
Step 3/9 : ARG USER_GROUP_ID
 ---> Using cache
 ---> 174db0b158ea
Step 4/9 : ARG USER_ID
 ---> Using cache
 ---> 9ce5401f06e0
Step 5/9 : RUN usermod -u ${USER_ID} -g ${USER_GROUP_ID} jenkins     && chown -R jenkins ${REF}
 ---> Running in 7067bd00d210
Removing intermediate container 7067bd00d210
 ---> 5b88ec50455b
Step 6/9 : USER jenkins
 ---> Running in 9375a6d2c74b
Removing intermediate container 9375a6d2c74b
 ---> acb150d577ac
Step 7/9 : COPY plugins.txt ${REF}/init.groovy.d/plugins.txt
 ---> 63a0bd2f9296
Step 8/9 : RUN install-plugins.sh < ${REF}/init.groovy.d/plugins.txt
 ---> Running in 87cf05438e12
Creating initial locks...
Analyzing war /usr/share/jenkins/jenkins.war...
Registering preinstalled plugins...
Using version-specific update center: https://updates.jenkins.io/2.204...
Downloading plugins...
Downloading plugin: configuration-as-code from https://updates.jenkins.io/download/plugins/configuration-as-code/1.35/configuration-as-code.hpi
Downloading plugin: role-strategy from https://updates.jenkins.io/download/plugins/role-strategy/2.16/role-strategy.hpi
 > role-strategy depends on configuration-as-code:1.29;resolution:=optional,matrix-auth:2.2
Skipping optional dependency configuration-as-code
Downloading plugin: matrix-auth from https://updates.jenkins.io/2.204/latest/matrix-auth.hpi
 > matrix-auth depends on configuration-as-code:1.12;resolution:=optional,cloudbees-folder:6.1.0;resolution:=optional
Skipping optional dependency configuration-as-code
Skipping optional dependency cloudbees-folder

WAR bundled plugins:


Installed plugins:
configuration-as-code:1.35
matrix-auth:2.5
role-strategy:2.16
Cleaning up locks
Removing intermediate container 87cf05438e12
 ---> c594a6a7a5fa
Step 9/9 : WORKDIR $JENKINS_HOME
 ---> Running in d7bc3152ee3c
Removing intermediate container d7bc3152ee3c
 ---> 65307ce54be8

Successfully built 65307ce54be8
Successfully tagged jenkins_jenkins:latest
Recreating jenkins_jenkins_1 ... done
Attaching to jenkins_jenkins_1
jenkins_1  | Running from: /usr/share/jenkins/jenkins.war

I skipped the rest of the output, which is just some more Jenkins server logs.

Getagged mit:
Jenkins Docker Tutorial Jenkins-Docker-Tutorial
blog comments powered by Disqus