Skip to content

SonarQube and SQL server in Azure App service for containers with Docker compose

Introduction

SonarQube is one of the world’s most successful tools for source code control. It provides, among other things, the static analysis of the source code, the technical debt and the vulnerabilities.

In today’s article we will focus on the deployment of SonarQube in an Azure PaaS environment (Web app for containers) using Docker compose (https://azure.microsoft.com/fr-fr/services/ app-service / containers).

Azure’s app service provides an abstraction of the runtime environment. The focus is more on the application rather than the operational maintenance of the infrastructure. The features provided by default:

  • An SSL / TLS termination provided with the App service plan
  • A shared storage space that can reach 250 GB
  • Manual or automatic scaling according to the pricing plan

Before starting the deployment journey, the following items should be noted:

  • An Azure subscription! If you do not yet have an Azure account you can create an account with a few clicks (https://azure.microsoft.com). Azure is giving away a month’s trial with a $ 200 credit.
  • At the time of writing this article, Docker compose is still in a preview version. The scripts and constraints may change when the final version is released.

Below is the overall SonarQube deployment diagram :

  • App Service Plan: To define the computing resources and the host OS
  • Web app with publication support in Docker containers
  • Storage account

The Docker compose file of the solution consists of the SonarQube service exposing the port 9000, and an internal non-public SQL server service on the default port 1433. For data persistence, we will use mounting volumes on Azure File Share that we will associate with the web app.

The stages of deployment:

1- Creation of the group resource

Go to the resource group and click on add. Choose a name and region

2- Create the app service plan

  • Set the resource group name.
  • The name of your service plan.
  • The operating system : Linux
  • The pricing tier : In this example I test my deployment with basic B3 pricing tier. But basic B2 will works as well also.

3- Create the Azure storage account

In the basics section :

  • Chose the storage account name
  • The location
  • The storage V2
  • Replication LRS. It is recommended to use other options for production purposes.
  • Leave the values as default in the other section wizard.

4- Create the Azure file share to mount Docker volumes

  • Access the newly created storage account
  • Go to File Shares
  • Click on « + file share » and add these items with default values. (don’t change the tiers option, keep the transaction optimized) :
    • mssql
    • sonarqube-bundled-plugins
    • sonarqube-conf
    • sonarqube-data
    • sonarqube-extensions

IMPORTANT : We must initiate some data in the volume MSSQL, as per default the Docker initiation command doesn’t create a new database, and therefore no sonar.mdf and sonar.ldf files will be available. Further more, there are some issues creating other files like mssql/.system/system/asr.hiv from the container once the volume is mounted due to constraints in App service. The workaround I found to resolve the issue is the following :

1- Create a Docker compose file with a SQL service and run it locally using : docker-compose up

version: '3.3'
   services:
      database_1:
        image: mcr.microsoft.com/mssql/server:2019-latest
        container_name: mssql-server
        restart: always
        ports:
          - "5550:1433"
        volumes:
            mssqlsalah:/var/opt/mssql
        environment:
           ACCEPT_EULA: 'Y'
           MSSQL_SA_PASSWORD: hsZGlEE"{OH=3lFTLGQtBRr]y
           MSSQL_PID: Express
           MSSQL_COLLATION: SQL_Latin1_General_CP1_CS_AS
   volumes:
      mssqlsalah:

2- Connect to the database with the credentials used  on port 5550 and then create a data base named sonar.

3- Copy the files located in /var/opt/mssql from your container into a location in your host drive.

4- Open the file share mssql and make the following operations :

Create the folder .system > system (the folder system must be a child of the folder .system) and upload the files *.hiv from your host drive.

Create a folder « data » in the root of the file share, and upload the content of the data located in your host.

And now we are good to go !

5- Create the Docker compose file of the deployment

 
 1-  version: '3.3'
 2-   
 3-  services:
 4-     sonarqube:
 5-       depends_on:
 6-         - db
 7-       image: sonarqube:8.5-community
 8-       command: "-Dsonar.search.javaAdditionalOpts=-Dnode.store.allow_mmapfs=false"
 9-       ports:
 10-        - "9000:9000"
 11-      volumes:
 12-        - sonarqube-conf:/opt/sonarqube/conf
 13-        - sonarqube-data:/opt/sonarqube/data
 14-        - sonarqube-extensions:/opt/sonarqube/extensions
 15-        - sonarqube-bundled-plugins:/opt/sonarqube/lib/bundled-plugins
 16-      environment:
 17-       - SONARQUBE_JDBC_URL=jdbc:sqlserver://db:1433;databaseName=sonar
 18-       - SONARQUBE_JDBC_USERNAME=sa
 19-       - SONARQUBE_JDBC_PASSWORD= x^DY&%K1#BJs[]@7=ju]J
 20-       - SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true
 21-  
 22-    db:
 23-       image: mcr.microsoft.com/mssql/server:2019-latest
 24-       container_name: mssql-server
 25-       volumes:
 26-         - mssql:/var/opt/mssql
 27-       environment:
 28-           ACCEPT_EULA: 'Y'
 29-           MSSQL_SA_PASSWORD: x^DY&%K1#BJs[]@7=ju]J
 30-           MSSQL_PID: Express
 31-           MSSQL_COLLATION: SQL_Latin1_General_CP1_CS_AS
 32-  
 33- volumes:
 34-   sonarqube-conf:
 35-      external: true
 36-   sonarqube-data:
 37-      external: true
 38-   sonarqube-extensions:
 39-      external: true
 40-   sonarqube-bundled-plugins:
 41-      external: true
 42-   mssql:
 43-     external: true 

IMPORTANT : Note that volumes are declared as external in the compose file, and the use of “-Dsonar.search.javaAdditionalOpts=-Dnode.store.allow_mmapfs=false” as a command to start the SonarQube container to avoid the max virtual memory areas issue as shown below :

6- Create the web app

Add a web app and add the basic items as shown below :

Make sure to choose the publish option : « Docker Container »

In the next session, choose the Docker compose option and the image source as Docker hub. And upload the Docker compose file created in section 5

Continue the process and create the resource.

7- Mount the volumes for the containers

Click the configuration link in the web app, and then in path mapping and add these items as described below :

And we are done. That’s all the steps to make the deployment !

To make sure the deployment is OK, you can click in the log stream of the web app and check the status of deployment.

Here is the final result if all the steps were set correctly:

You can force the restart of the web application by making a change to the compose file, and you can check that the items are not lost after restarting thanks to mounting volume we made with Azure Files.

Final words …

Leave a comment if you found this article useful, and I will be pleased to answer your questions if you have any problem.

Salah.

Published inAzureDockerSql Server

5 Comments

  1. Loubna Loubna

    Very interesting and insightful. Thank you for sharing

  2. Wahid Wahid

    Nice ! Thanks for sharing,

  3. zakaria zakaria

    Very helpful thanks for sharing

  4. Arpan Ghosh Arpan Ghosh

    I did the configuration as you mentioned but still getting the error.
    2022-04-22T13:50:28.973Z ERROR – Pull image threw Exception: Object reference not set to an instance of an object.
    2022-04-22T13:50:28.974Z ERROR – Image pull failed: Verify docker image configuration and credentials (if using private repository)
    2022-04-22T13:50:28.975Z ERROR – multi-container unit was not started successfully

  5. Arpan Ghosh Arpan Ghosh

    I did the configuration as you mentioned but still getting the error.
    Can you please help me here ?

    2022-04-22T13:50:28.973Z ERROR – Pull image threw Exception: Object reference not set to an instance of an object.
    2022-04-22T13:50:28.974Z ERROR – Image pull failed: Verify docker image configuration and credentials (if using private repository)
    2022-04-22T13:50:28.975Z ERROR – multi-container unit was not started successfully

Leave a Reply

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