Here’s a concise step-by-step guide to install SonarQube using Docker on Linux, mounting important files in /media/, and allowing remote access for both SonarQube and the database.

Prerequisites

  1. Ensure Docker and Docker Compose are installed.
  2. Create directories for SonarQube and the database in /media/.
# Example directory structure
mkdir -p /media/sonarqube/conf 
mkdir -p /media/sonarqube/data 
mkdir -p /media/sonarqube/logs 
mkdir -p /media/sonarqube/extensions
mkdir -p /media/postgres
sudo chmod -R 777 /media/
sudo chown -R 1000:1000 /media/sonarqube/logs

Step 1: Set Up Docker Compose File

Create a docker-compose.yml file in your working directory:

nano 7.2                                                                       docker-compose.yml
services:
  db:
    image: postgres:13
    container_name: sonarqube_db
    environment:
      POSTGRES_USER: sonarqube
      POSTGRES_PASSWORD: sonarqube
      POSTGRES_DB: sonarqube
    volumes:
      - /media/postgres:/var/lib/postgresql/data
    networks:
      - sonarqube_network
    ports:
      - "5432:5432"  # Allows remote access
    restart: always  # Restarts the container automatically

  sonarqube:
    image: sonarqube:10.7-community
    container_name: sonarqube
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonarqube
      SONAR_JDBC_USERNAME: sonarqube
      SONAR_JDBC_PASSWORD: sonarqube
    volumes:
      - /media/sonarqube/conf:/opt/sonarqube/conf
      - /media/sonarqube/data:/opt/sonarqube/data
      - /media/sonarqube/logs:/opt/sonarqube/logs
      - /media/sonarqube/extensions:/opt/sonarqube/extensions
    ports:
      - "9000:9000"  # Allows remote access
    restart: always  # Restarts the container automatically
    networks:
      - sonarqube_network
    depends_on:
      - db

networks:
  sonarqube_network:
    driver: bridge

Step 2: Start SonarQube and Database

Run Docker Compose to start SonarQube and PostgreSQL:

docker-compose up -d

In case there is a mistake in deployment just follow

docker-compose down
docker-compose up -d

Step 3: Access SonarQube

  1. Access SonarQube byhttp://<your-server-ip>:9000 in a web browser.
  2. Default account: admin / admin.

Now visit Website generate everything and Start the analyser.

if you run this in linux or mac register the sonar token

export SONAR_TOKEN=YOUR_SONAR_TOKEN

Pull Docker image

docker pull  sonarsource/sonar-scanner-cli

If you are working on an offline instance like a very secure server environment. you need to pull the image on your machine, then save them into a docker image zip files. Transport them and import them. following two command will help you.

docker save -o sonar-scanner-cli.tar.gz sonarsource/sonar-scanner-cli

Then transport the files from current location to the destination server. after that run the following command to import the docker image.

docker load -i sonar-scanner-cli.tar.gz

Now prepare the running files, In this case we are running them against dot net code.

nano sonar-project.properties
nar.projectKey= project_key 
sonar.projectName= name
sonar.projectVersion=1.0
sonar.sources=.
sonar.exclusions=**/*.java 

Ensure that NO COMMENT is in files (sonar-project.properties) sonar is not like having comment here.

Now run this to scan your code and submit report to sonarqube

docker run --rm 
-e SONAR_HOST_URL="http://10.0.0.165:9000" 
-e SONAR_TOKEN="sqp_xxxxxxxxxxx" 
-v "/home/azureuser/DotNetCode:/usr/src" 
sonarsource/sonar-scanner-cli

BIG NOTE : you are running this in a docker. The IP can’t be localhost. You cannot use 127.0.0.1 here. This will refer them inside the docker which is different from the docker you run sonarQube.

Then for my project we also need to scan java code. Sonar cannot seem to scan .java files. they only able to scan the class files which you can just compile it.

For me it’s Maven build.

docker run --rm --platform=linux/arm64 -v "$PWD:/usr/src/app" -w /usr/src/app maven:3.8.1-openjdk-11 bash -c "
    mvn -X -DskipTests=true -f ./JavaCode/pom.xml clean compile
"

Then once build just prep the sonar-project.properties file.

sonar.projectKey=your_project_key
sonar.projectName=Your Project Name
sonar.projectVersion=1.0
sonar.sources=.
sonar.java.binaries=**/target/classes  # Adjust based on your setup

Remember what I said previously Sonar DON’T LIKE COMMENT HERE. The actually look for directory with name # Adjust based on your setup. So remove them before you running.

The ** is just a recursive search for class files in multiple directory. you likely will need that.

Now Submit the scan again on javacode

docker run --rm 
-e SONAR_HOST_URL="http://10.0.0.165:9000" 
-e SONAR_TOKEN="sqp_xxxxxxxxxxx" 
-v "/home/azureuser/JavaCODE:/usr/src" 
sonarsource/sonar-scanner-cli

Leave a Reply

Your email address will not be published. Required fields are marked *