Root-Server Tutorial

ROOT-SERVER TUTORIAL

How to Install Dockerized SonarQube with a Postgres Database

This tutorial explains how to install docker and SonarQube with a database in just a few minutes.

Author:

Peter Mesiha

Last updated:

19/04/2023

Introduction

In this "how to" tutorial you are going to set up your SonarQube instance with db.

SonarQube is the leading tool for continuously inspecting the code quality and security of your codebases, and guiding development teams during code reviews. Covering 27 programming languages, while pairing-up with your existing software pipeline, SonarQube provides clear remediation guidance for developers to understand and fix issues, and for teams overall to deliver better and safer software. With over 225,000 deployments helping small development teams as well as global organizations, SonarQube provides the means for all teams and companies around the world to own and impact their code quality and security.

First we are going to uninstall any old docker engine and install the latest (can be skipped), then we'll create/edit the docker-compose file, and check all the containers and the setup.

Requirements

  • Docker
  • Docker Compose or docker-compose
  • Linux server

Step 1 - Uninstall old Docker Engine, CLI, containerd, and Docker Compose packages

sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

Step 2 - Curl the script for installation from docker

curl -fsSL https://get.docker.com -o get-docker.sh

Step 3 - Dry run the script to check for complications

sudo sh ./get-docker.sh --dry-run

Step 4 - Run the script

sudo sh ./get-docker.sh

Step 5 - Verify the installation

docker --version

Step 6 - Create a folder to save the docker-compose file and cd into that folder

mkdir sonarqube-with-postgres
cd sonarqube-with-postgres

Step 7 - Copy the docker-compose file to your system

vim docker-compose.yml

Paste the content inside this file.

---
version: '3.8'
    
services:
   app:
      hostname: 'sonarqube'
      image: 'sonarqube:lts-community'
      container_name: 'sonar-app'
      restart: 'unless-stopped'
      networks:
         - 'sonar-network'
      environment:
         - SONAR_JDBC_URL=jdbc:postgresql://sonar-db:5432/sonarqube
         - SONAR_JDBC_USERNAME=sonar-user
         - SONAR_JDBC_PASSWORD=changeMe.aHighlyComplexPasswordToKeepYouSecure
      volumes:
         - 'sonar-app-conf-vol:/opt/sonarqube/conf'
         - 'sonar-app-data-vol:/opt/sonarqube/data'
         - 'sonar-app-extension-vol:/opt/sonarqube/extensions'
         - 'sonar-app-bundled-plugin-vol:/opt/sonarqube/lib/bundled-plugins'
      ports:
         ## left side is host side - right side is container side and should not be changed
         - '9000:9000'
      depends_on:
         db:
            condition: service_healthy
   db:
      hostname: 'postgresql'
      image: 'postgres:latest'
      container_name: 'sonar-db'
      restart: 'unless-stopped'
      networks:
         - 'sonar-network'
      environment:
         - PGUSER=sonar-user
         - PGPASSWORD=changeMe.aHighlyComplexPasswordToKeepYouSecure
         - PGDATABASE=sonarqube
      volumes:
         - 'sonar-db-postgresql-vol:/var/lib/postgresql'
         - 'sonar-db-postgresql-data-vol:/var/lib/postgresql/data'
      healthcheck:
         test: ["CMD-SHELL", "pg_isready"]
         interval: 10s
         timeout: 5s
         retries: 5

volumes:
   sonar-app-conf-vol:
      name: 'sonar-app-conf-vol'
   sonar-app-data-vol:
      name: 'sonar-app-data-vol'
   sonar-app-extension-vol:
      name: 'sonar-app-extension-vol'
   sonar-app-bundled-plugin-vol:
      name: 'sonar-app-bundled-plugin-vol'
   sonar-db-postgresql-vol:
      name: 'sonar-db-postgresql-vol'
   sonar-db-postgresql-data-vol:
      name: 'sonar-db-postgresql-data-vol'

networks:
   sonar-network:
      name: 'sonar-network'

Step 8 - Edit the docker-compose file

Edit the parts with 'changeMe.' and of course any other parts you want to modify, such as the ports exposed on the host machine.

Step 9 - Check the configuration

Make sure you have changed all the 'changeMe.' parts. Now you are good to go.

Step 10 - Create the containers

With docker-compose, it's fairly easy to set everything up.

Type:

docker compose up -d

Step 11 - Check the containers

Deployment should take a maximum of 2 minutes to be up and running as the app waits for the db and a successful health check.

Type:

docker ps -a

You should wait till the sonarqube-db container is up and healthy and the sonarqube-app is running.

Step 12 - Check the URL

Now you can check your port 9000 (or another port if you have changed the left side) with your browser at localhost:9000, or serverIP:9000 if you run it on a server in a different network or your provided URL inside the docker-compose file.

If you cannot reach the URL after a few minutes (3 to 4), you should check your ufw/ufw-docker to expose the ports through the firewall or check your proxy manager, if you use one.

Congratulations, you have created your own SonarQube server with its own db in a few minutes.

Conclusion

As you have seen, setting up SonarQube with its own db instance is quite simple and straightforward.

License

MIT

Copyright (c) 2021 netcup

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributor's Certificate of Origin

By making a contribution to this project, I certify that:

  1. The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or

  2. The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or

  3. The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

  4. I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.

Published 19/04/2023 by Peter Mesiha

Submit your tutorial

Get 60€ netcup vouchers for every published tutorial.