Understand VM vs Docker Container for Jenkins

Before we start learning on the jenkins job, let's understand one thing from the docker.

Peeking into docker container:

We are doing below things to make you understand that there are two different entities

  • VM where jenkins docker image is running
  • Docker Container where our jenkins is running.

Let's do basic things for understanding the concept

  • Open your putty and connect to the remote machine
  • Type docker ps inside terminal/putty
    docker-ps-jenkins
  • Now you have executed the command on your remote machine;
  • Similar to remote machine the container (here jenkins) also a machine and it also has the process and terminal but to run something on the container we have to switch to the container
  • Switch to the container using
    [[email protected] ~]# docker exec -it jenkins bash
    [email protected]:/$​

    switch-to-docker-jenkins

  • Now if you execute something, it will affect only the docker container but not the VM.
  • For example, list all the files
    list-all-files-jenkins-docker
  • You can exit out of the container by using exit command
    exit-jenkins-docker-job
  • Now type ls command to list files
    ls-vm-docker-jenkins

New Jenkins Job

Now open the jenkins on the UI and log into it using the credential you have created.

  • Create new job by click on New Item on the left side pane or create new Jobs
    new-item-jenkins-docker
  • Enter Name and select FreeStyle Project
    first-job-jenkins-freestyle
  • Click Ok and Jenkins will open the configuration page.
    configuration-page-jenkins
  • Scroll down to the Build section
    execute-shell-build-jenkins
  • Type echo Hello world command in the command input box
    echo-hello-world-jenkins
  • Save the job and you will be navigated to the home page.
  • Click the Build Now link to start the job
    jenkins-home-page-first-job
  • Now you can see the first build (#1) and click on it.
    first-build-jenkins
  • Click on the console output link
    click-console-output
  • Now you see the output of the terminal.
    hello-world-output
  • The above script is executed on the docker container box but not on the VM

Redirect output to file

instead of showing the out on console we can rediret the out to a file. For example, we can create the file with touch command (vi command will not work inside the docker container by default)

First, switch to the Jenkins container; In the below command, we are writing "chercher tech" to file called sample using touch command and we are using cat command to show the content of the sample file.

[[email protected] ~]# docker exec -it jenkins bash
[email protected]:/$ echo "chercher tech" > /tmp/sample
[email protected]:/$ cat /tmp/sample
chercher tech

redirect-output-jenkins-docker

Similar to above we can create files in jenkins; sometimes we required to redirect the output to file. In such cases, this example will help.

  • Click on the configure link from left pane
    configure-jenkins-first-job
  • Scroll down to the build step and add below script to get the date.
    echo "todays date and time is :  $(date)" > /tmp/datetime.txt​
  • Save the Job and Press Build Button
  • Open the latest build and go to the console. You will get the output
    first-job-jenkins-date-output-to-file
  • Open the Jenkins container, and open the datetime.txt file; the same output is present in the file
    output-file-jenkins-docker

Execute bash/shell script with Jenkins

In the above topic we have seen how to eecute a command from jenkins; sometimes we might need to execute bunch of commands. In such cases, it is better create a file and place those commands or script in it.

lets create sample script in jenkins container, we cannot create files inside the docker conatiner (jenkins container is docker conatainer). so We have to create the file in VM (centOS here) and then copy the file to jenkins container.

Create a file called demo

[[email protected] jenkins-data]# vi demo.sh

Place the below content in it; In the below code, we are exoecting two parameter along with the filename for the execution.

#!/bin/bash
NAME=$1
SITE=$2
echo "hello $NAME, your website is $SITE"

Provide the executable permission to the file

chmod +x demo.sh

To execute the above file we have to use below

# ./filename param1 param2
./demo.sh karthiQ CherCherTech

bash-script-execute-jenkins

Now copy the file into jenkins container

#docker cp srcFile targetContainer:targetFolder
docker cp ./demo.sh jenkins:/tmp/
Run Bash with Jenkins :

Edit the Build step like below
execute-script-from-jenkins

Build the Jenkins Job and Goto the Console output
execute-bash-jenkins-output

Parameterization on Jenkins

There will be times where you have to change the parameters used in the script. In the above example we have used "Larry" and "Google" as parameters and which are hardcoded; But if you are working for an application and if it's URL/Username/Password keeps changing then you cannot hard code them.

In such cases, we have to change it based on the build. For this Purpose, Jenkins provides a way for parameterization.

  • Go to configure
    configure-jenkins-parameter
  • Under the General Section, Select the option This project is parameterized
    parameter-jenkins
  • Select String Parameter from the Add Parameter dropdown
    string-parameter-jenkins
  • Add one more String parameter and set values (description filed is edited due to space constraint)
    jenkins-parameters
  • Now Go to the Build Section and edit the code according to the names given to those parameters
    paramter-script-jenkins
  • Save The Job
  • Now you will see the option as Build with Parameters rather than Build, click itbuild-with-paramaters-jenkins
  • Jenkins will prompt to enter the values for the Parameter, you can ignore if you want to use default values, otherwise, you can change them; then click on the Build button
    build-parameter-jenkins
  • The output is
    build-parameters-jenkins-output
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions