Skip to content

Instantly share code, notes, and snippets.

@Oluwatobilobaoke
Last active November 13, 2024 06:35
Show Gist options
  • Save Oluwatobilobaoke/c4ade1c8a14d7146806f12d3107df6e2 to your computer and use it in GitHub Desktop.
Save Oluwatobilobaoke/c4ade1c8a14d7146806f12d3107df6e2 to your computer and use it in GitHub Desktop.
Installing SonarQube on Docker and SonarScanner using Homebrew on macOS

«SonarScanner» - SonarQube Code Analyser

Install the SonarScanner (SonarQube Code Analyser)

brew install sonar-scanner

Add the SonarScanner bin-directory to $PATH

Find the SonarScanner bin-directory (can be taken from the homebrew package installation information)

  • Should be something like: /usr/local/Cellar/sonar-scanner/4.7.0.2747/libexec/bin

Edit the current User's bash source nano ~/.zshrc

Add these line somewhere:

# === SonarQube ===     
# NOTE: sonar-scanner Version in dir-path may change!
export PATH=/usr/local/Cellar/sonar-scanner/4.7.0.2747/libexec/bin:$PATH

Save and close.

Install and run SonarQube on Docker

Installing SonarQube

On Intel based Macs (x86_64)

Install the Docker image from Docker Hub

docker pull sonarqube

Start the x86_64 SonarQube Server

docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube

On ARM based Macs (Apple Silicon)

Install the Docker image from a custom build

  1. docker pull sonarqube

Start the compiled ARM SonarQube Server

docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube

Open SonarQube in the webbrowser

http://localhost:9000

Login with default credentials

  • User: admin
  • Pass: admin …and as instructed, set a new password.

Add a code project to SonarQube

On the SonarQube Dashboard

  1. Click on «Create a project manually»
  2. Fill-in a project name and project key
  • NOTE: you will need this project key later!
  1. Click on «Analyze your project locally»
  2. Fill-in a Token name
  • NOTE: copy the generated Token, you will need this later!
  1. Click on «Other (for JS, TS, Go, Python, PHP, ...)» and select your OS preferences
  2. Follow the on-screen instructions

Add a SonarScanner project configuration-file to the desired code project

  1. Go to the intended project directory
  2. Create a new file named sonar-project.properties
  3. Add and modify the following contents to the sonar-project file:
# must be unique in a given SonarQube instance
sonar.projectKey=my_project_key_as_defined_on_sonarqube_web

# --- optional properties ---
# defaults to project key
#sonar.projectName=My project
# defaults to 'not provided'
#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Defaults to .
#sonar.sources=.
#sonar.sources=src

# path to test source directories (optional)
#sonar.tests=tests

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

Run the SonarScanner Code Analysis for this project

HINT: You can copy this command from the last step in the SonarQube project setup!

sonar-scanner \
  -Dsonar.projectKey=my_project_key_as_defined_on_sonarqube_web \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=my_sonarqube_project_token

…or using a simplified command, which reads other settings from the sonar-project file: sonar-scanner -Dsonar.login=my_sonarqube_project_token

DONE - see the results on the SonarQube web dashboard

http://localhost:9000/dashboard

Export and transfer the SonarQube Docker container

E.g. to be used with same settings on another Mac.

  • Make sure the Docker.app is running and the Desktop was started!

Stop the server (if running)

docker stop sonarqube

Create an image file from the Docker container

docker commit sonarqube mysonarqube

  • NOTE: this can take a moment (or two)…

Save the image file to any user accessible folder in macOS Finder e.g. to the «Downloads»-folder, using:

  • Default Docker file (tar, uncompressed):
    docker save mysonarqube -o ~/Downloads/SonarQube-DockerBackup.tar
  • Gzipped (compressed) Docker file:
    docker save mysonarqube | gzip > ~/Downloads/SonarQube-DockerBackup.tar.gz

Transfer the Docker container file to where you need it…

Import a Docker container file

E.g. on the second Mac.

Start the Docker.app, if not running…

Load the Docker container file

docker load < /path/to/SonarQube-DockerBackup.tar.gz

Loading layer [==================================================>]  337.9MB/337.9MB
Loaded image: mysonarqube:latest
  • NOTE: this can take a moment (or two)…

Run the imported SonarQube Docker image

NOTE: must use the image file reference, instead of sonarqube:latest! docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 mysonarqube:latest

DONE - see the results on the SonarQube web dashboard

http://localhost:9000/dashboard

  • NOTE: may take a moment (or two) until accessible…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment