Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Deploy Jenkins With Docker

I've previously written about installing jenkins to a server, but now we can deploy the tool easily with docker.

Steps

If you haven't already installed docker, I have tutorials on how to do so on various distributions.

Running the script/commands below will create a folder in your home directory and deploy Jenkins through docker, which will use that directory to store it's state.

Create Volume / Storage Area

First we need to create a folder to act as a volume that will hold our Jenkins server's state:

mkdir -p $HOME/jenkins

Deploy Jenkins

Then you can run the following to deploy Jenkins through Docker.

docker run \
  --name jenkins \
  -p 8080:8080 \
  -p 50000:50000 \
  -v $HOME/jenkins:/var/jenkins_home \
  jenkins/jenkins:lts

If you install docker-compose, then you can use the following docker-compose.yml file instead which I find makes things easier.

version: "3"

services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins:lts
    privileged: true
    restart: always
    ports:
      - "8080:8080"
      - "5000:5000"
    volumes:
      - ${HOME}/jenkins:/var/jenkins_home

volumes:
  jenkins:
    driver: local

Running Jenkins With Docker

If you want jenkins to be able to use docker containers, then you want to use the following instead:

version: "3"

services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins:lts
    user: 0:0
    privileged: true
    restart: always
    ports:
      - "80:8080"
      - "5000:5000"
    volumes:
      - ${HOME}/jenkins:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker

volumes:
  jenkins:
    driver: local

For more information, refer here.

Optional - Use Specific Jenkins Version

The commands above will pull and use the latest LTS version. However, sometimes there can be issues with plugins for the latest version, and you may wish to stick to an older version. You can find the version names by going to the LTS changelog page.

Then search for that version in the tag filter of the Docker Hub page.

That will find you the appropriate tags that you can put on the end. E.g. :2.319.3-lts instead of :lts.

Watch Output For Initial Admin Password

Watch the output as you will see your password print out for you. E.g.

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

20adb45aeeb24dc0871bb364c8eb4700

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

If you fail to see the secret, you can run this command to fetch your secret

cat $HOME/jenkins/secrets/initialAdminPassword

Open Jenkins In Browser

Now navigate your server's IP or hostname in the browser, but with port 8080 attached at the end like so:

http://jenkins.programster.org:8080

Follow Setup Guide

Plug that "secret" into the form you see when the page loads.


Next we will be prompted for installing plugins. I will go with installing the suggested ones for now.


Wait for the plugins to install. They should all install successfully.


Create a user for logging in with in future.


Set the site URL for your Jenkins server. You probably want to leave this with its pre-populated value.


You've now finished setting up Jenkins...


... and should now be able to see the dashboard.

Updating Jenkins

After a while, it's probably a good idea to try to pull the Jenkins image again to ensure you have the latest version. Before you do this, you should probably back up your data just in case. The script below takes care of all of this for you.

#!/bin/bash
echo "Backing up your data..."
DATE=`date +%Y_%m_%d` 
sudo tar --create --gzip --file "jenkins_$DATE.tar.gz" $HOME/jenkins

echo "Updating Jenkins Image..."
docker pull jenkins/jenkins:lts

echo "Re-deploying Jenkins..."
docker stop jenkins

docker rm jenkins

docker run \
  -d \
  --name jenkins \
  -p 8080:8080 \
  -p 50000:50000 \
  -v $HOME/jenkins_home:/var/jenkins_home \
  jenkins

echo "complete!"
echo ""

If you are using docker-compose, then you could do the following:

#!/bin/bash
echo "Backing up your data..."
DATE=`date +%Y_%m_%d` 
sudo tar --create --gzip --file "jenkins_$DATE.tar.gz" $HOME/jenkins

echo "Updating Jenkins Image..."
docker-compose pull

echo "Re-deploying Jenkins..."
docker-compose stop
docker-compose up -d

echo "complete!"
echo ""

References

Last updated: 31st March 2022
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch