Main Image

One thing really missing from everywhere is a way to run Jenkins on Docker with Docker agents and Testcontainers. It's not as easy as it seems! * Docker will be exposed on the Jenkins master network via a Socat ("""proxy""") image * The build nodes will be created dynamically on request * The build could use Testcontainers out of the box * Docker will not be exposed to the outside world! * No need for privileged containers or weird CAPs *

The stack

Let's first create the stack with docker compose

version: '3.8'
networks:
  jenkins:
    driver: bridge
services:
  jenkins:
    networks: 
      - jenkins
    image: jenkins/jenkins:lts
    user: root
    environment:
     - DOCKER_HOST=tcp://socket-proxy:2375
    ports:
     - 8011:8080
     - 50000:50000
    container_name: jenkins
    volumes:
     - jenkins_home:/var/jenkins_home
  socket-proxy:
    image: alpine/socat
    container_name: socket-proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    restart: unless-stopped
    networks: 
      - jenkins
    command: Tcp-listen:2375,fork,reuseaddr unix-connect:/var/run/docker.sock
volumes:
  jenkins_home:

The whole server is based on the exposure of the docker deamon inside the jenkins network through the socat image that act as a proxy for port 2375 to the local docker.

Jenkins server

Now you can * Get the logs of jenkins -container- and the temporary password and login

Image

Image

Image

Image

Image

Image

Java/Maven configuration

Image

Simple build with docker!

Image

echo "build done"

Image

And here is the build result!

Image

Running a Testcontainer based project

Now to run the standard testcontainer Spring Boot example. It runs on Jdk 17 so we have first to add the jdk

Then we can create a new build

pipeline {
    agent { label 'dockeragent'}
    tools {
        maven 'maven399'
        jdk 'jdk17'  }
    stages {
        stage('Build') {
            steps {
                git branch: 'main',
                    url:'https://github.com/testcontainers/testcontainers-java-spring-boot-quickstart.git'
                sh "mvn package"   }  } }
    post {
        always {
            archiveArtifacts artifacts: 'target/**/*.jar', fingerprint: true
            junit 'target/surefire-reports/*.xml'
        }  }  }

Image

And here is the result, with the downloadable artifact and the progress of test results

Image


Last modified on: September 20, 2024