Install Jenkins and Nginx with Docker-compose

In my post this time, I will give a tutorial on how to install jenkins and nginx with docker-compose. Starting from creating the docker-compose.yml file, installing using docker-compose, configuring the nginx proxy so that it can run on port 80 (because by default jenkins runs on ports 8080 and 8443 ) and the last one doing the initial setup on jenkins. Alright, let’s get started :

Requirements :

  • Docker
  • Docker Compose
  • Domain Name (optional)

Steps to install jenkins and nginx with docker-compose :

1. Create docker-compose.yml file

First of all, we create a docker-compose.yml file, in this tutorial I put it in /home/docker (the folder I specially created for docker). So, go to the /home/docker folder, create a docker-compose.yml file

cd /home/docker

vim docker-compose.yml

Enter the configuration below and save :

version: '3'

services:
   nginx:
     container_name: nginx-jenkins 
     image: nginx:1.20-alpine
     volumes:
       - /home/docker/nginx/conf.d:/etc/nginx/conf.d
       - /home/docker/log/nginx:/var/log/nginx
       - /home/docker/log/nginx/jenkins:/var/log/nginx/jenkins
     restart: always
     environment:
       TZ: Asia/Jakarta

   jenkins:
     depends_on:
       - nginx
     container_name: jenkins
     image: jenkins/jenkins:lts-jdk11 
     restart: always
     environment:
       TZ: Asia/Jakarta
     volumes:
       - /home/docker/jenkins_home:/var/jenkins_home

some explanations :

image: nginx:1.20-alpine
image: jenkins/jenkins:lts-jdk11
For docker image, you can adjust it to what you want to use
– /home/docker/nginx/conf.d:/etc/nginx/conf.d Later we will create an nginx configuration file for the jenkins reverse proxy here
– /home/docker/log/nginx:/var/log/nginx
– /home/docker/log/nginx/jenkins:/var/log/nginx/jenkins
To put nginx jenkins logs
TZ: Asia/Jakarta Docker environment to determine the timezone, this is optional, you can use it or not it’s okay too
– /home/docker/jenkins_home:/var/jenkins_home Place for the jenkins files is placed so that they can be accessed, later the jenkins administration password can be seen in this path

2. Install jenkins and nginx with docker-compose

After you create the docker-compose.yml file, install it with command :

docker-compose up -d

Check whether the container you created with docker-compose is running or not with command :

docker ps -a

If the status is “UP”, we go to the next step.

 

3. Create Nginx configuration file for Jenkins

To be able to run jenkins using the domain and without typing the jenkins port in the browser, you need to make a reverse proxy configuration for jenkins. Because in the first step I have explained the path where I will create the configuration file, let’s just go into /home/docker/nginx/conf.d

cd /home/docker/nginx/conf.d

create nginx configuration file for jenkins reverse proxy

vim jenkins.conf

Enter the configuration below and save :

upstream jenkins {
  keepalive 32; # keepalive connections
  server jenkins:8080; # jenkins container name and port
}

# Required for Jenkins websocket agents
map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
  listen          80;       # Listen on port 80 for IPv4 requests

  server_name     jenkins.local;  # replace 'jenkins.local' with your server domain name

  # this is the jenkins web root directory
  # (mentioned in the /etc/default/jenkins file)
  root            /var/run/jenkins/war/;

  access_log      /var/log/nginx/jenkins/access.log;
  error_log       /var/log/nginx/jenkins/error.log;

  # pass through headers from Jenkins that Nginx considers invalid
  ignore_invalid_headers off;

  location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {
    # rewrite all static files into requests to the root
    # E.g /static/12345678/css/something.css will become /css/something.css
    rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
  }

  location /userContent {
    # have nginx handle all the static requests to userContent folder
    # note : This is the $JENKINS_HOME dir
    root /var/lib/jenkins/;
    if (!-f $request_filename){
      # this file does not exist, might be a directory or a /**view** url
      rewrite (.*) /$1 last;
      break;
    }
    sendfile on;
  }

  location / {
      sendfile off;
      proxy_pass         http://jenkins;
      proxy_redirect     default;
      proxy_http_version 1.1;

      # Required for Jenkins websocket agents
      proxy_set_header   Connection        $connection_upgrade;
      proxy_set_header   Upgrade           $http_upgrade;

      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 $scheme;
      proxy_max_temp_file_size 0;

      #this is the maximum upload size
      client_max_body_size       10m;
      client_body_buffer_size    128k;

      proxy_connect_timeout      90;
      proxy_send_timeout         90;
      proxy_read_timeout         90;
      proxy_buffering            off;
      proxy_request_buffering    off; # Required for HTTP CLI commands
      proxy_set_header Connection ""; # Clear for keepalive
  }

}

