How to run SonarQube with docker-compose

Shivam Chauhan July 22, 2022
How to run sonarqube with docker compose

It is nearly impossible to overstate the significance of code quality with your project’s success. You can spend endless hours and money on your agile development process, however, if the code doesn’t work, your development project might not progress. Running SonarQube might help developers write safer, reliable and quality code. 

Since SonarQube is well versed with the docker compose environment, it would be easier to integrate the tool in your build project. In this article, we will learn to set up a SonarQube server with Docker without having to spend much time in installing and configuration.

Prerequisite

To follow this article you must have a brief idea on SonarQube, docker and docker compose.

What is SonarQube?

SonarQube is a continuous inspection tool which can be used to test the quality of the code. It analyzes the source code and sends the analytical report to us to check on the final quality. 

It is available as an open-source platform and supports multiple programming languages like Java, Python, Javascript, TypeScript, COBOL, HTML, XML, C#, C/C++ Apex, Object-C, Swift, Kotlin, Ruby, Scala, CSS, ABAP etc. However, some languages require a commercial license to work with. 

You do not have to alter your workflow or learn new tools to install SonarQube since it can easily be integrated with common build tools such as Ant, Maven, Make, Gradle, MS Build etc. All you have to do is add appropriate plugins for a smoother analysis in your build process. 

What is Docker Compose? 

Docker compose is a tool developed to define and share multi-container applications. With docker compose, you can create a YAML file for defining services within a single command. This command can either build up or tear down your entire build. 

So let’s begin our tutorial on:

How to Setup SonarQube Server with Docker-compose?

1. Start with creating the SonarQube with the Docker-compose.yml file

Apply pending updates:

sudo apt update

Now install the docker compose installation:
Command to install the docker-compose

sudo apt-get install docker-compose -y

Start with creating the SonarQube with the Docker-compose.yml file

2. Add current user to the docker group:

sudo usermod -aG Docker $USER

3. Create a new directory:

mkdir ~/sonar

cd sonal/

4. Now lets create the yaml file which will sets up the SonarQube with PostgreSQL

sudo nano docker-compose.yml

version: "3"

services:

  SonarQube:

    image: SonarQube:community

    depends_on:

      - db

    environment:

      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar

      SONAR_JDBC_USERNAME: sonartest

      SONAR_JDBC_PASSWORD: sonartest

    volumes:

      - SonarQube_data:/opt/SonarQube/data

      - SonarQube_extensions:/opt/SonarQube/extensions

      - SonarQube_logs:/opt/SonarQube/logs

    ports:

      - "9000:9000"

  db:

    image: postgres:12

    environment:

      POSTGRES_USER: sonartest

      POSTGRES_PASSWORD: sonartesrt

    volumes:

      - postgresql:/var/lib/postgresql

      - postgresql_data:/var/lib/postgresql/data

volumes:

  SonarQube_data:

  SonarQube_extensions:

  SonarQube_logs:

  postgresql:

  postgresql_data:

Consult our experts

The SonarQube section containing the version uses the latest version of image. Environment section tells the variables for connecting to the postgresql database.SonarQube volume section used for the storing configuration.

Next the postgresql also uses the latest version, and the environment section used for the SonarQube database- JDBC connection storing.

5. Now execute the compose file using Docker compose command:

sudo docker-compose up -d

Now execute the compose file using Docker compose command

6. List the running containers 

docker ps command to list the running containers, as we can see both SonarQube and postgreSQL containers are running.

List the running containers

7. Command to check the docker compose logs

docker-compose logs

Now open in your browser http://localhost:9000 and login to our default admin account.

8. Use the default credentials admin:admin to login.

Use the default credentials adminadmin to login

We have successfully built a SonarQube server.

Useful Links to guide you better:

SonarQube on Docker Hub: https://docs.SonarQube.org/latest/setup/install-server/

PostgreSQL on Docker Hub: https://hub.docker.com/r/bitnami/SonarQube/

Conclusion

In this post, you learnt how to successfully run SonarQube with the Docker compose environment. You can see how easy it is to integrate Sonar in your existing development workflow.

However, it is suggested to rely on an experienced DevOps service provider when experimenting with your existing code. For more personalized technical guidance, reach out to our development experts to guide you through a smoother development process. 

THE AUTHOR
Shivam Chauhan
Prev PostNext Post
Read more blogs
implementing DevOps Pipelines

Practically Implementing DevOps Pipelines

DevOps has been incredibly popular in recent years as IT decision-makers all across the world have begun to see its advantages.  DevOps is both a methodology and a way of thinking. As a methodology, it aims to combine development and operations into a single team. As a way of thinking, DevOps emphasizes cooperation, communication, and…

Sudeep Srivastava
Why is DevOps a Good Investment For Your Enterprise

Why is DevOps a Good Investment for your Enterprise?

The DevOps trend has really started to take shape in the last few years. It's a movement of IT leaders who want to see things change for the better—to provide amazing software to their clients consistently and with confidence. The global DevOps market size was valued at $ 6.78 billion in 2020 and is projected…

Sudeep Srivastava
How can DevOps and agile work together to help your business grow

How Can DevOps and Agile Work Together to Help Your Business Grow?

In today’s fast-paced technological development and deliveries, companies focus on accelerated deliveries while maintaining high customer value. DevOps and Agile are two different technologies focused on delivering the same output. The primary function of the DevOps application is to reduce the number of steps during the software development process for faster delivery of the software…

Sudeep Srivastava