Step-by-Step Guide to Installing SonarQube on Ubuntu with Docker Compose

Step-by-Step Guide to Installing SonarQube on Ubuntu with Docker Compose

Comprehensive Step-by-Step Guide to Installing SonarQube on Ubuntu Using Docker Compose

In this detailed guide, we will walk you through the entire process of installing SonarQube on an Ubuntu system using Docker Compose. SonarQube is a powerful tool for continuous inspection of code quality, and using Docker Compose simplifies the setup by managing dependencies and configurations. Follow these steps to ensure a smooth installation:

Prerequisites

Before starting, ensure that your system meets the following requirements:

  1. Ubuntu Operating System: Ensure you have a recent version of Ubuntu installed.

  2. Docker: Docker should be installed and running on your system. If not, you can install it by following the official Docker installation guide for Ubuntu. Follow this article on how to install Docker on Ubuntu: https://medium.com/@prasad.reddy0708/install-docker-in-linux-cfc1b537ee30

  3. Docker Compose: Make sure Docker Compose is installed. You can install it using the command:

     sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
     chmod +x /usr/local/bin/docker-compose
    

    Check Docker Compose version after installation.

     docker-compose --version
    
  4. Sufficient Resources: SonarQube requires at least 2GB of RAM to run smoothly. Ensure your system has adequate resources.

https://gist.github.com/vprasadreddy/00025d432acc1a6f636555502ccf0a26

sonar-docker-compose.yml

version: "3"

services:
  sonarqube:
    image: sonarqube:community
    restart: unless-stopped
    container_name: sonarqube
    hostname: sonarqube
    read_only: true
    depends_on:
      - postgres_db
    ports:
      - 9000:9000
    environment:
      - sonar.jdbc.username=sonar
      - sonar.jdbc.password=sonarpass
      - sonar.jdbc.url=jdbc:postgresql://postgres_db:5432/sonar
#      - sonar.search.javaAdditionalOpts=-Dbootstrap.system_call_filter=false
    volumes:
      - sonarqube_logs:/opt/sonarqube/logs
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_temp:/opt/sonarqube/temp

  postgres_db:
    image: postgres:13.1
    container_name: postgresql
    hostname: postgresql
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=postgrespass
      - POSTGRES_DB=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data
    ulimits:
      nofile:
        soft: 65536
        hard: 65536

volumes:
  sonarqube_data:
  sonarqube_temp:
  sonarqube_extensions:
  sonarqube_logs:
  postgresql:
  postgresql_data:

Create Docker Compose File

  1. Create a file with name sonar-docker-compose.yml

  2. Add the code from the above GitHub Gist

  3. Save it locally.

    Copy Docker Compose File to Ubuntu machine using scp command

  1. Using scp command copy the Docker Compose file from local machine to Ubuntu machine

    scp -i <path_to_ubuntu_vm_pem_key> <source_path_of_docker_compose_file_to_copy_from_local_machine> <ubuntu_vm_username>@<ubuntu_vm_ip_address>:<destination_path_in_ubuntu_vm_to_place_the_file>

    Example: scp -i /Users/varaprasad/Downloads/demo-linux-vm.pem /Users/varaprasad/sonar-docker-compose.yml azureuser@172.111.05.430:/home/azureuser

    Alternative method to get Docker Compose File using wget command

  1. Login to Ubuntu machine using ssh command

    ssh -i <path_to_ubuntu_vm_pem_key> <ubuntu_vm_username>@<ubuntu_vm_ip_address>

    Example: ssh -i /Users/varaprasad/Downloads/demo-linux-vm.pem azureuser@172.111.05.430

  2. Run the wget command to download the file from GitHub Gist.

     wget https://gist.githubusercontent.com/vprasadreddy/00025d432acc1a6f636555502ccf0a26/raw/c6655b0f37c33ddb9e530237db946e3f359d06a6/sonarqube-with-postgresql-docker-compose.yml -O sonar-docker-compose.yml
    
  3. Verify if the file is copied to destination folder using ls command.

Run the Docker Compose file

  1. Run the below docker compose up command to start the docker container for SonarQube.

     docker-compose -f sonar-docker-compose.yml up -d
    

  2. Check the status of the containers using docker ps command

     docker ps
    
  3. Wait for few seconds to spin up the SonarQube container and browse to SonarQube URL by appending port 9000 to Ubuntu Server Public IP. By default SonarQube runs on port 9000.

    http://ubuntu_vm_ip_address:9000

    Example: http://172.111.05.430:9000

  4. You should see the SonarQube login page. The default credentials for SonarQube are:

    username: admin

    password: admin

Configure SonarQube

  1. After logging in, you may want to configure SonarQube according to your project requirements. This includes setting up quality profiles, defining rules, and integrating with your CI/CD pipeline.

Stop the SonarQube container

  1. Run the below docker compose down command to stop the SonarQube docker container.

     docker-compose -f sonar-docker-compose.yml down
    

Conclusion

By following this comprehensive guide, you have successfully installed SonarQube on your Ubuntu system using Docker Compose. This setup allows you to leverage SonarQube's powerful code quality inspection capabilities with ease. Remember to regularly update your Docker images and SonarQube configurations to keep your system secure and efficient.