How to run Jenkins in Docker Container
Are you looking to start with Jenkins but you don't want to setup on your host machine. Instead you want to try it on Docker? Then you are on right place. We will run the Jenkins in Docker Container.
Let's understand the Jenkins first
Jenkins is an open source automation server which can be used to automate all sorts of tasks including building, testing and deploying the software.
Jenkins can be installed through Docker or can be run standalone by any machine with Java Runtime Environment(JRE).
Pull the Jenkins Image
Below docker command will be used to download/pull the image from DockerHub.
docker pull jenkins/jenkins
docker pull jenkins/jenkins
Using default tag: latest
latest: Pulling from jenkins/jenkins
ffacddc51d82: Pull complete
781188a9e523: Pull complete
69a15122fa9e: Pull complete
1ab7a0b3a992: Pull complete
990ebae0b67d: Pull complete
dabca586d152: Pull complete
f2d0d9a7d002: Pull complete
4ac45f07946b: Pull complete
afa4a0ea0cf8: Pull complete
a20c227120b1: Pull complete
5ed87337d302: Pull complete
a64886bb3f9f: Pull complete
52920e8e3e8d: Pull complete
2639292b661e: Pull complete
c001f3ea6952: Pull complete
bb7385fc1236: Pull complete
1b9f514ccf6a: Pull complete
Digest: sha256:76d67d16e06cf63c670405b0f08dcbbbc434ca49b4a5bc9a043b51d6c2b4d285
Status: Downloaded newer image for jenkins/jenkins:latest
docker.io/jenkins/jenkins:latest
Run the image using below command
docker run -p 8080:8080 -p 50000:50000 jenkins
This command will store the workspace under /var/jenkins_home. All the plugins and configuration stays under the same path. This can't be persistent so it's always recommended to use docker volume to create persistent data.
Instead you can use below command with bind mount for data persistency.
docker run --name homejenkins -p 8080:8080 -p 50000:50000 -v /var/jenkins_home jenkins/jenkins
Output will be like below. It has the password to start your jenkins.
*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
4772035c16234e769eaa7bcf4278
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************
2021-09-27 18:51:46.657+0000 [id=29] INFO jenkins.InitReactorRunner$1#onAttained: Completed initialization
2021-09-27 18:51:46.666+0000 [id=23] INFO hudson.WebAppMain$3#run: Jenkins is fully up and running
2021-09-27 18:51:59.156+0000 [id=49] INFO h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
2021-09-27 18:51:59.157+0000 [id=49] INFO hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1
2021-09-27 18:51:59.161+0000 [id=49] INFO hudson.model.AsyncPeriodicWork#lambda$doRun$0: Finished Download metadata. 58,870 ms
Now go to localhost:8080 on your browser and It will popup a screen asking for this password. Once you'll enter the password for Jenkins it will give you below screen.
Click on Install plugins and wait for couple of minutes as it takes some time to install recommended plugins. Once installation is done ,you'll get username and password screen. Just put your username and password and save it. You are all set to use Jenkins.
Tip: Make sure you take the backup of your directory volume of Jenkins so that you don't loose any data.
By default you get 2 executers but you can change it and modify your image to set more number of executers.
Once everything is done you'll se below home screen for Jenkins
Click on New Item and select freestyle project and give it name. now go to build environment and select Execute shell script.
Now paste below command inside build environment. We'll print hello Jenkins then print working directory and then the user, using shell script.
echo 'Hello Jenkins'
pwd
whoami
Click on Apply and save. Now navigate to project and click Build now and your build will be running.
Once your build is completed ,click on green check sign on your build number. It will open the output console and you will see below results on console.
Started by user Pankaj D
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/simple project
[simple project] $ /bin/sh -xe /tmp/jenkins86668692502344527.sh
+ echo Hello Jenkins
Hello Jenkins
+ pwd
/var/jenkins_home/workspace/simple project
+ whoami
jenkins
Finished: SUCCESS
That was it for running Jenkins on Docker. We can use Jenkins for other options as well like creating a pipeline and integrating with github.
Note: If you think this helped you and you want to learn more stuff on devops, then I would recommend joining the Kodecloud devops course and go for the complete certification path by clicking this link