How to Install Jenkins as Docker Container with Nginx

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.

Table of Contents

In this guide, we will configure the following:

 

  • Deploy Jenkins and Nginx using Dockerfile.
  • Use Docker Volumes for persistent storage.
  • Use Docker Network to interconnect NGINX and Jenkins containers.
  • Deploy Nginx container to masks Jenkins listening on port 8080 with an Nginx web server that listens on port 80.
  • Use Docker Compose to manage a set of containers as a single “Service” and simplify configuration.

Install Docker and Docker Compose

Before starting, Docker and Docker Compose must be installed in your server. If not installed, follow the below steps to install both packages.

First, add a Docker repo with the following command:

				
					dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
				
			

Next, run the following command to install Docker CE:

				
					dnf install docker-ce docker-ce-cli containerd.io -y
				
			

After installing Docker, start and enable the Docker service using the following command:

				
					systemctl start docker
systemctl enable docker
				
			

Next, verify the Docker version using the following command:

				
					docker --version
				
			

Sample output:

				
					Docker version 20.10.8, build 3967b7d
				
			

Next, download the Docker-Compose binary using the following command:

				
					curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
				
			

Next, set execution permission to the downloaded binary:

				
					chmod +x /usr/local/bin/docker-compose
				
			

Next, verify the Docker-Compose version using the following command:

				
					docker-compose --version
				
			

Sample output:

				
					docker-compose version 1.27.4, build 40524192
				
			

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.

Create an Nginx and Jenkins Configuration File

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:

				
					nano project/jenkins-nginx/conf/nginx.conf
				
			

Add the following code:

				
					daemon off;
user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
    use epoll;
    accept_mutex off;
}


http {
    include       /etc/nginx/mime.types;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    client_max_body_size 300m;
    client_body_buffer_size 128k;

    gzip  on;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_min_length 0;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    include /etc/nginx/conf.d/*.conf;
}
				
			

Save and close the file when you are finished. Then, create a Jenkins configuration file:

				
					nano project/jenkins-nginx/conf/jenkins.conf
				
			

Add the following lines:

				
					server {
    listen       80;
    server_name  "";

    access_log off;

    location / {
        proxy_pass         http://jenkins_master_1:8080;

        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
	    proxy_set_header   X-Forwarded-Proto http;
        proxy_max_temp_file_size 0;

        proxy_connect_timeout      150;
        proxy_send_timeout         100;
        proxy_read_timeout         100;

        proxy_buffer_size          8k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;	


    }

}
				
			

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:

				
					nano project/docker-compose.yml
				
			

Add the following code:

				
					version: '3'
services:
  master:
    build: ./jenkins-master
    ports:
      - "50000:50000"
    volumes:
      - jenkins-log:/var/log/jenkins
      - jenkins-data:/var/jenkins_home
    networks:
      - jenkins-net
  nginx:
    build: ./jenkins-nginx
    ports:
      - "80:80"
    networks:
      - jenkins-net
volumes:
  jenkins-data:
  jenkins-log:
networks:
  jenkins-net:
				
			

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.

Build and Run Jenkins with Nginx

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
				
			

Access Jenkins Dashboard

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.

				
					cat /var/lib/docker/volumes/jenkins_jenkins-log/_data/jenkins.log
				
			

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:

Jenkins password prompt

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:

jenkins installed on docker with nginx

Now, click on the Start using Jenkins to open the Jenkins dashboard as shown below:

Jenkins on Docker using Nginx - Conclusion

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.

Avatar for Hitesh Jethva
Hitesh Jethva

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.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x