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 a directory to store jenkins state
mkdir -p $HOME/jenkins_home

# start the jenkins container using that directory
docker run \
-d \
--name jenkins-container \
-p 8080:8080 \
-p 50000:50000 \
-v $HOME/jenkins_home:/var/jenkins_home \
jenkins

After the container has been deployed, you will want to run this command to get your "secret" to unlock Jenkins.

cat $HOME/jenkins_home/secrets/initialAdminPassword

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

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

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` 
tar --create --gzip --file "jenkins_home_$DATE.tar.gz" $HOME/jenkins_home

echo "Updating Jenkins Image..."
docker pull jenkins

echo "Re-deploying Jenkins..."
docker kill jenkins-container

docker rm jenkins-container

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

echo "complete!"
echo ""

References