do a restart of docker-compose :

docker-compose restart

some explanations :

server jenkins:8080; If you want to give your jenkins container name with another name, change in this section this
server_name jenkins.local; Change the jenkins.local section with the domain name you have or it could be with your server IP. If you want to use a *.local domain on your local network, you can read Creating Your Own Local Domain on Linux
access_log /var/log/nginx/jenkins/access.log;
error_log /var/log/nginx/jenkins/error.log;
You can access the LOG at /home/docker/nginx/jenkins

4. Getting started with Jenkins

To start using Jenkins, there are several things that need to be prepared, here are the steps :

#First – Access Jenkins and enter Administrator password

The installation is complete, now it’s time to start running jenkins. Open jenkins in your browser by entering the jenkins url that you have set in the nginx configuration.

When you run Jenkins for the first time, you will be asked for an Administrator password. You can find it in the folder /home/docker/jenkins_home ( this path is already specified in the docker-compose.yml file ) . To make it easier, use this command :

cat /home/docker/jenkins_home/secrets/initialAdminPassword

copy the result, paste it in your browser, then click Continue.

 

#Second – Install the Jenkins plugin

Next you will be directed to Customize Jenkins, here you can install the plugins you need. You can choose Install suggested plugins to install the plugins suggested by jenkins or you can choose what plugins you need by clicking on the Select plugins to install option.

#Third – Create a Jenkins admin user

After installing the plugin, you will be asked to create an admin user for your Jenkins. Fill in the data then click Save and Continue.

#Fourth – Configure Jenkins URL

Next, you will be asked to configure your jenkins url, enter your jenkins url then click Save and finish.

#Fifth – Jenkins is ready!

Jenkins is ready for you to use, click Start using Jenkins.

 

Welcome to Jenkins!. So far, you have successfully installed Jenkins and run it using an nginx reverse proxy and installed it using docker-compose. I think this is enough tutorial that I can share, ask me if there is anything confusing about my writing, thank you 😀

 

SOURCE :

 

Read also :

Lukems:

Tampilkan komentar (4)

  • First off, great work! One of the better tutorials I've seen for docker/nginx/jenkins.

    However, I'm having some troubles making this work. Basically getting a browser error "ERR_CONNECTION_REFUSED" I can spin up an nginx container with the command

    docker run --name test-nginx -d -p 80:80 nginx

    And I get the welcome page as expected so I know there's no firewall or other connection issues. I used your compose files verbatim except that I'm using /var/docker/.... instead of /home/docker/....

    Any ideas or something I should look for?

    Regards,
    Bob P

    • Hallo Bob, I thank you in advance.
      is port 80 used by other than nginx-docker?
      for example, you have installed nginx or apache outside of docker which by default will use port 80.

      if yes, you can change the nginx port for your docker. for example with port 8080. add this configuration in the docker-compose.yml file in the nginx service section:

      ports:
      - "8080:80"

      then you can open nginx in your browser with http://localhost:8080

      • Thanks for the speedy reply.
        Nothing else is running on port 80. The test that I mentioned was brought up for a few minutes to see if I had firewall or other issues.

        At one point I thought the problem was that the ports weren't referenced in the docker-compose ymal file so I added some port mapping. This did not work either.

        I noticed you put the url for localhost.. My config is for a remote server... does this make a difference? Please forgive my ignorance in docker. We've got a ew instances of it running but this is my first experience with docker compose.

        Again thanks for your help. I'll continue plugging away here.

        Bob

        • sorry i just replied to you bob,

          there should be no difference if your configuration is for a remote server. You simply enter the domain you own / your server's public ip in the "server_name xxxxxxx;" in your nginx configuration.

This website uses cookies.