This repository contains a Docker Swarm setup to create an automated Jenkins instance. See https://technologyconversations.com/2017/06/16/automating-jenkins-docker-setup/
- Automated Jenkins Setup
- Init local Swarm
- Generate dummy image to get plugins
- Building the Jenkins Master image
- Building the Jenkins data image
- Building the Jenkins config image
- Building the Jenkins jobs image
- Building the nginx image
- Building all necessary images with Docker Compose
- Create Jenkins service with automated setup
- Cleaning up afterwards
- Documentation of the Jenkins image
- TODOs
- Further Resources
docker swarm init
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
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
docker image build -t michaellihs/jenkinsdata jenkins-data/
docker image build -t michaellihs/jenkinsconf jenkins-conf/
docker image build -t michaellihs/jenkinsjobs jenkins-jobs/
docker build -t michaellihs/jenkinsnginx jenkins-nginx --no-cache
docker-compose -f jenkins.yml build --no-cache
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"
docker stack rm jenkins
docker secret rm jenkins-user
docker secret rm jenkins-pass
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>
- Find out why Jenkins is run as
root
rather than asjenkins
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
- Viktor Farcic's blog post
- Official Jenkins Docker images
- Groovy snippet to manage Jenkins users
- Reference for
tini
(init replacement) - Riot Games Blog posts about Jenkins in Docker
- Cleaning up Docker Images