How to Install and Set Up SonarQube with Docker

SonarQube is an open-source platform for continuous code quality inspection. It helps developers detect bugs, vulnerabilities, and code smells in various programming languages. In this guide, we will walk through the step-by-step process of installing and running SonarQube using Docker.
Prerequisites
Before proceeding, ensure that you have the following installed on your system:
- Docker (Install from here)
- Docker Compose (Optional, but recommended for easier management)
- At least 2GB of RAM for smooth operation
Method 1: Install SonarQube Using Docker
Step 1: Pull SonarQube Docker Image
The first step is to download the latest SonarQube image from Docker Hub. Run the following command:
docker pull sonarqube
Step 2: Set Up PostgreSQL Database for SonarQube
SonarQube requires a database to store its analysis data. We will use PostgreSQL as our database. Run the following command to start a PostgreSQL container:
docker run -d \
--name sonarqube-db \
-e POSTGRES_USER=sonar \
-e POSTGRES_PASSWORD=sonar \
-e POSTGRES_DB=sonarqube \
postgres:alpine
Step 3: Run SonarQube Container
Once the database container is running, start the SonarQube container and link it to the PostgreSQL database:
docker run -d \
--name sonarqube \
-p 9000:9000 \
--link sonarqube-db:db \
-e SONAR_JDBC_URL=jdbc:postgresql://db:5432/sonarqube \
-e SONAR_JDBC_USERNAME=sonar \
-e SONAR_JDBC_PASSWORD=sonar \
sonarqube
Step 4: Access SonarQube Dashboard
Once SonarQube is running, open your web browser and navigate to:
http://localhost:9000
The default login credentials are:
- Username: admin
- Password: admin
Step 5: Set Up Sonar Scanner
To analyze your projects, you need to install Sonar Scanner and configure it in your system.
Configure Sonar Scanner Environment Variables
- Open the
~/.bashrc
file in a text editor:
nano ~/.bashrc
2. Add the following lines at the end of the file:
export SONAR_SCANNER_HOME=/path/to/sonar-scanner
export PATH=$SONAR_SCANNER_HOME/bin:$PATH
3. Save the file and apply the changes:
source ~/.bashrc
Method 2: Alternative Installation Steps
Step 1: Pull SonarQube Docker Image
docker pull sonarqube
Step 2: Set Up PostgreSQL Database
docker run -d --name sonarqube-db -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar -e POSTGRES_DB=sonarqube postgres:alpine
Step 3: Run SonarQube Container
docker run -d --name sonarqube -p 9000:9000 --link sonarqube-db:db -e SONAR_JDBC_URL=jdbc:postgresql://db:5432/sonarqube -e SONAR_JDBC_USERNAME=sonar -e SONAR_JDBC_PASSWORD=sonar sonarqube
This command sets up the SonarQube container with a link to the PostgreSQL database.
Step 4: Set Up Sonar Scanner
nano ~/.bashrc
export SONAR_SCANNER_HOME=/path/to/sonar-scanner# Add Sonar Scanner to PATH
export PATH=$SONAR_SCANNER_HOME/bin:$PATH
Conclusion
You have successfully installed and configured SonarQube using Docker and PostgreSQL. Now, you can start analyzing your code quality and improve your projects. Happy coding!
Need More Help?
Check out the official SonarQube documentation for advanced configurations and best practices.