SonarQube installation on docker container with docker-compose

Last Updated on April 16, 2024
SonarQube is an open source tool that is being widely used to manage code quality. SonarQube performs code inspection to detect issues in the code.
In this post we will learn how to install SonarQube on a docker container. We will install community version of SonarQube with LTS edition. We will create one more container for database with latest version of PostgreSQL server. We will have both these containers talk to each other via a network.
Overview of Docker-compose
We can automate the process of creating and running multiple containers with docker-compose.
For using docker-compose, we will write all the commands required to run the container or multiple containers in a file in a structured manner. Then all we need to do is to run a single command to bring multiple containers either up or down.
Learn more about SonarQube:
SonarQube integration with Jenkins
To read more on SonarQube click here
Learn more about Docker:
Simple Jenkins Declarative Pipeline to Push Docker Image To Docker Hub
To read more on Docker click here
Using docker-compose also makes it easy to setup the network for containers, so that they are able to connect with each other.
We will go through the steps one by one, so you just need to follow along.
Pre-requisite
For the purpose of this post, we will assume Docker is already installed on the system. If not then please install it from official docker website. If you already have docker installed but do not have docker compose installed, then please go ahead and install Docker-compose based on your Linux distribution from here.
Create Docker-compose file
You can use any text editor you are comfortable with to create the docker compose file.
To write a docker-compose file, we need to start by mentioning the version of docker compose we would be using in the first line. As shown below we are using version “3”
version: '3'
Create SonarQube container
Next step is to list the containers that we want to create. This will be mentioned under services, followed by container names and their specifications.
services:
<container name>:
image:
Following the above structure, first we will create the SonarQube container as follows
services: sonarqube: image: lts-communityWe will use the community edition of SonarQube from Docker Hub with the latest stable version with long term support.
Next step is to map the ports.
port: '9000:9000'Here we are mapping port 9000 of our host machine to the port 9000 (default port for SonarQube web server) of docker container.
Then we need to setup environment variables.
environment: sonar.jdbc.username= sonar sonar.jdbc.password= sonar sonar.jdbc.url= jdbc:postgresql://db:5432/sonarqubePost this we will setup volumes for persistent storage of SonarQube data.
volumes: sonarqube_conf: /opt/sonarqube/conf sonarqube_data: /opt/sonarqube/data sonarqube_extensions:
/opt/sonarqube/extensions sonarqube_bundled-plugins: /opt/sonarqube/lib/bundled-pluginsSonarQube will create a default network with a default name for multiple containers to interact with each other.
For only our understanding purpose, we will specify a network name explicitly.
networks: sonarnetworkCreate PostgreSQL container
Similarly we will create a container for postgreSQL database.
db: postgres: image: postgres networks: - sonarnetwork environment: POSTGRES_USER= sonar POSTGRES_PASSWORD= sonar volumes: postgresql: /var/lib/postgresql postgresql_data: /var/lib/postgresql/data Resources to be created by Docker-compose file
Once we have the container creation details in the docker-compose file, we will specify all the networks to be used for container interaction. In our case, we have just one network.
Bridge is the default network driver created by compose. We have mentioned the network driver explicitly as bridge for our clarity.
networks: sonarnetwork: driver: bridgeThen we will list all the volumes required by containers in this compose file.
volumes: sonarqube_conf: sonarqube_data: sonarqube_extensions: sonarqube_bundled-plugins: postgresql: postgresql_data:We have created volumes for persistent storage. This will enable us to use the stored data when we bring up the containers again.
Complete Docker-compose file
In this post we have learnt how to install SonarQube on docker container with persistent storage.
Let us now list the complete docker compose file that we have created.
version: '3'
services:
sonarqube:
image: lts-community
port: 9000:9000
environment:
sonar.jdbc.username= sonar
sonar.jdbc.password= sonar
sonar.jdbc.url= jdbc:postgresql://db:5432/sonarqube
volumes:
sonarqube_conf: /opt/sonarqube/conf
sonarqube_data: /opt/sonarqube/data
sonarqube_extensions: /opt/sonarqube/extensions
sonarqube_bundled-plugins: /opt/sonarqube/lib/bundled-plugins
networks:
sonarnetwork
db:
image: postgres
networks:
– sonarnetwork
environment:
POSTGRES_USER= sonar
POSTGRES_PASSWORD= sonar
volumes:
postgresql: /var/lib/postgresql
postgresql_data: /var/lib/postgresql/data
networks:
sonarnetwork:
driver: bridge
volumes:
sonarqube_conf:
sonarqube_data:
sonarqube_extensions:
sonarqube_bundled-plugins:
postgresql:
postgresql_data:Create SonarQube Container
As we have automated the container creation using docker compose, we will be able to use this file to bring the containers up or down whenever required.
We can save this file as docker-compose.yml or docker-compose.yaml. With this file name, we won’t need to specify the filename when running the docker-compose command from the same directory.
However, if we want to save it as a different name e.g. sonar.yml, then we will need to use -f option and then the file name, shown as follows
docker-compose -f sonar.yml up
In the console, we will see a number of commands running internally by docker-compose while creating the containers.
Access SonarQube
Now we should be able to access SonarQube from the host machine via http://localhost:9000.
To access SonarQube container from a different system over the internet, we will use this url : http://<publicipaddressofhost>:9000.
We are using port 9000 to access SonarQube because we have mapped the container port running SonarQube with the same port number (i.e. 9000) on the host machine in the docker-compose file.

Bring down SonarQube Container
When we want to bring down all the containers from the docker-compose file, we will need to run the following command
docker-compose -f sonar.yml down
Conclusion
In this post we have learnt how to install SonarQube from the SonarQube community edition image on Docker Hub. Then, we have also created a PostgreSQL container using PostgreSQL image from Docker Hub on the same network. After this, we have created volumes, so that are able to access SonarQube data even after the containers have been restarted. Finally, we have mapped the ports of the docker container to the host machine so that we are able to access SonarQube via host machine.
Let us know if you found this post helpful.
