Lang
Blog

Mastering Sonar Scan Through Docker

BySiddharth Singh
December 15th . 3 min read
Mastering Sonar Scan Through Docker

SonarQube stands tall as an indispensable tool for ensuring code quality in software development. Leveraging its capabilities through Docker simplifies the setup process, enabling a seamless integration into your project workflow. By following a few straightforward steps, you can effortlessly deploy and harness the power of SonarQube within a Docker container.

Let's dive into the process together!

Step 1:

Setup SonarQube Docker

  • Pull SonarQube Docker Image if not present
$ sudo docker pull sonarqube
$ sudo docker pull sonarsource/sonar-scanner-cli:latest

$ sudo docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube

  • Note: To check SonarQube Docker Container logs, you can run below command.

$ sudo docker logs -f sonarqube

Step 2:

Access SonarQube Web Interface Open your web browser and navigate to http://localhost:9000. The default credentials (username / password) are: admin / admin

1.jpg

Step 3:

Create Local Project in SonarQube

  • Choose a project name and project key to identify your project.

2.jpg

  • Use global settings as baseline for new code for this project.

3.jpg

  • Create project and select option Locally to analyse your project.

4.jpg

Step 4:

Generate Tokens for Local Project After setting up the project, you'll be prompted to generate a token. Enter a name for the token and click on the "Generate" button. Note down the generated token. This token is needed for authentication during the analysis.

5.jpg

6.jpg

Step 5:

Set Up Your React-Node Project Navigate to your project directory and create a sonar-project.properties.

Note: Project key and sonar token are necessary. See below example for file contents:

7.jpg

Step 6:

Run Sonar Scan Through Docker Image

  • Get Your IP Address
$ hostname -I | awk '{print $1}'
  • Run Sonar Scan
$ sudo docker run --rm --network=host -e SONAR_HOST_URL=
"http://<ip- address>:9000/" -e SONAR_TOKEN="<your-sonar-token>" 
-v $(pwd):/usr/src/ sonarsource/sonar-scanner-cli
  • Note: Replace < > with actual value.

Step 7:

View Results in SonarQube Web Interface

8.jpg

Conclusion:

By following these steps and harnessing the combined power of SonarQube and Docker, you've established a robust foundation for maintaining high code quality standards. This foundation paves the way for smoother development cycles, fewer bugs, and ultimately, more reliable software that stands the test of time.

Continue this journey of code excellence by integrating regular Sonar scans into your development pipeline. Your commitment to quality ensures that your software not only meets but exceeds expectations, setting you on a path towards unparalleled success.

Share:
0
+0