# SonarQube + PostgreSQL + SonarScanner

> Code quality and security analysis platform.

- Category: DevOps & CI/CD
- Difficulty: intermediate
- Components: sonarqube, postgresql, sonarscanner
- Tags: sonarqube, code-quality, security, static-analysis, devsecops
- Source: https://docker.recipes/devops/sonarqube-complete

## Overview

SonarQube is a comprehensive code quality and security analysis platform that has become the industry standard for continuous inspection of code quality. Originally developed by SonarSource in 2008, SonarQube performs automated static analysis to detect bugs, vulnerabilities, security hotspots, and code smells across 30+ programming languages. It provides quality gates that can block deployments when code doesn't meet defined standards, making it essential for enterprise development teams focused on maintaining high code quality and security standards.

This stack combines SonarQube with PostgreSQL as the backend database and includes SonarScanner for automated code analysis. PostgreSQL serves as SonarQube's primary data store, handling analysis results, project configurations, user management, and historical quality metrics. The SonarScanner component enables automated code scanning that can be triggered on-demand or integrated into CI/CD pipelines. Together, these components create a complete code quality platform that can analyze codebases, store results persistently, and provide detailed reporting on code health over time.

This configuration is ideal for development teams implementing DevSecOps practices, organizations requiring code quality governance, and enterprises needing centralized code analysis across multiple projects. The combination provides both immediate feedback on code quality issues and long-term tracking of technical debt, making it valuable for teams ranging from small startups establishing coding standards to large enterprises managing hundreds of applications with strict security and quality requirements.

## Features

- Static code analysis across 30+ programming languages including Java, C#, JavaScript, Python, PHP, and Go
- Security vulnerability detection with OWASP Top 10 coverage and CWE classification
- Quality gates with customizable thresholds for bugs, vulnerabilities, code smells, and technical debt
- Technical debt quantification with time-to-fix estimates for identified issues
- Branch analysis and pull request decoration for code review integration
- Historical trend analysis showing code quality evolution over time
- PostgreSQL backend providing ACID compliance and advanced querying for complex quality metrics
- On-demand code scanning with SonarScanner supporting multiple build tools and CI systems

## Use cases

- Enterprise code quality governance with centralized analysis across multiple development teams
- DevSecOps pipeline integration for automated security vulnerability scanning before deployment
- Technical debt management and tracking for legacy system modernization projects
- Code review enhancement with automated quality checks and pull request decoration
- Compliance reporting for industries requiring code quality documentation and audit trails
- Open source project quality assurance with community edition features
- Multi-language development environment requiring consistent quality standards across different technologies

## Prerequisites

- Minimum 4GB RAM available (2GB for SonarQube + 1GB for PostgreSQL + system overhead)
- Port 9000 available for SonarQube web interface access
- Docker Compose v2.0+ with profiles support for SonarScanner execution
- Understanding of static code analysis concepts and quality gate configuration
- Source code projects with build tools supported by SonarScanner (Maven, Gradle, npm, etc.)
- Basic knowledge of PostgreSQL administration for database maintenance and backup procedures

## docker-compose.yml

```yaml
services:
  sonarqube:
    image: sonarqube:lts-community
    environment:
      - SONAR_JDBC_URL=jdbc:postgresql://postgres:5432/sonarqube
      - SONAR_JDBC_USERNAME=${POSTGRES_USER}
      - SONAR_JDBC_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - sonarqube-data:/opt/sonarqube/data
      - sonarqube-extensions:/opt/sonarqube/extensions
      - sonarqube-logs:/opt/sonarqube/logs
    ports:
      - "9000:9000"
    depends_on:
      - postgres
    networks:
      - sonarqube-network
    restart: unless-stopped

  postgres:
    image: postgres:15
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=sonarqube
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - sonarqube-network
    restart: unless-stopped

  scanner:
    image: sonarsource/sonar-scanner-cli:latest
    environment:
      - SONAR_HOST_URL=http://sonarqube:9000
      - SONAR_LOGIN=${SONAR_TOKEN}
    volumes:
      - ./:/usr/src
    working_dir: /usr/src
    depends_on:
      - sonarqube
    networks:
      - sonarqube-network
    profiles:
      - tools

volumes:
  sonarqube-data:
  sonarqube-extensions:
  sonarqube-logs:
  postgres-data:

networks:
  sonarqube-network:
    driver: bridge
```

## .env

```bash
# SonarQube
POSTGRES_USER=sonarqube
POSTGRES_PASSWORD=secure_postgres_password

# Generate token in SonarQube UI
SONAR_TOKEN=your_sonar_token

# Linux: sysctl -w vm.max_map_count=524288
```

## Usage notes

- SonarQube at http://localhost:9000
- Default login: admin / admin
- Run scanner: docker compose --profile tools run scanner
- Configure project token for CI
- Install language plugins

## Troubleshooting

- SonarQube fails to start with 'Elasticsearch bootstrap checks failed': Increase Docker Desktop memory allocation to at least 4GB and set vm.max_map_count=262144 on Linux hosts
- Database connection errors 'FATAL: database sonarqube does not exist': Ensure PostgreSQL container starts completely before SonarQube by adding healthcheck or increasing startup delay
- SonarScanner fails with 'java.net.ConnectException Connection refused': Wait for SonarQube to fully initialize (check logs for 'SonarQube is up' message) before running scanner
- Analysis fails with 'Insufficient token permissions': Generate a project analysis token in SonarQube admin panel and set it as SONAR_TOKEN environment variable
- Out of memory errors during large project analysis: Increase SONAR_SCANNER_OPTS with -Xmx2g or higher in scanner environment variables
- Quality gate status not updating in CI: Ensure sonar-scanner includes -Dsonar.qualitygate.wait=true parameter for synchronous quality gate evaluation

