Home Setup Sonarqube with Docker (1)
Post
Cancel

Setup Sonarqube with Docker (1)

Setup Sonarqube With Docker

I. Install Docker

II. Install & Run SonarQube container with docker

  • docker pull sonarqube
    • Get the lastest image of sonarqube image
  • docker run -d --name sonarqube -p 9000:9000 sonarqube
    • Run docker sonarqube image with
      • detach mode
      • port 9000
      • name sonarqube
  • docker ps
    • Check if the image is currently running

III. Add maven plugin in your project

  • In your pom.xml
    • So far, 3.6.0.1398 is the newest version for sonar scanner. (2019. 8. 14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<project>
...
	<build>
		<plugins>
            ...
            
			<plugin>
				<groupId>org.sonarsource.scanner.maven</groupId>
				<artifactId>sonar-maven-plugin</artifactId>
				<version>3.6.0.1398</version>
  		</plugin>
  	</plugins>
	</build>
</project>

IV. Run with sonar command

  • Run with maven plugin button on your intellij

  • Or use Command mvn clean install org.sonarsource.scanner.maven:sonar-maven-plugin:${sonarVersion}:sonar
    • You should make sure the sonarqube is running
    • Change the ${sonarVersion} version with your version
    • If you have added sonar plugin in your pom.xml at the step above,
    1
    
    $ mvn clean install sonar
    
  • After you check the build success message checkout the sonarqube server (http://localhost:9000)

V. Connect SonarLint with SonarQube in Intellij

  • Add SonarQube Server Preferences > SonarQube General Settings
    • The admin account user name is admin with password admin
  • Add the server you connected above in SonarQube Project Settings

VI. Setup DB for SonarQube (Postgres)

  • Pull and start postgres

    1
    
    docker run --name postgres -e POSTGRES_PASSWORD=password1234 -d postgres
    
    • I guess POSTGRES_PASSWORD field is not necessary
  • Connect Sonar Container to postgres

    1
    
    docker run -d -p 10000:9000 --name sonarqube --link postgres:sonardb -e sonar.jdbc.username=sonar -e sonar.jdbc.password=sonar -e sonar.jdbc.url=jdbc:postgresql://sonardb/sonar sonarqube
    
    • I am setting the db user name as sonar and password as sonar
    • Linking postgres container as sonardb
    • Using environment variables for sonarqube db setup is not a recommended way according to the sonarqube guide in the docker hub
    • I guess it will be better to change the property file in
    1
    
    /opt/sonarqube/conf/sonar.properties
    
  • enter postgres container

  • 1
    
    docker exec -it postgres /bin/bash
    
  • Enter psql as username = postgres psql --username=postgres

    • Check user roles

      • Type the command \du
      • the user sonar would not exist
    • create user sonar having password sonar

    • 1
      
      CREATE ROLE sonar LOGIN CREATEDB PASSWORD 'sonar';
      
    • create table sonar to save the data from sonarqube. It’s owner user is sonar, which I created just above

      1
      
      CREATE DATABASE sonar OWNER sonar;
      

VII. Resources

This post is licensed under CC BY 4.0 by the author.