Install SonarQube Inside Docker Container in Linux

Install SonarQube Inside Docker Container in Linux

SonarQube is a code quality assurance platform that can be used for continuous code analysis and inspection. SonarQube is an open-source project available under the GPLv3 license.

This tutorial explains how to install SonarQube inside a Docker container in the Linux. Commands have been tested on Ubuntu.

Prepare environment

Make sure you have installed Docker in your system. If you are using Ubuntu, installation instructions can be found in the post.

You also need to have a running PostgreSQL container. Instructions can be found in the post.

Install SonarQube

Before starting, create sonarqube database:

docker exec -it postgresql psql -U postgres -c "CREATE DATABASE sonarqube"

Elasticsearch is used by SonarQube. ElasticSearch can crash due to low max virtual memory area. To increase it, run the following command:

echo 'vm.max_map_count=262144' | sudo tee -a /etc/sysctl.conf

Reload configuration with new value:

sudo sysctl -p
  • Host network

Run the following command to create a container for SonarQube that uses host network:

docker run -d --name=sonarqube --restart=always --network=host \
    -v /opt/sonarqube/data:/opt/sonarqube/data \
    -v /opt/sonarqube/logs:/opt/sonarqube/logs \
    -v /opt/sonarqube/extensions:/opt/sonarqube/extensions \
    -e SONAR_JDBC_USERNAME=postgres \
    -e SONAR_JDBC_PASSWORD=pwd123 \
    -e SONAR_JDBC_URL=jdbc:postgresql://127.0.0.1:5432/sonarqube \
    sonarqube

PostgreSQL container should run on host network as well.

  • User-defined bridge network

User-defined bridge network can be used for listening on different port. By default, SonarQube service is listening on port 9000. It can be changed with -p option.

docker network create app-net
docker run -d --name=sonarqube --restart=always --network=app-net \
    -p 8080:9000 \
    -v /opt/sonarqube/data:/opt/sonarqube/data \
    -v /opt/sonarqube/logs:/opt/sonarqube/logs \
    -v /opt/sonarqube/extensions:/opt/sonarqube/extensions \
    -e SONAR_JDBC_USERNAME=postgres \
    -e SONAR_JDBC_PASSWORD=pwd123 \
    -e SONAR_JDBC_URL=jdbc:postgresql://postgresql:5432/sonarqube \
    sonarqube

PostgreSQL container should run on the same user-defined bridge network as well.

Notes:

  • It might take a while before initialization is finished and the Docker container starts to respond to requests.
  • The SONAR_JDBC_USERNAME and SONAR_JDBC_PASSWORD can be used to specify PostgreSQL credentials.
  • When user-defined bridge network is used, don't forget to change SONAR_JDBC_URL. It contains PostgreSQL container name.

Testing SonarQube

Open a web browser and go to http://<IP_ADDRESS>:9000, where <IP_ADDRESS> is the IP address of the system. Log in to the dashboard with the default username (admin) and password (admin).

SonarQube Inside Docker Container in Linux

Uninstall SonarQube

To completely remove SonarQube, remove its container:

docker rm --force sonarqube

Remove SonarQube image:

docker rmi sonarqube

You can also remove SonarQube data:

sudo rm -rf /opt/sonarqube

If a user-defined bridge network was created, you can delete it as follows:

docker network rm app-net

Leave a Comment

Cancel reply

Your email address will not be published.