Skip links

Deploy SonarQube 8.9 with Docker on Ubuntu, and set GitLab CI integration

SonarQube is one the most popular static analysis tool, mainly because it exists since 2008 and is open-source, easing its adoption in the IT industry.

In this post, we’ll show how to deploy the latest SonarQube LTS Community Edition so far (8.9), with a PostgreSQL DB and using Docker. We’re using Ubuntu 20.04 but, as we use Docker, you may only have to adapt the Docker installation and hardware requirements sections. We’ll finally demonstrate how to run a SonarQube analysis using GitLab CI for a Java project.

🚢 Install Docker and Docker Compose

If not already done, the first step is to install Docker and Docker Compose. The official documentation already has everything you need, but we’ll give you the basics steps for a fresh install of Docker on your server :


					
				

Logout and Login again to your server, you should be able to run


					
				

And now you can install Docker Compose :


					
				

🏵️ Adjust OS settings

SonarQube has some hardware requirements : you should increase the values for those 2 OS settings :

  • fs.file-max: “The value in file-max denotes the maximum number of file- handles that the Linux kernel will allocate. When you get lots of error messages about running out of file handles, you might want to increase this limit.”
  • vm.max_map_count: “the maximum number of memory map areas a process may have. Memory map areas are used as a side-effect of calling malloc, directly by mmap and mprotect, and also when loading shared libraries.”

You should modifiy as root user the /etc/sysctl.conf file to set permanently the values for these 2 parameters :


					
				

Then add this content to the file :


					
				

Save, close, and then reload the settings :


					
				

🚀 Run SonarQube

Set the following docker-compose.yml file to run both SonarQube and PostgreSQL 12 containers :


					
				

It would be best if you changed the default password.

You’re now ready to run your SonarQube 🚀


					
				

You can check the evolution of the logs using docker-compose logs -f command. SonarQube is ready when the following logs are displayed :


					
				

Wait a little bit and you should get the following screen on http://localhost:9000 :

The next steps will to change the default password for the admin account, and now you’ll be ready to start.

🧪 Run analysis on GitLab CI for a Java project

We’re now going to run a SonarQube analysis on a project example written in Java 11 and configured with Maven. We also use JUnit and Jacoco to produce code coverage reports. Here is the pom.xml file :


					
				

Here is the .gitlab-ci.yml file to be put at the root of the repository. We have two steps here : one to compile the project and run the unit tests, and the second to run SonarQube analysis. Note that the image maven:3.6.3-jdk-11 is publicly available. If you’re not using Maven, you should browse the SonarQube documentation to check how to run analysis from your context.


					
				

On GitLab CI, you should set 2 environment variables :

  • SONAR_HOST_URL : This is the full URL of your SonarQube instance.
  • SONAR_TOKEN : An API token, follow this documentation to generate one.

To achieve that GitLab, go to your project/group settings → CI/CD → Variables section. Then you can add the 2 values.

Once you’ve pushed your .gitlab-ci.yml, your first SonarQube analysis should start, and you should get something like this : 🤗

That was quite simple, don’t you think? 🙂

Going further with SonarQube

At Promyze, we commonly help our customers with their projects using SonarQube: setup, migration of versions, configuration, integration with CI/CD, and so on. Such code quality platforms help to define coding rules repositories shared across teams.

Unfortunately, automatic tools can’t detect all kinds of issues. That’s why developers are doing code reviews and create custom best practices wikis. Promyze is designed for such a purpose; take a tour and check how Promyze can help you to define and share your best coding practices! 🚀

Also, feel free to comment to ask for clarification or suggest improvements to this post. 🙏🏽