fbpx

Kontinuierliche Integration mit Docker Compose, Traefik und Jenkins.

Kontinuierliche Integration mit Docker Compose, Traefik und Jenkins.

Kontinuierliche Integration

Docker kann ein sehr leistungsfähiges Tool zum Testen sein, insbesondere wenn Sie gerade erst mit dem Aufbau einer kontinuierlichen Integrationsinfrastruktur mit Open Source-Lösungen beginnen. Darüber hinaus können Sie mit Docker Compose problemlos Anwendungscontainer hochfahren und über interne Netzwerke miteinander kommunizieren.

In diesem Beitrag werde ich zeigen, wie Sie eine kontinuierliche Integrationsplattform erstellen, um mithilfe von Jenkins als Automatisierungstool auf demselben Docker-Host problemlos containerisierte Mikrodienste bereitzustellen.

Um dies zu erreichen, müssen wir einen Reverse-Proxy einrichten, um Port 80 dem Rest der Welt zugänglich zu machen. In diesem Bestreben verwenden wir Traefik ( https://docs.traefik.io/ ).

Traefik ist ein Docker-kompatibler Reverse-Proxy mit einem eigenen Überwachungs-Dashboard. Wir werden Traefik verwenden, um die Anforderungen zwischen verschiedenen Webanwendungscontainern weiterzuleiten.

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 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 []

Benötigen Sie Hilfe zu diesem Thema?

Ich werde Ihnen gerne weiterhelfen.

Bitte kontaktieren Sie uns für weitere Informationen: here 

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

Buy Me a Coffee

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

de_DEGerman