How to deploy SonarQube with Docker

Jack Wallen shows you how to implement the continuous code inspection tool, SonarQube, using Docker on Ubuntu Server 22.04.

Team of programmers working to find solutions to problems and coding technologies in an office of a software development company.
Image: snowing12/Adobe Stock

SonarQube is a continuous code inspection tool used for automatic code quality inspection. The tool runs these automated checks to detect bugs and code odors (deeper problems) in 29 different programming languages. SonarQube comes with an easy to use web based GUI and can be deployed as a docker container.

SEE: Hiring Kit: Back-End Developer (TechRepublic Premium)

And that, my friends, is exactly what I am going to do. With the help of Ubuntu Server 22.04 and Docker Compose, we will implement SonarQube.

One thing to note is that this implementation uses an embedded database, which does not scale. If you know you’ll need to use SonarCube for larger projects, you’ll want to follow the steps for the full installation, which I covered in “How to install SonarQube Code Quality Analyzer on Ubuntu Server 20.04”. For smaller projects, deploying with Docker is a great option to get SonarCube up and running quickly.

What you will need to implement SonarQube

All you will need for this is a running instance of Ubuntu Server 22.04 (this can also be done on any Ubuntu based distribution) and a user with sudo privileges.

How to install Docker Compose

One of the reasons I prefer to implement SonarQube with Ubuntu Server is how easy it is to install Docker. Login to your Ubuntu instance, open a terminal window and issue the command:

sudo apt-get install docker-compose -y

The above installation will also install the docker command, which you will use to deploy SonarQube. Once the installation is complete, you will need to add your user to the docker group with:

sudo usermod -aG docker $USER

Please log out and log back in for the changes to take effect.

How to extract the latest image from SonarQube

The first step is to get the official SonarQube image, which is done with the command:

docker pull sonarqube

How to create the necessary volumes

Next, we will create the necessary volumes for SonarQube with the following commands:

docker volume create sonarqube-conf
docker volume create sonarqube-data
docker volume create sonarqube-logs
docker volume create sonarqube-extensions

How to implement SonarQube

With everything ready, deploy the container with the command:

docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 -v sonarqube-conf:/opt/sonarqube/conf -v sonarqube-data:/opt/sonarqube/data -v sonarqube-logs:/opt/sonarqube/logs -v sonarqube-extensions:/opt/sonarqube/extensions sonarqube

Give the container a couple of minutes to unfold.

How to access SonarQube

Before you can log in to SonarQube, you must first set up the database. This is really very simple. Open a web browser and point it to http://SERVER:9000/setup, where SERVER is the IP address of the hosting server. You should then be presented with a link labeled HOME. Click on that and then you should see the SonarQube login window (Figure A).

Figure A

The SonarCube login window.

The default credentials are admin/admin. Upon successful authentication, you will be presented with an update password window (Figure B).

Figure B

Update of the password of the administrator user in SonarQube.

Once you have updated the password, you will be presented with the main SonarCube window (Figure C), where you can start creating your first project.

Figure C

The main window of SonarCube.

And that’s all there is to deploying SonarCube with Docker. Although this instance may not scale to meet your needs, it is a great way to check for issues on small to medium sized projects.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube to get the latest tech tips for business professionals from Jack Wallen.

Leave a Comment