Run Jenkins & SonarQube in Docker

Jenkins & SonarQube running in a docker services.

Let us create a YML file for the configuration to run the Jenkins & Sonarqube in the same environment in a docker stack.

#######################################
# Jenkins & Sonar docker stack
# Kathirvel Gopal
########################################
version: "3.7"
networks:
devops:
name: devops
driver: overlay
volumes:
jenkins-data: {}
jenkins-docker-certs: {}
sonarqube_data: {}
sonarqube_extensions: {}
sonarqube_logs: {}
sonarqube_temp: {}
postgresql: {}
postgresql_data: {}
services:
jenkins_service:
image: 'jenkinsci/blueocean'
networks:
- devops
ports:
- target: 8080
published: 8080
protocol: tcp
- target: 50000
published: 50000
protocol: tcp
volumes:
- jenkins-data:/var/jenkins_home
- jenkins-docker-certs:/certs/client
sonarqube_server:
image: 'sonarqube:latest'
depends_on:
- db
networks:
- devops
ports:
- target: 9000
published: 2020
protocol: tcp
environment:
SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: 'true'
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
- sonarqube_temp:/opt/sonarqube/temp
db:
image: 'postgres:latest'
networks:
- devops
environment:
POSTGRES_USER: sonar
POSTGRES_PASSWORD: sonar
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data

Step 1: We have to create a YML file docker-compose.yml and add the network & Volumes for the jenkins & sonarqube.

Step 2: Add a official image of Jenkins & Sonar from the docker Hub. so login in the docker hub and pull the image using the below command.

docker pull jenkinsci/blueocean sonarqube:latest

Step 3: Then create a service and configure the Port & Volume. By default jenkins run on the port 8080. Whereas sonarqube run on 9000 by default.So we published the sonarqube on port 2020.

Step 4: Finally i have added one more service called “db”. Here i am using the image “postgres:latest”. Since we are using the sonarqube we can add more plugins into that. So in my case we use the sonarqube scanner, it produce the report so we need a database to have that. So we are using “postgres”.

Step 5: You can run the stack by using the below command.

sudo docker stack deploy -c docker-compose.yml jenkinserver --with-registry-auth

You can access the jenkins using http://<YOURIP>:8080 and sonarqube using http://<YOURIP>:2020.

Now you can configure your project in jenkins.

Thanks.

Team lead at Contus Support Interactive PVT LTD.