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


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.


TLDR: Bitbucket code here.

Jenkins provides a shell script that is meant exactly for that use-case. It's called 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 script with the plugins.txt as input.

These changes lead to the following Dockerfile:

FROM jenkins/jenkins:lts

USER root


# 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 < ${REF}/init.groovy.d/plugins.txt


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


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$ ./ up --build
Building jenkins
Step 1/9 : FROM jenkins/jenkins:lts
 ---> a3f949e5ebfd
Step 2/9 : USER root
 ---> Using cache
 ---> d6391fcb8ed0
 ---> 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 < ${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:
Downloading plugins...
Downloading plugin: configuration-as-code from
Downloading plugin: role-strategy from
 > 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
 > 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:
Cleaning up locks
Removing intermediate container 87cf05438e12
 ---> c594a6a7a5fa
 ---> 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