Production Ready Jenkins 2.0 in Two Steps with Docker

jenkins-docker

This blog post will show you how to run production-ready Jenkins with 2 steps.  We have been using this for over six months with great success!  Just a note, you can only run this on a machine that has Docker installed on it.

We are going to use the official image from Jenkins located here. The following scripts can also be found in this Github repository: https://github.com/sekka1/jenkins-docker

First, we will create the init upstart script so that when the system boots, reboots, or if the container fails, it will restart this process just like any other server processes you run.

Step 1 – Write the start configuration file

Using your favorite text editor, open this file as a root user and edit /etc/init/jenkins.conf to add this content:

description Starts the Jenkins Docker server

start on filesystem or runlevel [2345]
stop on shutdown

script
 echo [`date`] Jenkin is starting /var/log/jenkins.log
 exec docker stop jenkins | true
 exec docker rm jenkins | true
 exec docker run --name jenkins -p 80:8080 -p 50000:50000 -v /opt/jenkins_home:/var/jenkins_home jenkins:2.0-alpine

end script

pre-stop script
 docker stop jenkins
 echo [`date`] Jenkin is stopping /var/log/jenkins.log
end script

respawn
respawn limit 2 60

This code accomplishes several things:

  1. Starts this service when the system boots and shuts it down when the system goes down
  2. Logs the starting and stopping to the file /var/log/jenkins.log
  3. Maps the Jenkins port to port 80 on your server
  4. Outputs the Jenkins home directory to your local server path: /opt/jenkins_home

Number 4 is the most important function.  If you change files in a Docker container and it restarts, the files do not persist.  By running this code, you map the files Jenkins writes to your local file system so that if the server reboots or Jenkins restarts, all of your settings and jobs will be safe.  Plus, all you have to do is save the folder /opt/jenkins_home to backup Jenkins.

Step 2 – Start it up

This step is really easy.  You simply have to start it up:


service jenkins start

You can now go to http://localhost to configure Jenkins.  Jenkins now has a setup process and requires a password.  You can retrieve the password in the logs when it first starts up.

jenkins-startup

You can also view and tail the logs of Jenkins in the Docker logs with this command:


docker logs -ft jenkins

You will see a section like this with the password


2016-05-05T17:25:49.011787978Z *************************************************************
2016-05-05T17:25:49.011808021Z *************************************************************
2016-05-05T17:25:49.011814273Z *************************************************************
2016-05-05T17:25:49.011819429Z
2016-05-05T17:25:49.011824960Z Jenkins initial setup is required. An admin user has been created and a password generated.
2016-05-05T17:25:49.011834803Z Please use the following password to proceed to installation:
2016-05-05T17:25:49.011839910Z
2016-05-05T17:25:49.011845543Z e30bed7d3c104005aeadd45de3a60d37
2016-05-05T17:25:49.011850557Z
2016-05-05T17:25:49.011855603Z This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
2016-05-05T17:25:49.011860984Z
2016-05-05T17:25:49.011865623Z *************************************************************
2016-05-05T17:25:49.011871858Z *************************************************************
2016-05-05T17:25:49.011877088Z *************************************************************

You can now walk through the initial setup.  All of the install files and configuration file Jenkins is creating will be saved to /opt/jenkins_home

We have found running Jenkins with Docker really easy.  We don’t even need to install Java on to the machine!

This blog post was written by Garland Kan, co-founder of FlowLog-Stats, and DevOps Engineering Consultant for several start-ups in the Bay area. FlowLog-Stats is a service that takes the detailed server IP flow logs provided by Amazon’s FlowLogs and translates them into easy-to-understand metrics, reports, and graphs.

Advertisements

7 thoughts on “Production Ready Jenkins 2.0 in Two Steps with Docker

    1. If you started it with the system init…it would restart it if this process dies and it would start it up if the machine reboots. I suppose it couldnt hurt to put the restart policy in the Docker command also?

      Like

      1. It would probably respawn faster than the upstart script would do it, if i.e. Jenkins is badly configured. Docker relaunches as fast as the process exits. If you also add –restart always to the docker command, I think that the respawn bit will never get used, unless the docker daemon dies (and then it won’t work).

        Like

      2. Good point. Going to add this into the repo and update the blog in a bit. Im going to add this into one of our production instances and see how it works out.

        Thanks for the feedback on making this better.

        Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s