fbpx

Continuous Integration environment with Docker compose using Traefik and Jenkins.

Continuous Integration environment with Docker compose using Traefik and Jenkins.

Continuous integration

Docker can be a very powerful tool for testing , especially if you are just starting to build an continuous integration infrastructure with open source solutions. In addition, with docker compose you can easily spin up  application containers and communicate each other using internal networks.

In this post I’m going to show how to create a continuous integration platform to easily deploy containerized microservices using Jenkins as automation tool, all in the same Docker host.

In order to achieve this we will need to set up a reverse proxy to expose port 80 to the rest of the world. In this endeavour , we will use Traefik ( https://docs.traefik.io/ ).

Traefik is a docker compliant reverse proxy that includes its own monitoring dashboard. We will use Traefik to route the requests among different web application containers.

Jenkins1 is very popular an open source automation server that supports continuons delivery pipelines as code via the pipeline domain-specific language (DSL) syntax, written in a text file called Jenkinsfile.2. This support comes in two flavous, declarative pipeline syntax and scripted pipeline syntax.

Declarative pipeline is a more modern and recent feature of Jenkins pipeline which is designed to make writing and reading code easier and provides richer syntactical features.

In this example I’m going to show a Jenkins declarative pipeline that is able to create and deploy new microservice containers in the docker host. To accomplish this task, we will need to bind-mounting the docker socket into our Jenkins container . Thus we will use a Jenkins docker image with the docker socket bind-mounted.3

Once the microservice is deployed, the “magic” happens when Traefik automatically discovers which service serves which request.

This stack is composed by 2 docker containers:

Prerequisites

  • Docker installed in your server.
  • Docker composed installed in your server.

I use Ubuntu 18.04.2 LTS inside WSL2 for Windows 10 within the Insider Program. In addition you can install Docker in any operative system or linux distribution.It’s beyond the scope of this post to explain how to install Docker or WLS2 for Windows 10. You can find more information at https://docs.docker.com/get-docker/ and https://docs.microsoft.com/en-us/windows/wsl/install-win10

Building latest Jenkins Docker Image

If you want the latest Jenkins version in your container you will need to build your own image since Jenkins version of this image in the docker hub is out of date.

To create the docker custom image, you first need to download the Dockerfile project.

git clone https://github.com/jareddlc/jenkins-with-docker-socket  jenkins-with-docker-socket

Switch to your prefered docker image version, for example, if you decide to use Jenkins lts-alpine image, you need to change the working directory to lts-alpine:

cd jenkins-with-docker-socket && cd lts-alpine

Build Jenkins image running docker-build.sh script.

./build.sh

The resulting imatge name will be jenkins-with-docker-socket and the tag lts-alpine . You can list this newly created docker image with the command:

docker image ls

Setting up Docker-compose

For Traefik to recognize our applications, they must be part of the same network. We specify the network name of proxy and we use default bridge network provider.

Set the name of the recently created image in docker-compose jenkins service section configuration. In this case, jenkins-with-docker-socket:lts-alpine.

In Jenkins service declaration, we set up one docker label that tell Traefik to direct traffic to the hostname jenkins.docker.localhost to port :8080 within the Jenkins container, exposing the Jenkins application.

version: '3.6'
networks:
  proxy:
    driver: bridge

services:
  reverse-proxy:
    # The official v2 Traefik docker image
    image: traefik:v2.2
    # Enables the web UI and tells Traefik to listen to docker
    command: --api.insecure=true --providers.docker
    ports:
      # The HTTP port
      - "80:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    networks:
      - proxy
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
  jenkins-master-docker:
      # name of the recently created image
    image: jenkins-with-docker-socket:lts-alpine
    volumes:
       - "/var/run/docker.sock:/var/run/docker.sock"
    networks:
       - proxy
    labels:
      - "traefik.http.routers.jenkins-master-docker.rule=Host(`jenkins.docker.localhost`)"
      - "traefik.http.services.jenkins-master-docker.loadbalancer.server.port=8080"
            
    environment:
    # you can set your preferred time zone
      - "TZ= Europe/Madrid"
      - "JAVA_OPTS=-Djenkins.install.runSetupWizard=false -Dhudson.footerURL=http://jordimarti.tech"
      

With this file, run the containers using docker-compose:

docker-compose up -d

With the containers started, you now have a dashboard you can access to see the health of your containers. You can also use this dashboard to visualize the frontends and backends that Traefik has registered. Access the monitoring dashboard by pointing your browser to http://localhost:8080 .

You can access Jenkins application pointing the browser to the following URL: http://jenkins.docker.localhost

This is all for today!

Stay tunned to discover the next steps regarding Jenkins pipeline configuration!!!

Ofereixo la gestió i planificació de projectes web de forma externa, així com el lideratge tècnic d’un equip de desenvolupadors.

Contacta’m per més informació.
Whatsapp

  1. https://www.jenkins.io/ []
  2. https://www.jenkins.io/doc/book/pipeline/syntax/ []
  3. https://github.com/jareddlc/jenkins-with-docker-socket []
Written by :

Jordi Marti

Telecomunications Engineer graduated in ETSETB Barcelona. I worked as full stack software engineer, web developer, business analyst and project manager. I'm a big fan of IoT embedded systems design using low power PANs I'm passionate about my job and positive and I'm very good at it. I enjoy learning, participate & innovate. Always thinking & innovating new strategies / activites in order to boost efficiency , productivity and continuous improvement.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish
caCatalan es_ESSpanish de_DEGerman en_USEnglish