Skip to content

michaellihs/jenkins-swarm

Repository files navigation

Automated Jenkins Setup

This repository contains a Docker Swarm setup to create an automated Jenkins instance. See https://technologyconversations.com/2017/06/16/automating-jenkins-docker-setup/

Init local Swarm

docker swarm init

Generate dummy image to get plugins

Start generic Jenkins container:

docker service create --name jenkins -p 8081:8080 jenkins/jenkins:lts-alpine

Open Jenkins UI in browser:

open http://localhost:8081

Read password from Jenkins container:

ID=$(docker container ls -q -f "label=com.docker.swarm.service.name=jenkins")
 
docker container exec -it $ID cat /var/jenkins_home/secrets/initialAdminPassword

Enter password to browser and install required plugins. Create an admin user with username admin and password password

Extract plugins with

curl -u  "admin:password" "http://localhost:8081/pluginManager/api/json?depth=1" | jq -r '.plugins[].shortName' | tee jenkins-master/plugins.txt

Shut down the service with

docker service rm jenkins

Building the Jenkins Master image

Create a Jenkins image with plugins from plugin.txt:

docker image build --no-cache -t michaellihs/jenkins jenkins-master/

Optional: push image to Docker registry:

docker image push michaellihs/jenkins

Building the Jenkins data image

docker image build -t michaellihs/jenkinsdata jenkins-data/

Building the Jenkins config image

docker image build -t michaellihs/jenkinsconf jenkins-conf/

Building the Jenkins jobs image

docker image build -t michaellihs/jenkinsjobs jenkins-jobs/

Building the nginx image

docker build -t michaellihs/jenkinsnginx jenkins-nginx --no-cache

Building all necessary images with Docker Compose

docker-compose -f jenkins.yml build --no-cache

Create Jenkins service with automated setup

Create the Docker secrets used as Jenkins admin user username and password:

echo "admin" | docker secret create jenkins-user -
echo "password" | docker secret create jenkins-pass -

Deploy the stack:

docker stack deploy -c jenkins.yml jenkins

Check running services:

docker stack ps jenkins

Open UI in browser:

open "http://localhost:8081"

Cleaning up afterwards

docker stack rm jenkins
docker secret rm jenkins-user
docker secret rm jenkins-pass

Documentation of the Jenkins image

Where is Jenkins installed?

  • Jenkins home: /var/jenkins_home
  • Jenkins configuration: /var/jenkins_home/config.xml
  • Jenkins binary: /usr/share/jenkins

How can I see logs from containers

docker logs <containerId>

How can I see logs from services in stacks

docker stack services jenkins          # where 'jenkins' is the stack name
docker service logs -f <SERVICE ID>

TODOs

  • Find out why Jenkins is run as root rather than as jenkins in container
  • Fix issue with re-build local image not being used by Docker Swarm
  • Add volume for (persistent) configuration
  • Add volume for (persistent) logfiles
  • Separate jobs config from global config in persistent configuration
  • Provide and use a given Jenkins config
  • Provide and use given Jenkins plugin configs
  • Configure Jenkins security to use local user database
  • Provide a clean way to add script approvals
  • Add Jenkins Agents
  • Add nginx reverse proxy
  • Make port for nginx configurable (currently 8081)
  • Add SSL encryption
  • Add tests

Further Resources

About

Configuration for Jenkins in Docker Swarm

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published