Jenkins on Docker is an open-source and one of the most popular Continuous Integration and Delivery software today. Jenkins is an automation service that allows developers to reliably build, test, and deploy their software. It supports the complete development life cycle of software from building, testing, documenting the software, deploying, and other stages of the software development life cycle.
You can automate the building, testing, and deployment of software by running Jenkins in a Docker container. Docker allows you to run isolated, self-contained applications consistently across different operating systems and hardware. You can take advantage of Docker’s consistency and cross-platform capability by deploying Jenkins instance inside a container.
Combining Jenkins with Docker can improve your Continuous Delivery pipeline using fewer resources. It allows you to scale up your builds, automate tasks and speed up Jenkins performance with the benefits of Docker containerization.
Create a Dockerfile for Jenkins and Nginx Container
First, create a directory to hold Jenkins files. You can create it with the following command:
mkdir -p project/jenkins-master
Next, create a Dockerfile for Jenkins using the following command:
nano project/jenkins-master/Dockerfile
Add the following codes:
FROM jenkins/jenkins:latest
LABEL maintainer="admin@example.com"
# Prep Jenkins Directories
USER root
RUN mkdir /var/log/jenkins
RUN mkdir /var/cache/jenkins
RUN chown -R jenkins:jenkins /var/log/jenkins
RUN chown -R jenkins:jenkins /var/cache/jenkins
USER jenkins
# Set Defaults
ENV JAVA_OPTS="-Xmx1024m"
ENV JENKINS_OPTS="--handlerCountMax=300 --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war"
Save and close the file when you are finished.
The above file will download the Jenkins latest image from the DockerHub repository, create required directories on the container, set Java memory for Jenkins and define Jenkins log path.
Next, create a directory to hold the Nginx file
mkdir -p project/jenkins-nginx
Next, create a Dockerfile for Nginx with the following command:
nano project/jenkins-nginx/Dockerfile
Add the following lines:
FROM centos:centos8
LABEL maintainer="admin@example.com"
# Install NGINX
RUN dnf -y update
RUN dnf -y install nginx
# Add default configuration
COPY conf/jenkins.conf /etc/nginx/conf.d/jenkins.conf
COPY conf/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx"]
Save and close the file when you are finished.
The above file will download the CentOS 8 image from the DockerHub repository, install the Nginx package inside the container, copy Nginx and Jenkins configuration file from the Docker host to the container and expose the Nginx container on port 80.
Next, you will need to create an Nginx and Jenkins configuration file on the Docker host machine. Nginx dockerfile will copy these files to the Nginx container during the build process.
First, create a directory to hold Nginx and Jenkins configuration files:
mkdir -p project/jenkins-nginx/conf
Next, create an Nginx configuration file using the following command:
Save and close the file then create a Docker network to interconnect both Nginx and Jenkins containers.
docker network create --driver bridge jenkins-net
You can verify your network using the following command:
docker network ls
Sample output:
NETWORK ID NAME DRIVER SCOPE
231d50652f17 bridge bridge local
dca050809981 host host local
7b9075392a21 jenkins-net bridge local
353f5dd78e21 none null local
Create docker-compose.yml File to Deploy Jenkins with Nginx
Now, we will create a docker-compose file to deploy all Nginx and Jenkins containers using all settings defined in the Dockerfile. Docker Compose makes it easier to manage a set of containers as a single “Service” and simplify configuration.
You can create a docker-compose.yml file inside your project directory:
Save and close the file when you are finished. The above file will start the Jenkins container and expose it on port 50000, mount jenkins-log and jenkins_data directory from the Docker host to the Jenkins container, start the Nginx container and expose it on port 80.
At this point, all files are ready to deploy your Nginx and Jenkins container. Now, navigate to the project directory and run the following command to build images for Nginx and Jenkins:
cd project
docker-compose build
This will download and build all required images from the DockerHub. You can verify the downloaded images using the following command:
docker images
Sample output:
REPOSITORY TAG IMAGE ID CREATED SIZE
project_nginx latest fa68e8ce5714 About a minute ago 497MB
project_master latest f9b71b20fd4b 2 minutes ago 441MB
jenkins/jenkins latest 49c664c4f907 2 days ago 441MB
centos centos8 5d0da3dc9764 8 days ago 231MB
Now, run all the containers using the following command:
docker-compose -p jenkins up -d
Once you are finished, you can verify the running containers using the following command:
docker-compose -p jenkins ps
You should get the following output:
Name Command State Ports
------------------------------------------------------------------------------------------------------------------
jenkins_master_1 /sbin/tini -- /usr/local/b ... Up 0.0.0.0:50000->50000/tcp,:::50000->50000/tcp, 8080/tcp
jenkins_nginx_1 nginx Up 0.0.0.0:80->80/tcp,:::80->80/tcp
You can also verify other Docker networks created by the docker-compose.yml file using the following command:
docker network ls
Sample output:
NETWORK ID NAME DRIVER SCOPE
231d50652f17 bridge bridge local
dca050809981 host host local
7b9075392a21 jenkins-net bridge local
589a9a2baf47 jenkins_jenkins-net bridge local
353f5dd78e21 none null local
To verify Docker volumes for Jenkins log and data, run the following command:
docker volume ls
Sample output:
DRIVER VOLUME NAME
local jenkins_jenkins-data
local jenkins_jenkins-log
Before accessing the Jenkins dashboard, you will need a Jenkins initial setup password to finish the Jenkins setup process. You can find the Jenkins initial setup password in Jenkins log file located on the Docker host.
You should see the Jenkins initial setup password in the following output:
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
ad163a1a111f4d9dafe50a777cf8aece
Now, open your web browser and type the URL http://your-server-ip. You will be redirected to the Jenkins initial setup screen as shown below:
Paste the Jenkins initial setup password and click on the Continue button. You should see the following screen:
Select Install suggested plugins or Select plugins to install. You should see the following screen:
Set your Jenkins admin user, password and click on the Save and Continue button. Once the installation has been finished, you should see the following screen:
Provide your Jenkins URL and click on the Save and Finish button. You should see the following screen:
Now, click on the Start using Jenkins to open the Jenkins dashboard as shown below:
In the above guide, you learned how to deploy Jenkins with Nginx in a Docker container. I hope this process will help you simplify your development workflow.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.
00votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.