How to use Jenkins docker image as a master and as a build node.
In the recent past, the Containerization is becoming very popular. Docker is a very prominent player in the filed of containerization.
Today I am going to show step by step procedure on how to use docker image for both Jenkins master and node. Also, I am going to address a few known issues which and how to find a workaround for them. In the tutorial, I will use the Jenkins version jenkins/jenkins:2.107.3.
Pull the image from docker hub
$ docker pull jenkins/Jenkins:2.107.3
Note that the docker-ized Jenkins will use /var/jenkins_home as Jenkins home. If you want to change this you have to edit the docker file provided by the docker hub, which was used to build the image jenkins/jenkins:2.107.3. I will not cover that in the tutorial.
The additional steps we are doing on top of the base image Jenkins/jenkins:2.107.3
1. Customize the Jenkins docker image with additional packages and new users
2. Use different user other than the default jenkins user.
3. Use different home directory for a different user but use the same jenkins_home folder
4. Use a host-volume to map to Jenkins_home inside the container
5. Use the host machine ssh keys on the container as also
Let us see one by one and what are the issues we have.
In most of the cases you will not want to use the default user called jenkins, you need to use the user which is specific to your organization or company. I will call the new user as user_jenkins, this user is already present in my host machine and it needs to be created in the new image.
Below docker file will add the new user, declares 2 arguments for ssh private and public key, copy the plugins.txt file and install them, finally copies the necessary ssh keys from host to the container.
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt
# Add user user_jenkins using static UID/GID from AD, home /var/lib/jenkins, adequate shell
RUN groupadd -g 215 group_jenkins && \
useradd -u 1396 -g group_jenkins -d /var/jenkins_home_tmp -s /bin/bash -m user_jenkins
# insert ssh keys
RUN mkdir -p ~/.ssh && \
echo "$priv" > ~/.ssh/id_rsa && \
echo "$pub" > ~/.ssh/id_rsa.pub && \
chmod 644 ~/.ssh/id_rsa.pub && chmod 600 ~/.ssh/id_rsa && chmod 700 ~/.ssh
The format of the plugins.txt file should as below
The docker build command
docker build --build-arg priv="$(cat ~/.ssh/id_rsa)" --build-arg pub="$(cat ~/.ssh/id_rsa.pub)" -t jenkins_2 --no-cache -f docker .
The key thing to be noted here is the --build-arg parameters. I am reading the existing ssh keys from the host machine and passing them as a parameter. Note that here 2 separate –-build-arg parameters one each for private key and public key. The parameters are handled by ARG instruction in the docker file
Once the image is built successfully we can start the container.
$ docker run -d -p 8082:8080 -p 5002:5000 jenkins_2
I am using the different ports. You will get the error message as shown below or similar.
Note that the ‘/var/jenkins_home/’is the default home directory from where the Jenkins runs. If you are using a user other than default userjenkinsyou will get permission denied error.
To overcome this error there should be a proper folder created before running the container. The new folder created will be mapped as host volume as in the below command.
$ su user_jenkins
$ mkdir -p /opt/my_jenkins_home
$ docker run -d -p 8082:8080 -p 5002:5000 -v /opt/my_jenkins_home:/var/jenkins_home jenkins_2