Entorn d'integració continua amb Docker Compose, Traefik i Jenkins.

Entorn d'integració continua amb Docker Compose, Traefik i Jenkins.

Integració continua

Docker pot ser una eina molt poderosa per fer proves, especialment si un comença a construir una infraestructura d'integració contínua amb solucions de codi obert. A més, amb docker compose es poden crear fàcilment contenidors d'aplicacions que es comuniquen entre si mitjançant xarxes internes.

En aquesta publicació mostraré com crear una plataforma d’integració contínua per desplegar fàcilment microserveis en contenidors utilitzant Jenkins com a eina d’automatització, amb tot al mateix host Docker.

Per aconseguir-ho, haurem de configurar un proxy invers per exposar el port 80 a la resta del món. Per aquesta tasca, utilitzarem Traefik ( https://docs.traefik.io/ ).

Traefik és un servidor intermediari invers compatible amb Docker que inclou el seu propi tauler de control. Utilitzarem Traefik per encaminar les sol·licituds entre els diferents contenidors d'aplicacions web.

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:


  • 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.


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'
    driver: bridge

    # 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
      # The HTTP port
      - "80:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
      - proxy
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
      # name of the recently created image
    image: jenkins-with-docker-socket:lts-alpine
       - "/var/run/docker.sock:/var/run/docker.sock"
       - proxy
      - "traefik.http.routers.jenkins-master-docker.rule=Host(`jenkins.docker.localhost`)"
      - "traefik.http.services.jenkins-master-docker.loadbalancer.server.port=8080"
    # 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 Traefik monitoring dashboard by pointing your browser to http://localhost:8080 .

Traefik monitoring dashboard
Traefik monitoring dashboard

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

Jenkins dashboard
Jenkins dashboard

This is all for today!

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

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

Necessites ajuda amb aquest tema?

Estaré encantat d'atendre't.

Contacta per més informació: aqui 

You can give me support if you find this content is usefull!

Buy Me a Coffee

Deixa un comentari

L'adreça electrònica no es publicarà.