This blog explains how to createa Deployment Pipeline using Jenkins and Docker for a Java application talking to a database.
Jenkins support the creation of pipelines. They arebuilt with simple text scripts that use a Pipeline DSL (domain-specific language) based on the Groovy programming language.
The script, typically called Jenkinsfile , definesmultiple steps to execute both simple and complex tasks according to the parameters that you establish. Once created, pipelines can build code and orchestrate the work required to drive applications from commit to delivery.
A pipeline consists of steps , node and stage . A pipeline is executed on a node a computer that is part of Jenkins installation. A pipelineoften consists of multiple stages. A stage consists of multiple steps.Read Getting Started with Pipeline for more details.
For our application, here is the basic flow:

Complete source code fortheapplicationused is at github.com/arun-gupta/docker-jenkins-pipeline .
Theapplicationis defined in the webapp directory. It opens a connection to the Couchbase databaseandstores a simple JSON document using Couchbase Java SDK . The application also has a test that verifies that the databaseindeed contains the document that was persisted.
Many thanks to @alexsotob for helping me withJenkins configuration.
Let’s get started!
Download and InstallJenkins DownloadJenkins from jenkins.io . Thiswas tested with Jenkins 2.21 . Start Jenkins:JENKINS_HOME=~/.jenkinsjava -jar ~/Downloads/jenkins-2.21.war --httpPort=9090 This commandstartsJenkins by specifying the home directory where all the configuration information is stored. It also defines the port on which Jenkins is listening, 9090 in this case. First start of Jenkins shows thefollowing message in the console:
************************************************************* ************************************************************* ************************************************************* Jenkinsinitialsetupis required. Anadminuserhasbeencreatedand a passwordgenerated. Pleaseuse thefollowingpasswordto proceedto installation: 3521fbc3d40448efa8942f8e464b2dd9 This mayalsobefoundat: /Users/arungupta/.jenkins/secrets/initialAdminPassword ************************************************************* ************************************************************* ************************************************************* Copy thepassword shown here. This will be used to unlock Jenkins. Access the Jenkins console at localhost:9090 and paste the password:

Click on Next . Create the first admin user as shown:

Click on Save and Finish . Click on Install suggested plugins :

A bunch of default plugins are installed:

Found it surprising that Ant and Subversion are the default plugins. Login screen is prompted.

Enter the username and password specified earlier. Finally, Jenkins is ready to use:

That’s quite a bit of steps toget started with basic Jenkins. Do I really have to jump through all these hoops to get started with Jenkins? Is there an easier, simpler, dumber, lazier way to start Jenkins?FollowConvention-over-Configuration and give meone-click pre-configured installation.
Install JenkinsPluginsInstall the requiredplugins in Jenkins.
If your Java project is built using Maven, then you need to configure Maven in Jenkins. Click on Manage Jenkins , Global Tool Configuration , Maven installations , and specify the location of Maven.
Name the tool as Maven3 as that is the name usedin the configuration later.Again a bit lame, why can’t Jenkins pick up the default location of Maven instead of expecting the user to specify a location. Click on Manage Jenkins , Manage Plugins , Available tab, search for docker pipe . Select CloudBees Docker Pipeline , click on Install without restart .

Click on Install without restart . Docker Pipeline Plugin plugin understands the Jenkinsfile and executes the commands listed there. Next screen shows the list of plugins that are installed:

The last line shows that CloudBees Docker Pipeline plugin is installed successfully. Select Restart Jenkins checkbox. This will install restart Jenkins as well. Create Jenkins Job
Let’s createa job in Jenkins that will run the pipeline.
After Jenkins restarts, it shows the login screen. Enter the username and password created earlier. This brings you back to Installing Plugins/Upgrades page. Click on theJenkins icon in the top left corner to see the main dashboard:
Click on create new jobs , give thename as docker-jenkins-pipeline and choose thetype as Pipeline:

Click on OK. Configure Pipeline as shown:

Local git repo is usedin this case. You can certainly choose a repo hosted on github. Further, thisrepo can be configured with a git hook or poll at a constant interval to trigger the pipeline.Click on Save to save the configuration. Run Jenkins Build
Before you start the job, Couchbase database need to be explicitly started as:
dockerrun -d --namedb -p 8091-8093:8091-8093 -p 11210:11210 arungupta/oreilly-couchbase:latest This will be resolved after #9 is fixed. Make sure you can access Couchbase at http://localhost:8091, use Administrator as the login and password as the password. Click on Data Buckets tab and see the books