# SonarQube Enterprise Stack

> Code quality and security analysis platform with PostgreSQL and Elasticsearch.

- Category: DevOps & CI/CD
- Difficulty: intermediate
- Components: sonarqube, postgresql, elasticsearch, nginx
- Tags: sonarqube, code-quality, security, static-analysis, cicd
- Source: https://docker.recipes/devops/sonarqube-enterprise

## Overview

SonarQube is a leading platform for continuous code quality inspection and security vulnerability detection, serving over 5 million developers worldwide since its launch in 2008. This enterprise-grade static analysis tool automatically reviews code for bugs, security vulnerabilities, code smells, and technical debt across 30+ programming languages including Java, C#, Python, JavaScript, and TypeScript. By integrating directly into CI/CD pipelines, SonarQube enforces quality gates that prevent problematic code from reaching production while providing detailed remediation guidance.

This comprehensive stack combines SonarQube with PostgreSQL for robust data persistence, Elasticsearch for advanced search and analytics capabilities, and NGINX for production-grade reverse proxy functionality. PostgreSQL handles SonarQube's relational data including project configurations, user management, and analysis results with full ACID compliance, while Elasticsearch powers the platform's real-time search features and complex code analytics. NGINX provides SSL termination, load balancing, and secure external access with enterprise-ready performance characteristics.

Development teams at medium to large organizations will benefit most from this configuration, particularly those implementing DevSecOps practices or managing multiple codebases with strict quality requirements. The stack is ideal for organizations transitioning from cloud-based code analysis tools to self-hosted solutions, teams requiring air-gapped deployments, or enterprises needing detailed compliance reporting and audit trails. With proper configuration, this setup can analyze millions of lines of code while maintaining sub-second search response times across historical analysis data.

## Features

- Static code analysis across 30+ programming languages with customizable quality profiles
- Security vulnerability detection including OWASP Top 10 and CWE standards compliance
- Technical debt quantification with time-to-fix estimates and trend analysis
- Quality gates with branch analysis and pull request decoration
- Advanced search capabilities powered by Elasticsearch for code patterns and historical analysis
- PostgreSQL-backed persistence ensuring ACID compliance for critical analysis data
- Multi-project portfolio management with role-based access control
- Real-time code coverage tracking and test result integration

## Use cases

- Enterprise CI/CD pipeline integration for automated code quality enforcement
- Financial services organizations requiring SOX compliance and audit trail documentation
- Open source project maintainers managing community contributions and code standards
- Development teams transitioning from SonarCloud to self-hosted infrastructure
- Government agencies or defense contractors requiring air-gapped code analysis
- Multi-team organizations needing centralized code quality metrics and reporting
- Companies implementing DevSecOps practices with security-first development workflows

## Prerequisites

- Minimum 8GB RAM available (4GB for SonarQube, 2GB for Elasticsearch, 1GB for PostgreSQL)
- Docker host with vm.max_map_count=262144 configured for Elasticsearch memory mapping
- Port availability: 9000 (SonarQube), 80/443 (NGINX), with 5432 and 9200 internal
- SSL certificates placed in ./certs directory for HTTPS-enabled NGINX configuration
- Basic understanding of static code analysis concepts and CI/CD integration patterns
- Administrative access to configure SonarQube projects and quality profiles post-deployment

## docker-compose.yml

```yaml
services:
  sonarqube:
    image: sonarqube:lts-community
    ports:
      - "9000:9000"
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://postgres:5432/${POSTGRES_DB}
      SONAR_JDBC_USERNAME: ${POSTGRES_USER}
      SONAR_JDBC_PASSWORD: ${POSTGRES_PASSWORD}
      SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: "true"
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    depends_on:
      postgres:
        condition: service_healthy
      elasticsearch:
        condition: service_healthy
    networks:
      - sonar-net
    restart: unless-stopped
    ulimits:
      nofile:
        soft: 65536
        hard: 65536

  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - sonar-net
    restart: unless-stopped

  elasticsearch:
    image: elasticsearch:8.11.0
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - ES_JAVA_OPTS=-Xms512m -Xmx512m
      - bootstrap.memory_lock=true
    volumes:
      - es_data:/usr/share/elasticsearch/data
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:9200/_cluster/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
    networks:
      - sonar-net
    restart: unless-stopped
    ulimits:
      memlock:
        soft: -1
        hard: -1

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./certs:/etc/nginx/certs:ro
    depends_on:
      - sonarqube
    networks:
      - sonar-net
    restart: unless-stopped

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  postgres_data:
  es_data:

networks:
  sonar-net:
    driver: bridge
```

## .env

```bash
# PostgreSQL Configuration
POSTGRES_USER=sonar
POSTGRES_PASSWORD=secure_postgres_password
POSTGRES_DB=sonarqube

# SonarQube settings
SONAR_WEB_CONTEXT=/
```

## Usage notes

- SonarQube at http://localhost:9000
- Default login: admin/admin (change on first login)
- Configure vm.max_map_count=262144 on host
- Install language plugins from marketplace

## Troubleshooting

- SonarQube fails to start with 'max virtual memory areas vm.max_map_count too low': Run 'sudo sysctl -w vm.max_map_count=262144' on Docker host and add to /etc/sysctl.conf for persistence
- Elasticsearch container exits with OutOfMemoryError: Increase ES_JAVA_OPTS heap size or add more system RAM, minimum 2GB required for stable operation
- SonarQube shows 'Database connection failure' on startup: Verify PostgreSQL health check passes and POSTGRES_* environment variables match between services
- NGINX returns 502 Bad Gateway when accessing SonarQube: Check that SonarQube container is fully started (can take 60-90 seconds) and nginx.conf proxy_pass configuration is correct
- SonarQube analysis fails with 'No space left on device': Elasticsearch requires significant disk space for indexing, ensure at least 10GB free space for production usage
- Plugin installation fails in SonarQube marketplace: Restart SonarQube container after plugin installation and verify sonarqube_extensions volume has proper write permissions

