SonarQube is an open-source platform for continuous inspection of code quality, supporting multiple languages including Java, C#, C/C++, and JavaScript through its plugin ecosystem. This guide details the deployement of a SonarQube server using Docker Compose and its integration with Maven for analyzing Java projects.
Prerequisites A CentOS 7 server with internet connectivity and JDK 11 installed is required. SonarQube version 8.2 and latter mandates JDK 11.
Installing Docker Engine Execute the following commands to install Docker:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
Installing Docker Compose Download and set up Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Deploying SonarQube with Docker Compose Start the Docker service:
sudo systemctl daemon-reload
sudo systemctl start docker
Create a docker-compose.yaml file with the following configuration:
version: "3.8"
services:
postgres_db:
image: postgres:13
networks:
- sq_network
environment:
POSTGRES_USER: sonar_user
POSTGRES_PASSWORD: sonar_pass
POSTGRES_DB: sonarqube_db
volumes:
- postgres_data:/var/lib/postgresql/data
sonarqube_server:
image: sonarqube:8.2-community
depends_on:
- postgres_db
networks:
- sq_network
ports:
- "9000:9000"
environment:
SONAR_JDBC_URL: jdbc:postgresql://postgres_db:5432/sonarqube_db
SONAR_JDBC_USERNAME: sonar_user
SONAR_JDBC_PASSWORD: sonar_pass
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
networks:
sq_network:
driver: bridge
volumes:
postgres_data:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
Launch the services in detached mode:
docker-compose up -d
Monitor the containre logs:
docker-compose logs -f sonarqube_server
Accessing SonarQube and Configuring Maven
After deployment, access the SonarQube web interface at http://<server_ip>:9000. The default credentials are admin/admin.
To configure Maven for analysis, add the following profile to your settings.xml file:
<settings>
<profiles>
<profile>
<id>sonar_scan</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://<server_ip>:9000</sonar.host.url>
<sonar.login>admin</sonar.login>
<sonar.password>admin</sonar.password>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sonar_scan</activeProfile>
</activeProfiles>
</settings>
Navigate to your Java project's root directory and execute:
mvn clean verify
mvn sonar:sonar
The analysis results will appear in the SonarQube dashboard after processing.
Troubleshooting Common Errors
If you encounter the error max virtual memory areas vm.max_map_count [65530] is too low, apply this system parameter change:
# Temporary fix
sudo sysctl -w vm.max_map_count=262144
# Permanent fix: Add 'vm.max_map_count=262144' to /etc/sysctl.conf, then run:
sudo sysctl -p
Removing Docker and Its Images To uninstall Docker and clean up associated data:
- List installed Docker packages:
yum list installed | grep docker - Remove the packages:
sudo yum remove docker-ce docker-ce-cli containerd.io -y - Delete all Docker data:
sudo rm -rf /var/lib/docker