Deploy Jenkins with Docker and Ansible

Ansible roles for deploying a Jenkins instance on Debian within a Docker container behind nginx.

Install Docker

Use ansible's apt module to install the Docker engine as a system service:

- name: Add Docker Group
  group: name=docker state=present

- name: Add Docker Signing Key
  apt_key:
    keyserver: 'hkp://p80.pool.sks-keyservers.net:80'
    id: 58118E89F3A912897C070ADBF76221572C52609D
    state: present

- name: Add Docker Repo
  apt_repository:
    repo: 'deb https://apt.dockerproject.org/repo debian-jessie main'
    filename: 'docker.list'

- name: Install Docker
  apt: name=docker-engine state=present

- name: Install docker-py
  pip: name=docker-py version=1.9.0

Install Jenkins

The official Jenkins container image will install application files to /var/jenkins_home within the container, and this directory needs to be available outside the container, so use the Docker -v option to map the volume to, say, /share/volumes/jenkins on the host:

- name: Ensure jenkins directory on docker host
  file:
    state: directory
    owner: 1000
    group: 1000
    path: /share/jenkins

- name: Pull the latest official jenkins docker image
  docker_image:
    name: "jenkins:latest"

- name: Create a container from the jenkins docker image
  docker_container:
    name: "jenkins-server"
    image: "jenkins"
    ports:
        - "8080:8080"
        - "50000:50000"
    volumes:
        - "/share/jenkins:/var/jenkins_home"
    state: present
    recreate: no

Create Service

Now create a service script to allow starting and stopping of the container:

[Unit]
Description=Jenkins in a Docker container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a jenkins-server
ExecStop=/usr/bin/docker stop -t 2 jenkins-server

[Install]
WantedBy=default.target

Copy this to the appropriate place and enable the service:

- name: Copy systemd service script to start and stop the jenkins container
  copy: src=files/jenkins.service dest=/etc/systemd/system

- name: Reload systemctl
  shell: systemctl reload-or-restart docker-jenkins

- name: Enable the docker-jenkins service
  shell: systemctl enable docker-jenkins

See the next post for configuring nginx to act as a reverse proxy for the container.

Powered by Pelican and ZURB Foundation.