interbeing
2 min readMay 15, 2021

--

Create custom Jenkins master docker image with docker client installed for k8s

Why?

Offical jenkins master image does not have docker client installed.

sometime if you do not want launch another (agent) node to run your jenkins work, you may want use your master node to build project. if that’s the case, you might want master node to have docker installed.

What need to do

Create a docker image based offical jenkins image , then add docker client .

then pass DOCKER_HOST enviromental variable to your docker container when luanch container from that image.

Step 1. Build your docker image from docker file

FROM jenkins/jenkins:2.277.4-lts-jdk11
ARG HOST_UID=1004
ARG HOST_GID=999
USER root
RUN apt-get update && apt-get install -y apt-transport-https \
ca-certificates curl gnupg2 \
software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN apt-get update && apt-get install -y procps
RUN apt-key fingerprint 0EBFCD88
RUN add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable"
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
ENV DOCKER_HOST=tcp://10.2.1.192:2375

Above will build your image . ENV DOCKER_HOST is optional. you shall pass this while your luanch containter. for example. pass this from your kubernets deployment yaml file.

Build image and upload to your docker repositorydocker build . -t interbeing/k8scicd:jenkins-2
docker push interbeing/k8scicd:jenkins-2

Create k8s deployment file to run instances from this image
ADFS-Admin:~/jenkins $ cat deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: jenkins-master
namespace: jenkins
labels:
app: jenkins-master
spec:
replicas: 1
selector:
matchLabels:
app: jenkins-master
template:
metadata:
labels:
app: jenkins-master
spec:
serviceAccountName: jenkins-account
securityContext:
fsGroup: 1000
containers:
- env:
- name: DOCKER_HOST
value: tcp://10.2.1.192:2375
- name: DOCKERHOST
value: tcp://10.2.1.192:2375
- name: jenkins
image: interbeing/k8scicd:jenkins-2
imagePullPolicy: Always
ports:
- containerPort: 8080
- containerPort: 50000
readinessProbe:
httpGet:
path: /login
port: 8080
initialDelaySeconds: 300
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 2
failureThreshold: 5
volumeMounts:
- mountPath: /data
name: jenkins-home
volumes:
- name: jenkins-home
persistentVolumeClaim:
claimName: pvc-jenkins-home

in above file, we passed DOCKER_HOST file to container. and specify using custome jenkins-2 image. since this dockerfile only install docker-cli client, not the docker engine, so the DOCKER_HOS T is external machine.

if you already have deployment running. you can use kubectl set image to update the image.

kubectl set image deployment jenkins-master -n jenkins  jenkins=interbeing/k8scicd:jenkins-2
kubectl rollout status deployment jenkins-master -n jenkins

to set an running deployment .use

kubectl set env deployment jenkins-master -n jenkins DOCKER_HOST=tcp://x.x.x.x:2375

--

--