- This is a guide to create Jenkins server with Docker compose.
- You can check out step-by-step guide in Instruction and Setting up sections.
- About The Project
- Getting Started
- Contact Me
- Becoming a Sponsor
- Contributing
This is the guide to setup Jenkins Server with docker compose.
Jenkins help to automate the software for building, testing, deploying, Continuous Integration and Continuous Delivery (CI/CD).
There are many ways to install Jenkins Server like installer, install with docker, install with brew (MacOs).
Below guild is to install Jenkins Server with Docker.
You can see sample Jenkins Pipeline projects as Spring Boot Jpa Docker Jenkins Pipeline application for using this Jenkins Server.
This project is built with
See the Prerequisites sections for basic knowledge and go as per below Instruction section.
If you are new in Git, GitHub and new in Spring Boot configuration structure,
You should see basic detail instructions first in here Spring Boot Application Instruction
If you are not good enough in basic Docker Compose commands, you can learn some commands in below projects.
Click below links.
- Spring Boot Docker Sample (to get basic docker commands)
- Spring Boot JPA Docker Compose Sample (to get basic docker compose sample)
- Clone the repo
git clone https://github.com/yewin-mm/jenkins-server.git
Prerequisites can be found here, Spring Boot Application Instruction.
You need to install Docker
in your machine. Get Docker.
If you don't know about basic docker command, see here. Spring Boot Docker Sample
You can learn sample docker-compose commands here. Spring Boot Docker Compose Sample.
You can see sample Jenkins Pipeline projects as Spring Boot Jpa Docker Jenkins Pipeline application for using this Jenkins Server.
- Make sure docker is running in your machine.
- Go to your cloned folder (clone this repo) with
cmd
(for window) orterminal
(for Mac and Linux).
-
Below commands need to type in your
CMD
orTerminal
where path should be in your application folder. -
Before you run below
docker-compose up -d
,- You need to check your db.
- If you run your database like this ways Run MySQL DB as Container
it's ok, you can skip below steps and type belowup
command under Run Jenkins Server title. - Because I run MySQL as container and give network name as
mysql
, so it's ok to connect to myMySQL
db container which running with network `mysql. - So, if you run your db as container and gave network name not like
mysql
you need to change that your network name in mydocker-compose
file. - If not change your network name, it will get error.
- Because if you run Jenkins Server as container, that container can't connect to DB container if those containers are not going under same network.
- So, you need to run your Jenkins Server as same network with your DB container.
- But if you run your database as normal (eg. run with installer), you need to change a bit in this Docker compose file.
- Open your cloned
docker-compose.yml
file with text editor like sublime text or notepad++, etc. - Comment out
- mysql
undernetworks
tag underservices
section. - Uncomment
jenkins
,aliases
and- docker
- Comment out
- mysql
,external: true
undernetworks
section (which is below in that file). - Uncomment
jenkins
,driver
andname
. - After that,
Save
the file and type belowup
command.
-
Run Jenkins Server (with port 9000)
-
docker-compose up -d
- After that you can call jenkins server portal with
http://localhost:9000/
through browser. (if you run jenkins at vm server, you need to addserver ip
ordomain
instead oflocalhost
).
-
- After you get in jenkins server portal through browser by
http://localhost:9000/
, - You will see below unlock screen to unlock jenkins server.
- Type below command in
CMD
orTerminal
.-
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
-
- Then you will get initial password like below
- Copy initial password which appeared in your
CMD
orTerminal
by typing above command. - Paste in jenkins server portal to unlock jenkins server.
- Please note that initial password is no need to use in the future. It's need only one time (this time).
- After you've unlocked jenkins server portal,
- Choose Install Suggested Plugins as below picture.
- This step will take a few minutes depend on your internet connection.
- If anything failed, you can retry download plugins or if you saw error in home page,
you can typedocker compose down
and clear volume Jenkins volume by typingdocker volume rm jenkins-data
anddocker volume rm jenkins-docker-certs
and typedocker compose up -d --build
again.
Please note that deleting Jenkins volume will clear all Jenkins data and so, if you can install plugins successfully or if anything is not wrong or if no need to clear data, don't delete volume.
- After you've installed suggested plugins in jenkins server portal,
- Create admin user and you can give user name as you want.
- After you've created admin user,
- You can see Instance Configuration wizard like below.
- Just click
Save and Finish
button to go withlocalhost:9000
which port I put indocker compose
file. - After that, Click
Start using Jenkins
. - After that, you can see the
Jenkins server portal
like below.
- Finally, you can successfully set up Jenkins Server with docker.
-
This part is important after you shut down or restart your computer or restart docker desktop daemon.
-
Because after your restart your computer or decker daemon, the connection for docker daemon is lost, and so, you won't able to run docker login in pipeline script.
-
It will get permission denied errors.
-
Go got Jenkins Container by below command.
-
Type
docker exec -u root -it jenkins bash
-
After you get inside jenkins container by root user,
-
Type
sh script.sh
-
Then, Type
exit
to leave Jenkins container.
- You can add tools, pre-defined credentials in Jenkins Server by below.
- In my case, I need maven to build my applications and docker hub credentials to accessing docker hub for pushing, pulling images (here, that could be any image repository instead of docker hub).
- You can add maven tools to build your applications by using
Maven
. - There are 3 ways to use maven with Jenkins
- you can use docker maven image as agent (this way need to install docker cli, but you can use different maven version as you want and can use share volume storage which not to download dependency every time)
- you can use maven as tools and install in Jenkins (this way is easy and with that way, you need to use only specific maven version which you installed in Jenkins)
- you can link to dockerfile which pull maven image from docker hub. (this way is a bit hard to handle for beginners)
- Here, I used way 2 as that is easy and understandable
- In Jenkins Server Portal,
- Go to
Manage Jenkins
sub menu ->Global Tool Configuration
-> underMaven
section, add like below picture - Please note that, you need to start with
Capital
letter in name field likeMaven-{any}
- After that Click
save
.
-
You need below three maven plugins to use
maven
in your pipeline. -
Go to
Manage Jenkins
sub menu ->Manage Plugins
-> underAvailable Plugins
section,- Search
Maven Integration
,Pipeline Maven Integration
andPipeline Utility Steps
(for reading pom file) - Select those plugins and click
Install without restart
.
- Search
-
Click
Restart Jenkins when installation is complete and no jobs are running
to apply plugins.
- This step take a few minutes depend on your internet connection.
- If downloading plugins was success and not able to restart Jenkins server (if too long), you can restart
docker desktop
or compose down and up to restart Jenkins by manually.
- You can add cloud image repository like Docker Hub credentials as pre-defined to upload your application image.
- Here, image repository can be anything like your cloud server, Docker Hub, etc.
- Here, I will test with Docker Hub.
- Go to
Docker Hub cloud
and login with your account. Docker-Hub - Go to
Account Setting
under your profile. - Click
Security
tag and clickNew Access Token
. - Fill the name as you want in
Access Token Description
field. You can give onlyread and write
permission if your pipeline don't want to delete image in docker hub. - After that Click
Generate
.
-
You can see generate Token and click
Copy and Close
. -
Please note that token will use when you try to connect your docker hub and so, it's better saving in your notepad.
-
You can delete and creat new token as you want. Please don't share your token to others as high security risk.
- You need to add global credentials to access your cloud repository from Jenkins pipelines.
- Go to
Manage Jenkins
sub menu ->Manage Credentials
-> click(global)
under domain -> clickGlobal credentials (unrestricted)
->Add Credentials
- Add credentials like below,
- Here, username will be your docker hub username and password will be docker hub token that you already generate in above step. ID will be used in pipeline script.
- After that click
create
. - Here, you can change your private cloud repository if you have and I showed with docker hub public cloud as demo.
- Here, you can add other credentials like
Github
if your project is private. Because if private repository, Jenkins pipelines won't be pull without credentials.
- You need to install Docker plugins in Jenkins Server to use docker command in pipelines.
- Go to
Manage Jenkins
sub menu ->Manage Plugins
-> underAvailable Plugins
section,
searchDocker Pipeline
and select that plugin and clickInstall without restart
.
-
Click
Restart Jenkins when installation is complete and no jobs are running
to apply plugins. -
If downloading plugins was success and not able to restart Jenkins server (if too long), you can restart
docker desktop
or compose down and up to restart Jenkins by manually.
- Here, I will push my image to Docker hub and so, I need to create repository to store my image in Docker Hub.
- Go to
Docker Hub cloud
and login with your account. Docker-Hub - Click
Repositories
tab in above. - Give name and click
Create
.
- Now, you are done for setting require configuration to run Java Project by using Docker and push to Docker Hub. Here, you can add more plugins as you want.
- Let's go to Testing in below.
- You can test pipeline by creating sample pipeline scripts in Jenkins Server.
- There are two types of pipelines scripts which used groovy languages.
- Scripted Pipeline
- Declarative Pipeline
- Here, I will use Declarative Pipeline as it's easy to understand and modern way.
- Below are testing for using maven in pipelines
-
Click
New Item
in your Jenkins Dashboard Portal. -
Enter
test-maven-with-maven-tools
in field. (you can give any name you want) -
Choose
Pipeline
and ClickOk
. -
Copy below scripts and paste under
Pipeline Script
section.pipeline { agent any tools { // you need to add Global Tools Configuration in Jenkins Server. // go to Jenkins Portal (Manage Jenkins -> Global Tools Configuration -> Add maven -> give name (ref - https://github.com/yewin-mm/jenkins-server#maven) maven 'Maven-3.9.0' } stages { stage('Print Maven Version') { steps { sh 'mvn -v' } } } }
- Here, tools name
maven 'Maven-3.9.0'
is need to be same with your Maven tools name which you gave in above Add Maven Tools section. - Click
Save
. - After you have entered that above
test-maven-with-maven-tools
pipeline job, ClickBuild Now
and refresh page. - There you will see job is running with build number #1.
- Click
Console Output
which can be reach by clickingdown arrow
button beside#1
job like above picture, to see console output.
- There you can see pipeline console output and can see maven version which we printed in above script.
- After that click
test-maven-with-maven-tools
(top left corner) and you can see pipeline jobs are success with green line. - Please note that above sample pipeline script has only one stages call
Print Maven Version
, you will see only two stages in screen and one is for installing declarative maven tools.
- After you can go to Dashboard and you can see one pipeline is showing in the dashboard.
- You can enter that pipeline again and build again, you will see it's running with build number #2.
-
Click
New Item
in your Jenkins Dashboard Portal. -
Enter
test-maven-with-docker-agent
in field. (you can give any name you want) -
Choose
Pipeline
and ClickOk
. -
Copy below scripts and paste under
Pipeline Script
section.pipeline { // run docker as agent to pull maven image from docker hub agent { docker { // use alpine maven image for reducing size and light weight image 'maven:3.9.0-eclipse-temurin-11-alpine' // mount volume with .m2 folder to reduce repeatative downloading jar file args '-v $HOME/.m2:/root/.m2' } } stages { stage('Print Maven Version') { steps { sh 'mvn -v' } } } }
- Click
Save
. - After you have entered that above
test-maven-with-docker-agent
pipeline job, ClickBuild Now
and refresh page. - There you will see job is running with build number #1.
- Click
Console Output
which can be reach by clickingdown arrow
button beside#1
job like above maven tools step, to see console output. - There you can see pipeline console output and can see maven version which we printed in above script.
- After that click
test-maven-with-docker-agent
(top left corner) and you can see pipeline jobs are success with green line. - Please note that above sample pipeline script has only one stages call
Print Maven Version
, you will see one stage.
- After you can go to Dashboard and you can see two pipeline is showing in the dashboard (one is for testing maven with tools and one is for testing maven with docker agent).
- You can enter these pipeline again and build again to test.
-
Here, we will try to Login to docker hub from jenkins server by using pipeline script.
-
Click
New Item
in your Jenkins Dashboard Portal. -
Enter
test-docker-login
in field. (you can give any name you want) -
Choose
Pipeline
and ClickOk
. -
Copy below scripts and paste under
Pipeline Script
section.pipeline { environment { // that is we already predefined with name `docker-hub` in jenkins Dashboard -> Manage Jenkins -> Credentials (ref - https://github.com/yewin-mm/jenkins-server#docker) DOCKER_HUB_CREDENTIALS = credentials('docker-hub') } agent any stages { stage('Test Username') { steps{ script { // print build number from jenkins echo "Jenkins build number: ${env.BUILD_NUMBER}" // we already defined DOCKER_HUB_CREDENTIALS variable and USR is provided from jenkins to get username from that credentials echo "docker-hub Username: $DOCKER_HUB_CREDENTIALS_USR" } } } stage('Test Login to Docker Hub') { steps{ sh 'echo $DOCKER_HUB_CREDENTIALS_PSW | docker login -u $DOCKER_HUB_CREDENTIALS_USR --password-stdin' echo 'Login Success' } } } }
- Here, credentials id
'docker-hub
is the ID which we pre-define in above Add Credentials in Jenkins section. - Click
Save
. - After you have entered that above
test-docker-login
pipeline job, ClickBuild Now
and refresh page. - There you will see job is running with build number #1.
- Click
Console Output
which can be reach by clickingdown arrow
button beside#1
job like above picture, to see console output. - There you can see pipeline console output and can see maven version which we printed in above script.
- Go back to your docker login pipeline, and you will see
Test Username
andTest Login to Docker Hub
with green line. - You can go back to dashboard now.
-
Here, we will try to read pom file properties by using
Maven Pipeline Utility
plugin which plugins we already installed in above. -
Click
New Item
in your Jenkins Dashboard Portal. -
Enter
read-pom
in field. (you can give any name you want) -
Choose
Pipeline
and ClickOk
. -
Copy below scripts and paste under
Pipeline Script
section.// declare variable as global (not declare in script tag) to use in other stages def readPom pipeline { agent any tools { // you need to add Global Tools Configuration in Jenkins Server. // go to Jenkins Portal (Manage Jenkins -> Global Tools Configuration -> Add maven -> give name (ref - https://github.com/yewin-mm/jenkins-server#maven) maven 'Maven-3.9.0' } stages { stage('Git Clone') { steps { git branch: 'master', url: 'https://github.com/yewin-mm/spring-cloud-config-sample-microservice-a' } } stage('Read pom file') { steps { script { // for using readMavenPom, you need to add Pipeline Utility Steps plugin in Jenkins Server (ref - https://github.com/yewin-mm/jenkins-server#maven) readPom = readMavenPom file: 'pom.xml'; } } } stage('Print Values') { steps { echo "Application Name: ${readPom.name}" echo "Artifact Id: ${readPom.artifactId}" echo "Application Version: ${readPom.version}" } } } }
-
Click
Save
. -
There, you can use any of your
Github projects
link to clone. -
After you have entered that above
read-pom
pipeline job, ClickBuild Now
and refresh page. -
You can see console log like above steps and there you will see your
application name, artifact id and version
which we print in above pipeline script. -
You can test other pipeline scripts by testing like above.
-
You can also generate pipeline tags by using
Pipeline Syntax
.
Please Don't Forget to run Script after everytime you start your computer or start docker daemon again as this Run Script section.
Have Fun and Enjoy in Learning Code
Name - Ye Win
LinkedIn profile - Ye Win
Email Address - yewin.mmr@gmail.com
WhatsApp - +959252656065
Website - My Website
Project Link: Jenkins Server
If you like any of my projects or if you want to support my work, please kindly consider becoming a sponsor.
It gives me great motivation and I can relentlessly maintain my projects and contribute to the open-source community.
Contributions are what make the open source community such an amazing place to be learnt, inspire, and create. Any contributions you make are greatly appreciated.
If you want to contribute....
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/yourname
) - Commit your Changes (
git commit -m 'Add some AmazingFeatures'
) - Push to the Branch (
git push -u origin feature/yourname
) - Open a Pull Request