Skip to content

Docker With SonarQube#

What Is The SonarQube?#

  • SonarQube, is a self-managed, automatic code review tool that systematically helps you deliver Clean Code. As a core element of our Sonar solution, SonarQube integrates into your existing workflow and detects issues in your code to help you perform continuous code inspections of your projects. The tool analyses 30+ different programming languages and integrates into your CI pipeline and DevOps platform to ensure that your code meets high-quality standards.
  • More information

Create SonarQube Server With Docker#

  • Let's create a file name docker-compose.yml, then add scripts as below:
docker-compose.yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
version: '3.7'

services:
  #Create service sonarqube
  sonarqube:
    #Get lastest image from DockerHub
    image: sonarqube:8-community
    #set container name
    container_name: sonarqube
    ports:
    - 9000:9000
    #this service is assigned to network sonarnet  
    networks:
    - sonarnet
    #Set username, password and url to connect postgresql database
    environment:
      SONARQUBE_JDBC_USERNAME: sonar
      SONARQUBE_JDBC_PASSWORD: sonar
      SONARQUBE_JDBC_URL: jdbc:postgresql://db/sonar
    #Set volumes which will sync data to real machine  
    volumes:
    - sonarqube_conf:/opt/sonarqube/conf
    - sonarqube_data:/opt/sonarqube/data
    - sonarqube_extensions:/opt/sonarqube/extensions
    - sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins

  #Create service postgres database  
  db:
    #Get lastest image from DockerHub
    image: postgres
    #this service is assigned to network sonarnet  
    networks:
    - sonarnet
    #Set username and password for postgres database  
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
    #Set volumes which will sync data to real machine  
    volumes:
    - postgresql:/var/lib/postgresql
    - postgresql_data:/var/lib/postgresql/data

#Create network sonarnet
networks:
  sonarnet:

#Create volumes on real machine that data of container can sync
volumes:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_bundled-plugins:
  postgresql:
  postgresql_data:
  • Then let's create some folders which is located next to the docker-compose.yml file as image below, then the data of container will be sync into these folders.

 #zoom

Testing#

  • Now you can use command docker-compose up to start your SonarQube server. If there are errors with messages as below:
1
window max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
  • So you should open Terminal and run the command below to set vm.max_map_count=262144 on Linux machine.
1
sudo sysctl -w vm.max_map_count=262144
  • If you are using Window. You should open PowerShell and put these commands
1
2
wsl -d docker-desktop
sysctl -w vm.max_map_count=262144
  • Then restart your docker-compose file and go to http://localhost:9000 and use default username and password of SonarQube to login.
  • username: admin
  • password: admin

 #zoom

References#