SonarQube installation on docker container with docker-compose


SonarQube is being widely used to manage code quality and code analysis. It is an open source tool that performs code inspection to detect issues in the code. 

In this post we will learn how to install SonarQube on a docker container. We will install community version LTS edition of SonarQube server. For database we will create another container and install postgresql server with latest version. We will have these 2 containers talk to each other via a network.

Learn more about SonarQube:

SonarQube integration with Jenkins

Read more on SonarQube Click here

Learn more about Docker:

Simple Jenkins Declarative Pipeline to Push Docker Image To Docker Hub

Read more on Docker Click here

Overview of Docker-compose

With docker-compose, we can automate the process of creating and running multiple containers. It also makes it easy to setup the network for containers to be able to connect to each other. 

With docker-compose, we write all the commands required to run the container or multiple containers in a file in a structured manner. So all we need to do is to run a single command to bring multiple containers either up or down. 

We will go through the steps one by one, you need to just follow along.

Pre-requisite

For the purpose of this post, we assume Docker is already installed on the system. If not then please install it from official docker website. If you already have docker installed but do not have docker compose installed, then please go ahead and install Docker-compose based on your Linux distribution from here.

Create Docker-compose file

To write a docker-compose file, we need to start by mentioning the version of docker compose we would be using in the first line. As shown below we are using version “3”

version: '3'

Create SonarQube container

Next step is to list the containers that we want to create. This will be mentioned under services, followed by container names and their specifications. 

services:

    <container name>:

    image:

Following the above structure, first we will create the SonarQube container as follows 

services:
    sonarqube:
        image: lts-community

We will use the community edition of SonarQube from Docker Hub with the latest stable version with long term support.

Next step is to map the ports. 

port: '9000:9000'

Here we are mapping port 9000 of our host machine to the port 9000 (default port for SonarQube web server) of docker container. 

Then we need to setup environment variables. 

environment:
    sonar.jdbc.username= sonar
    sonar.jdbc.password= sonar
    sonar.jdbc.url= jdbc:postgresql://db:5432/sonarqube

Post this we will setup volumes for persistent storage of SonarQube data. 

volumes:
    sonarqube_conf: /opt/sonarqube/conf
    sonarqube_data: /opt/sonarqube/data
    sonarqube_extensions: 
/opt/sonarqube/extensions
    sonarqube_bundled-plugins: /opt/sonarqube/lib/bundled-plugins

SonarQube will create a default network with a default name for multiple containers to interact with each other. 

For our understanding, we will specify a network name explicitly. 

networks:
    sonarnetwork

Create Postgres container

Similarly we will create a container for postgres database. 

db:
    postgres:
        image: postgres
        networks:
            - sonarnetwork
        environment:
            POSTGRES_USER= sonar
            POSTGRES_PASSWORD= sonar
        volumes:
            postgresql: /var/lib/postgresql
            postgresql_data: /var/lib/postgresql/data       

Resources to be created by Docker-compose file

Post container creation details we will specify all the networks to be used for container interaction. In our case, we have just one network.

Bridge is the default network driver created by compose. We have mentioned the network driver as bridge for our clarity.

networks:
    sonarnetwork:
        driver: bridge

Then we will list all the volumes required by containers in this compose file. 

volumes:
    sonarqube_conf:
    sonarqube_data: 
    sonarqube_extensions:
    sonarqube_bundled-plugins:
    postgresql:
    postgresql_data:

As we have created volumes for persistent storage, we will be able to use the stored data when we bring up the containers once again.

Complete Docker-compose file

In this post we have learnt how to install SonarQube on docker container with persistent storage. 

Let us now list the complete docker compose file that we have created. 

version: '3' 
services:
    sonarqube:
        image: lts-community
        port: 9000:9000
        environment:
            sonar.jdbc.username= sonar
sonar.jdbc.password= sonar
            sonar.jdbc.url= jdbc:postgresql://db:5432/sonarqube
        volumes:
            sonarqube_conf: /opt/sonarqube/conf
            sonarqube_data: /opt/sonarqube/data
            sonarqube_extensions: /opt/sonarqube/extensions
            sonarqube_bundled-plugins: /opt/sonarqube/lib/bundled-plugins
        networks:
            sonarnetwork
    db:
        image: postgres
        networks:
            – sonarnetwork
        environment:
            POSTGRES_USER= sonar
            POSTGRES_PASSWORD= sonar
        volumes:
            postgresql: /var/lib/postgresql
            postgresql_data: /var/lib/postgresql/data
    networks:
        sonarnetwork:
            driver: bridge
    volumes:
        sonarqube_conf:
        sonarqube_data:
        sonarqube_extensions:
        sonarqube_bundled-plugins:
        postgresql:
        postgresql_data:

Create containers using docker-compose

As we have automated the container creation using docker compose, we will be able to use this file to bring the containers up or down whenever required. 

We can save this file as docker-compose.yml or docker-compose.yaml. With this file name, we won’t need to specify the filename when running the docker-compose command from the same directory. 

However, if we want to save it as a different name e.g. sonar.yml, then we will need to use -f option and then the file name, shown as follows 

docker-compose -f sonar.yml up

In the console, we will see the docker compose commands fot creating the containers.

On our host machine we should be able to access SonarQube installed on the docker container via http://localhost:9000 or via http://<ipaddressofhost>:9000 from another system. This is because we have mapped the container port running SonarQube with the same port number i.e. 9000 on the host machine.

SonarQube Login
Image : SonarQube Login Page

When we want to bring down all the containers from the docker-compose file, we will need to run the following command

docker-compose – f sonar.yml down

Conclusion

In this post we have seen how to install SonarQube from the image for SonarQube community edition on Docker Hub. Then, we have also installed postgresql image on the same network. After this, we have created volumes, so that are able to access SonarQube data even after the containers have been restarted. Finally, we have mapped the ports of the docker container to the host machine so that we are able to access SonarQube via host machine.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *