Jenkins is an open source, Java-based automation server that offers an easy way to set up a continuous integration and continuous delivery (CI/CD) pipeline.
This tutorial demonstrates the steps of installing Jenkins as a container using a docker compose file. The Jenkins image that has been used was built with Docker Engine inside and some custom configuration like setting the Jenkins admin user and password, also pre-installed some plugins.
Make sure that you have met the following prerequisites before continuing with this tutorial:
- Logged in as a user with sudo privileges.
- Docker Engine installed: How to Install Docker on CentOS
- Docker compose installed: How to install Docker Compose
Create the Docker compose file as below:
01- First, we will use our custom Jenkins Docker image
yallalabs/jenkins:centos built with docker and jenkins preinstalled.
version: '3.7' services: jenkins: image: yallalabs/jenkins:centos
02- Our custom Jenkins image
yallalabs/jenkins was built with a default Jenkins admin username
admin and default password
admin. So, to modify the default jenkins admin user and password, easy just provide the following environment values
JENKINS_PASS and it’s done.
environment: - JENKINS_USER=CHANGE_ME - JENKINS_PASS=CHANGE_ME
03- However, the default Jenkins
8080 port will be exposed to the host as
8000. Also, if you have set up one or more JNLP-based Jenkins agents on other machines, so you have to publish the default JNLP port
ports: - 8000:8080 - 50000:50000
04- To persist jenkins data, let’s map the
/var/jenkins_home directory inside the container to the Docker volume named
jenkins_data. Don’t forgot to create the directory
/jenkinsdata as Mount host directory.
volumes: - jenkins_data:/var/jenkins_home
05- Finally, to be able to use docker daemon with Jenkins, we need to mount the
/var/run/docker.sock as volume.
– The finale compose file should be like below:
version: '3.7' services: jenkins: image: yallalabs/jenkins:centos environment: - JAVA_OPTS=-Djenkins.install.runSetupWizard=false -Dhudson.footerURL=http://yallalabs.com - JENKINS_USER=CHANGE_ME - JENKINS_PASS=CHANGE_ME ports: - 8000:8080 - 50000:50000 volumes: - jenkins_data:/var/jenkins_home - /var/run/docker.sock:/var/run/docker.sock volumes: jenkins_data: driver: local driver_opts: type: "none" o: "bind" device: "/jenkinsdata"
06- After saving the file, start the Jenkins container as a background process with the following command:
$ docker-compose up -d
07- Now, to check if the container is runnung and up, use the below command:
$ docker-compose ps
08- open your browser and type your docker IP address followed by port
8080, login using the user and the password that you setup in the docker-compose file:
In this tutorial, you have learned how to deploy and run Jenkins as a container using Docker Compose. What’s Next? You might want to check the following guides: