Jenkins and Docker compose setup with persistent Jenkins $HOME volume

2017-03-08

This is a short guide on how to set a single node Jenkins server using Docker compose.


As software engineers and DevOps practitioners we love abstractions, codified and disposable infrastructure. In this tutorial I’m going to show how to use Docker compose to set a single node Jenkins server with Jenkins persistent home directory mounted on the host (OSX) filesystem.

Why Docker?

Few years ago I would have used Vagrant, which I still love it. However, Vagrant requires to spin up a full virtual machine with all its nuts and bolts. Docker, in the other hand takes the full advantage of containers in terms of performance and time to boot.

Whether whether you’d like to get a deep insight on the topic, here is a interesting link about comparing containers to virtual machines.

Requirements.

In order to complete this tutorial you need to have Docker and Docker-compose installed, the installation of such tools is not in the scope of this tutorial. Please find the installation guides from the below links:

https://www.docker.com/docker-mac

https://docs.docker.com/compose/

Setup

To get started with our demo we need to create a compose file. Docker compose has a neat declarative syntax to define single of group of containers along with additional properties such networking and volumes.

docker-compose.yml example:

version: '2'

services:
  web:
    image: jenkins/jenkins:lts
    ports:
     - "5000:5000"
     - "8080:8080"
    volumes:
     - ${PWD}/jenkins:/var/jenkins_home

Once the compose file has been created it is possible to start the Jenkins container with the following command:

docker-compose up -d

Docker compose arguments:

up - Create and start containers

-d - Detached mode: Run containers in the background

At this point Docker engine will connect to Docker-Hub and pull down the latest Jenkins image and run a container instance of it along mounting the Jenkins HOME in the current host directory. While Jenkins successfully starts an initial admin password would be created, this password is required as first login to set a new administrative user.

The initial administrative password can be retrieved with:

cat jenkins/secrets/initialAdminPassword

or with:

docker-compose logs

Now you can point your browser to http://127.0.0.1:8080/ to enjoy Jenkins.

Rollback

Whether you’d like to start from a fresh state, you have to stop the container and delete the Jenkins home directory and run docker compose up again.

Cleanup:

docker-compose stop
rm -rf jenkins

Jenkins up:

docker-compose up -d