Running Jenkins in Docker

Requirements

I went to an FSOSS 2016 [1] conference and I found one talk on Jenkins to be particularly interesting.

I wanted to try Jenkins, without configuring Java on my laptop or setting up a VM. Using Jenkins docker container would be the easiest solution for this kind of problem.

Prerequisites

This article assumes Docker [2] and Docker Compose [3] have been set up.

Jenkins container

I used an official container built by Jenkins community [4].

The container comes with these settings:

  • Jenkins server runs on ports 5000 and 8000
  • Jenkins settings saved to /var/jenkins_home and the directory is exposed as a docker volume
  • user / group ids are set to jenkins / jenkins with uid/gid set to 1001/1001.

Volume Setup

Docker volume settings, specify container directory mapping between host and container [5], allowing files generated inside of the container to persist. The host system will store the data in /srv/dockercontainer/jenkins_home.

Following steps to set up docker volume and permission mapping on the host.

  1. Create docker volume path on the host
# mkdir /srv/dockercontainer/jenkins_home
  1. Set up user/group mapping on the host.
# groupadd --gid 20000 jenkins
# useradd --home-dir /srv/dockercontainer/jenkins_home \
   --uid 20000 --gid 20000 -s /bin/false -M jenkins
# chown -R jenkins:jenkins /srv/dockercontainer/jenkins_home

Docker compose file

docker-compose.yml specifies startup settings for Jenkins container.

version: '2'
services:
  jenkins:
    image: jenkins
    user: "20000:20000"
    ports:
      - "5000:5000"
      - "8080:8080"
    volumes:
      - /srv/dockercontainer/jenkins_home:/var/jenkins_home
  • image -- the directive specifies container to use, in this case it's Jenkins container [4].
  • user -- user/group mapping, 'uid:gid' from the container is mapped 'uid:gid' of the host.
  • ports -- port mapping from the container to the host.
  • volumes -- directory (docker volume) mapping, between the host and the container.

Starting up the container

Go to the directory containing docker-compose.yml.

The following command starts the container in foreground, the container needs to be run in the foreground, as during the setup, system activation code for Jenkins will be printed to the console.

# docker-compose up

Open a browser and go to http://localhost:8080. Once initial setup is completed, stop the container with Ctrl+C, then start it as a daemon.

# docker-compose up -d

To shut down the container running in daemon mode, execute the following.

# docker-compose stop

Deleting the container

To remove container setup, run the following command from the directory where docker-compose.yml is located.

# docker-compose rm jenkins

The data generated by Jenkins in docker container is saved in /srv/dockercontainer/jenkins_home with user 'jenkins' and group 'jenkins'.

[1]FSOSS 2016 -- http://cdot.fsoss.ca/2016/
[2]Docker installation on Debian -- https://docs.docker.com/engine/installation/linux/debian/
[3]Install Compose -- https://docs.docker.com/compose/install/
[4]Official Jenkins Docker image -- https://hub.docker.com/_/jenkins/
[5]Manage data in containers -- https://docs.docker.com/engine/tutorials/dockervolumes/