Skip to content

Run Sonarqube In Docker

  1. Start a Postgres Docker

    It needs to set the hostname for the Postgres Docker, so that the Sonarqube docker can connect it via hostname.

    Bash
    docker run --name postgres --hostname postgres -e POSTGRES_USER=<pg user> -e POSTGRES_PASSWORD=<pg password> \
      -e PGDATA=/var/lib/postgresql/data/pgdata  -e POSTGRES_HOST_AUTH_METHOD=scram-sha-256  \
      -v "$(pwd)/data":/var/lib/postgresql/data -p 5432:5432 -d --restart always postgres:alpine
    
  1. Start Sonarqube Docker

    1. Create volume for Sonarqube

      Bash
      sudo docker volume create --name sonarqube_data
      sudo docker volume create --name sonarqube_logs
      sudo docker volume create --name sonarqube_extensions
      
    2. Start Sonarqube Docker

      It needs to link the postgres docker, so that sonarqube docker can connect it via hostname.

      Bash
      sudo docker run -d --name sonarqube \
      --link postgres:postgres \
      -p 9000:9000 \
      -e SONAR_JDBC_URL=jdbc:postgresql://postgres/sonarqube?currentSchema=public \
      -e SONAR_JDBC_USERNAME=<pg user> \
      -e SONAR_JDBC_PASSWORD=<pg pass> \
      -v sonarqube_data:/opt/sonarqube/data \
      -v sonarqube_extensions:/opt/sonarqube/extensions \
      -v sonarqube_logs:/opt/sonarqube/logs \
      sonarqube:lts
      

      Now, run docker logs -f sonarqube to check the sonarqube log.

  2. Using Nginx as Proxy

    Nginx Configuration File
    server {
        ...
        location / {
            proxy_pass  http://127.0.0.1:9000;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
            proxy_redirect off;
            proxy_set_header    Host            $host;
            proxy_set_header    X-Real-IP       $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto http;
        }
        ...
    }