Setup SonarQube with Docker

With this post we are going to walkthrough how to setup SonarQube using Docker.

During this walkthrough we make use of the following system and software:

  • Windows 10, version 20H2
  • Docker Desktop, version 3.0.4 with WSL 2
  • SonarQube, version 8.5.1

Install Docker

Docker desktop can be downloaded from docker.com/get-started, after downloaded Docker Desktop Installer.exe run it and follow the instructions. If request to install and configure WSL2, follow the instructions.

After the installation you are ready to start Docker for the first time. Probably a shortcut is added to your desktop, else you can find it in the default location C:\Program Files\Docker\Docker\Docker Desktop.exe.

When starting Docker for the first time it can take a while before all processes are booted and the docker environment is ready.

Configure with WSL 2

For the best performance and the latest features we advice to use WSL 2. During installation it's probably already requested, else follow the instructions on /docs.docker.com/docker-for-windows/wsl/.

Verify docker is installed correctly

Now we have installed and configured Docker we want to verify all is running as expected. Also if you are new to Docker this flow helps you to understand and get started with Docker. Docker provides a good documentation in the form of getting-started or a quick tutorial from the empty dashboard.

Prepare SonarQube compose file

Before we can start and run SonarQube we first need to prepare a docker-compose file.
What is a docker-compose file? This is from the Docker website:

Compose is a tool for defining and running multi-container Docker applications. 
With Compose, you use a YAML file to configure your application’s services. 
Then, with a single command, you create and start all the services from 
your configuration.
        

We can start building a docker-compose from scratch, but Warchant is already maintaining a up-to-date docker-compose file for SonarQube.


First we need to copy this file to a location, in this example we use the path C:\docker\sonarqube and store the file as docker-compose.yml. This file contains a configuration to setup SonarQube with Postgres as database, it creates a local Docker Network and defines several Docker Volumes to store custom data.

Open PowerShell or command prompt and run the following command execute the docker-compose file:

docker-compose up -d

With this command the docker-compose file is executed. The following actions will be executed:

  • Docker will create a local network called sonarqube_sonarnet.
  • Docker will download both SonarQube and Postgres Docker image.
  • Docker will define Docker volumes to store data.
  • Docker will configure and start the Docker Compose.

When the docker-compose file is executed, Docker Desktop looks like below image.

vm.max_map_count error

Normally this should be all, except that SonarQube needs one more custom configuration.

As you see the SonarQube icon is orange, where we should expect a green image. This is correct when you open the SonarQube tree and click on SonarQube_sonarqube then Logs element is selected. This makes the Linux terminal output visible. In this use case its ended with SonarQube is stopped.

The root-cause of this is the following message max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144].

To solve this issue we need to increase the maximum number of memory map areas a process (vm.max_map_count). Execute the following commands in PowerShell to set the value vm.max_map_count error to 262144. This will set max_map_count value only temporary. Currently there is no way to set the persistent, this is still an open issue.

wsl -d docker-desktop
sysctl -w vm.max_map_count=262144
sysctl vm.max_map_count
        

With above fix we can rerun docker-compose up -d from PowerShell on location C:\docker\sonarqube with should (re)start SonarQube without any issues.

Verify SonarQube is running

At this moment SonarQube is running and we can open it in the browser. Open any browser and navigate to http://localhost:9000/. When the page is loaded you have successfully setup SonarQube with Docker!

The default username is "admin" with the password "admin", in the admin environment you can define projects, configure static code analysis rules and much more!

Links

With this guide of walkthrough of Docker and SonarQube you have setup a fresh installation of SonarQube, ready to use! If you have any questions or feedback, we like to hear it in the comments!

Thank you for reading this article, If you liked the article, help others find this article by sharing it.



Reacties

Populaire posts van deze blog

Android-x86 virtual machine configuration tips

Android and SonarQube with code coverage