Skip to content

SonarQube

Joan Bellver edited this page Mar 7, 2022 · 5 revisions

What we are going to do in the following chapters is creating a Jenkins pipeline which will include the use of SonarQube and Nexus. All of these services will be launched using a docker-compose file, but the explanation of each service will be on its corresponding chapter. But we are going to create this file now.

First inside our main project directory (C:\...\workspaces\main\jumpthequeue) we will create a new folder called cicd where we will create a new docker-compose file with the following code:

version: '3'

services:
    jenkins:
        user: root
        image: jenkins/jenkins
        ports:
            - '8080:8080'
            - '50000:50000'
        container_name: 'jenkins'
        networks:
            - cicd
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock

    sonarqube:
        image: sonarqube:latest
        container_name: 'sonarqube'
        depends_on:
            - db
        ports:
            - '9000:9000'
        networks:
            - cicd
        environment:
            - SONAR_JDBC_URL=jdbc:postgresql://db:5432/sonar
            - SONAR_JDBC_USERNAME=sonar
            - SONAR_JDBC_PASSWORD=sonar

    db:
        image: postgres
        container_name: 'db'
        networks:
            - cicd
        environment:
            - POSTGRES_USER=sonar
            - POSTGRES_PASSWORD=sonar

    nexus:
        image: sonatype/nexus3
        ports:
            - '8085:8081'
            - '8090:8082'
        networks:
            - cicd
        volumes:
            - /nexus-data:/nexus-data

networks:
    cicd:
        driver: bridge

What is SonarQube?

SonarQube is the leading tool for continuously inspecting the code quality and security of your codebases and guiding development teams during code reviews.

We will be using this tool inside our Jenkins pipeline for checking the quality and security of our code.

Launching SonarQube

As you can see in the docker-compose that we have done there are four services, but the ones related to SnarQube are only the ones named sonarqube and db:

sonarqube:
        image: sonarqube:latest
        container_name: 'sonarqube'
        depends_on:
            - db
        ports:
            - '9000:9000'
        networks:
            - cicd
        environment:
            - SONAR_JDBC_URL=jdbc:postgresql://db:5432/sonar
            - SONAR_JDBC_USERNAME=sonar
            - SONAR_JDBC_PASSWORD=sonar

    db:
        image: postgres
        container_name: 'db'
        networks:
            - cicd
        environment:
            - POSTGRES_USER=sonar
            - POSTGRES_PASSWORD=sonar

What we are doing here is setting a services called sonarqube which gets the latest image of sonarqube and and creating a new container called sonarqube which is going to be exposed on the port 9000. We can also see that it depends on another services called db (which is connected to the same network cicd) and has some environment variables which will help us to connect into the database.

The other service is db, which gets the image postgres (for a postgreSQL database) and creates a container named db.

With this docker-compose set up we can now launch our containers using the order:

docker-compose up -d

And now we are ready to go into http://localhost:9000 and start setting up our SonarQube.

⚠️ If the sonarqube container does not start, you should go into your Linux distribution and enter the following order: sudo sysctl -w vm.max_map_count=262144.

Setting up SonarQube

Once your container is running, you will be able to access the SonarQube environment by entering http://localhost:9000 in your web browser. A login page will pop up asking you for the credentials, if it is the first time you access the page the username is admin and the password is admin, after entering you will be asked to change the password for future entries.

For now this is it with SonarQube, we will come back afterwards to set up a few things in order to connect with Jenkins.


Next Chapter: Nexus

Clone this wiki locally