Run Jenkins With Docker image

Abdalrhmanalkraien
Dev Genius
Published in
5 min readDec 30, 2021

--

Introduction:

in this article, I will run Jenkins as a docker image but you must for you read what is Jenkins and read what is CI/CD Concept

in the future I want to use the Jenkins for Build pipeline and handle my project with Jenkins by my pipeline I will update this article with all links about Jenkins articles and you can find any update in the following section

important articles you must read before reading this article

  • you can see what a Jenkins from this link
  • you can see what is a CI/CD from this link

okay now, let's go running our Jenkins image for use in other times:

very important note:

you must have a docker in your machine to apply with me

in the first step, I want create a docker network for use with jenkines

docker  network create jenkins

in the second step, I want create my docker file

docker run \--name myjenkins \ 1--rm \ 2--detach \ 3-p 8088:8080 -p 50000:50000 \ 4-v ~/project/Jenkins_Test/CD-CD-deployment/volume/:/var/jenkins_home \ 5jenkins/jenkins:lts\ 6

now I want explain the above code :

  1. (Optional) because when run the docker take any container-specific name by default .
  2. (Optional) when shutdown jenkins image so docker will Automatically removes the Docker container
  3. (Optional) run the docker image in background if you want stop docker container you can write this command docker stop container-name or container-id
  4. (Optional) run Jenkins in port 8088 , by default docker will run Jenkins with port 8080
  5. Maps the /var/jenkins_home the directory inside the container to the Docker volume named jenkins-data. This will allow for other Docker containers controlled by this Docker container’s Docker daemon to mount data from Jenkins.
  6. Maps the /~/project/Jenkins_Test/CD-CD-deployment/volume directory inside the container to the Docker volume named jenkins-data. This will allow for other Docker containers controlled by this Docker container’s Docker daemon to mount data from Jenkins.
  7. select Jenkins version to will using.

you can use the following code to run Jenkins without any problem

docker run --name myjenkins --rm --detach  -p 8088:8080 -p 50000:50000 -v /home/abed/project/Jenkins_Test/CD-CD-deployment/volume/:/var/jenkins_home jenkins/jenkins:lts

if have any problem with run docker inside Jenkins in above command you can use the following command is stable ,if you want run docker image inside Jenkins. but in this article you can use the above command without any problem .

docker run --name myjenkins --rm -u 0 -d   -p 8088:8080 -p 50000:50000 -v $(which docker):/usr/bin/docker  -v /home/abed/project/Jenkins_Test/CD-CD-deployment/volume/:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock  -e JAVA_OPTS:-Djava.awt.headless=true  jenkins/jenkins:ltsafter that you must go to this link to check Jenkins if already exist or not please open this link http://localhost:8088if the Jenkins is already run you must input Jenkins password you can found it in the Jenkins Volume we are already defined it when run Jenkins image

after that, you must go to this link to check Jenkins if already exist or not please open this link http://localhost:8088

if the Jenkins is already run you must input Jenkins password you can find it in the Jenkins Volume we have already defined it when run Jenkins image

in my case i selected volume and i can found my password when execute following command in terminal

cat Jenkins_Test/CD-CD-deployment/volume/secrets/initialAdminPassword

this password provider by Jenkins to run you image

when setup all of that you can go to this link http://localhost:8088 when open this link you can see like the following image that meaning the Jenkins is already run in your machine

now i want make first job in jenkines

it is an easy job just I want create a job called test1 and I want print message when building this job

  • in the first step, I want click above to create new jobs button look at the following image
  • and after that I want select a name for my new job in my case called “test1” and select free Style project
  • and now I am in configuration to the new job I want go tho the build section and select Execute Shell and write inside shell and then click apply and save
  • now I have new job but I want test my job I want build my job form Job dashboard before making this action I want click on build now
  • and now I want show what is the job status and if execute my command or not for make this action I need go to build history section and then I want to select my build in my case my build number is #1 I need click on this it
  • after that I will go to Console Output and see what is output

Conclusion

in this article, I explained how I can run Jenkins image by docker and what is attribute use when building my image and how I can run image when going to Jenkins port and where I can find Jenkins password and how I can take it.

and I explained how I can make new Jobs with Jenkins

thanks for reading it and you can ask me anything about Jenkins

References

https://www.jenkins.io/doc/book/installing/docker/

https://hub.docker.com/_/jenkins

https://github.com/jenkinsci/docker/issues/785

https://github.com/jenkinsci/docker/issues/785

https://hub.docker.com/r/jenkins/jenkins/

--

--

I am a Java Developer and DevOps Engineer. I used the latest Technology for build completed development or Deployment