Using SonarQube in Azure Container Instances

Cristiano Rodrigues
4 min readOct 22, 2019

Hi! In this article, I'm going to talk about SonarQube running at Azure Container Instances and using Azure SQL for persistent data. This scenario is most used in the Production Environment.

SonarQube is an open-source platform for continuous inspection. It’s used to analyze the code and verify its quality (bugs, code smells and vulnerabilities).

I like to work with SonarQube, it is efficient, fast and easy to include in a development pipeline because it supports many languages and it has easy integration with the best build systems and CI engines in the market.

Create an Environment

At this point, we will use Azure CLI to create objects in Azure, but first, we need to authenticate in Azure. There are several ways to authenticate a user and we will use the default authenticate in the Azure CLI, the interactive sign in. In this mode, the default browser is opened for you to enter credentials and sign-in.

az login

After you sign in Azure we will organize objects through Resource Group and all created objects will be put inside it.

az group create -n rg-sonarqube -l eastus

Now we have a Resource Group named rg-sonarqube, it’s created at Azure Datacenter in United States East where all objects will be created for lower latency.

SonarQube uses an embedded memory database called H2, it’s installed when you use a default option during the installation, but not recommended in a production environment because all data are lost when a host is down or powering off. For production, scenarios are must recommended using persistence databases, like SQL Server, Postgres, and MySQL to save all data.

In this article, we will use an Azure SQL it’s a database as a service (DBaaS) and a good choice for persisting the data at the cloud.

Using Azure CLI we will create a logical server in previously created Resource Group, define a firewall rule to allow Azure Services access to the server and database. We will create a database specificating the database collation as SQL_Latin1_General_CP1_CS_AS. This collation is required to install the SonarQube in Azure SQL.

# Create a logical server
az sql server create \
--name srv-sql-sonarqube \
--resource-group rg-sonarqube \
--location eastus \
--admin-user sonar \
--admin-password <your password>
# Configure a firewall rule to allow Azure Services
az sql server firewall-rule create \
--resource-group rg-sonarqube \
--server srv-sql-sonarqube \
-n AzureServices \
--start-ip-address 0.0.0.0 \
--end-ip-address 0.0.0.0
# Create a database in the server
az sql db create \
--resource-group rg-sonarqube \
--server srv-sql-sonarqube \
--name sonarqubeDb \
--service-objective S0
--collation SQL_Latin1_General_CP1_CS_AS

As a final part of the article, we will create a container instance using an official SonarQube image. In this image, environment variables use a dot in the name, but Azure Container Instances don’t support environment variable names using a dot, therefore, we will use environment variables using underscore it’s marked as deprecated, but it’s still working at 7.7-community image.

We will create a container instance with 2 CPU, 3.5GB of memory, using a Linux OS type, exposing a 9000 TCP port, a public IP for external access and connected to Azure SQL. This container will be created inside the same resource group previously created.

# Create a container instance using offical SonarQube imageaz container create --resource-group rg-sonarqube \ 
--location eastus --name sonarqube \
--cpu 2 --memory 3.5 \
--image sonarqube:7.7-community \
--os-type Linux --ip-address Public \
--environment-variables\
'SONARQUBE_JDBC_USERNAME'='sonar' \
'SONARQUBE_JDBC_PASSWORD'='<your password>' \
'SONARQUBE_JDBC_URL'='jdbc:sqlserver://srv-sql-sonarqube.database.windows.net:1433;database=sonarqubeDb;user=sonar@srv-sql-sonarqube;password=<your password>;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30'\
--ports 9000 \
--protocol TCP

When we executing the command above the SonarQube will be installed at the Azure Container Instance and you could access through the public IP assigned by Azure and you will see SonarQube about page. To Log In on SonarQube click on the right top corner and enter with the default credentials (admin/admin).

SonarQube about page.
SonarQube home page after login.

Now, its time to enjoy the SonarQube!!!

In another article, I will talk about how-to configure SonarQube to receive an Azure DevOps C# project for code analysis.

See you next!!

--

--

Cristiano Rodrigues

Microsoft MVP | Solutions Architect with more than 15 years in software development. In love for Docker and Kubernetes, a specialist in .NET and SQL Server.