Here’s a guide on installing SonarQube with Docker. SonarQube is an open-source platform for continuous inspection of code quality, and using Docker simplifies the setup process.

How to Install SonarQube with Docker

Prerequisites

  1. Docker: Make sure Docker is installed on your machine. Download Docker here.
  2. Docker Compose: Required for setting up services like SonarQube and its dependencies together. Install Docker Compose.

Step-by-Step Installation Guide

  1. Set Up a Docker Compose File Create a directory for the SonarQube installation and navigate to it:
   mkdir sonarqube-docker
   cd sonarqube-docker

In this directory, create a docker-compose.yml file to define the services SonarQube requires. Here’s a sample configuration:

services:
  sonarqube:
    image: sonarqube:latest
    container_name: sonarqube
    restart: unless-stopped
    ports:
      - "9100:9000"
    environment:
      - SONAR_JDBC_URL=jdbc:postgresql://db:5432/sonarqube
      - SONAR_JDBC_USERNAME=sonar
      - SONAR_JDBC_PASSWORD=sonar
      - SONAR_WEB_HOST=0.0.0.0
      - SONAR_WEB_PORT=9000
      - SONAR_LOG_LEVEL=INFO
      - SONAR_LOG_MAXFILES=7
      - SONAR_WEB_CONTEXT=/sonar-server # optional (setting proxy path)
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_logs:/opt/sonarqube/logs
      - sonarqube_extensions:/opt/sonarqube/extensions
    depends_on:
      - db

  db:
    image: postgres:14
    container_name: sonarqube_db
    restart: unless-stopped
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
      - POSTGRES_DB=sonarqube
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_logs:
  sonarqube_extensions:
  postgres_data:

In this configuration:

  • sonarqube: This is the SonarQube service. It connects to the PostgreSQL database service and exposes port 9000 for access.
  • db: This is the PostgreSQL database for SonarQube.
  1. Start SonarQube and Database Services Run the following command to start the services:
   docker-compose up -d

This will download the necessary images if they are not already on your system, then start SonarQube and PostgreSQL containers in detached mode.

  1. Access SonarQube After the services start, give SonarQube a few moments to initialize. Then, open your web browser and go to:
   http://localhost:9100
  1. Log In to SonarQube
  • Default username: admin
  • Default password: admin After logging in for the first time, you’ll be prompted to change the default password.

Optional: Configuring Volumes for Data Persistence

To make sure your SonarQube data persists between container restarts, modify your docker-compose.yml file to include volume mounts:

services:
  sonarqube:
    ...
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs

  db:
    ...
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  db_data:

Troubleshooting Tips

  • Database Connection Issues: Ensure that the PostgreSQL container is up and running. The logs can be viewed with docker-compose logs db.
  • Port Conflicts: If port 9000 is already in use, change it in the docker-compose.yml file, e.g., "9001:9000".

Stopping and Restarting

To stop SonarQube and PostgreSQL, run:

docker-compose down

To restart:

docker-compose up -d

With these steps, you’ll have SonarQube running on Docker with a PostgreSQL database, making it easy to analyze and monitor your code quality.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *