How to persist configuration & analytics across container invocations in Sonarqube docker image

  • Sonarqube image comes with a temporary h2 database engine which is not recommended for production and doesn't persist across container restarts.
  • We need to setup a database of our own and point it to Sonarqube at the time of starting the container.
  • Sonarqube docker images exposes two volumes "$SONARQUBE_HOME/data", "$SONARQUBE_HOME/extensions" as seen from Sonarqube Dockerfile.

Since we wanted to persist the data across invocations, we need to make sure that a production grade database is setup and is linked to Sonarqube and the extensions directory is created and mounted as volume on the host machine so that all the downloaded plugins are available across container invocations and can be used by multiple containers (if required).

Database Setup:

create database sonar;
grant all on sonar.* to `sonar`@`%` identified by "SOME_PASSWORD";
flush privileges;

# since we do not know the containers IP before hand, we use '%' for sonarqube host IP.

It is not necessary to create tables, Sonarqube creates them if it doesn't find them.

Starting up Sonarqube container:

# create a directory on host
mkdir /server_data/sonarqube/extensions
mkdir /server_data/sonarqube/data # this will be useful in saving startup time

# Start the container
docker run -d \
    --name sonarqube \
    -p 9000:9000 \
    -e SONARQUBE_JDBC_USERNAME=sonar \
    -e SONARQUBE_JDBC_PASSWORD=SOME_PASSWORD \
    -e SONARQUBE_JDBC_URL="jdbc:mysql://HOST_IP_OF_DB_SERVER:PORT/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance" \
    -v /server_data/sonarqube/data:/opt/sonarqube/data \
    -v /server_data/sonarqube/extensions:/opt/sonarqube/extensions \
    sonarqube

Hi @VanagaS and others landing here.

I just wanted to provide an alternative to the above. Maybe some would even consider it an easier one.

Notice this line SONARQUBE_HOME in the Dockerfile for the docker-sonarqube image. We can control this environment variable.

When using docker run. Simply do:

txt docker run -d \ ... ... -e SONARQUBE_HOME=/sonarqube-data -v /PERSISTENT_DISK/sonarqubeVolume:/sonarqube-data

This will make Sonarqube create the conf, data and so forth folders and store data therein. As needed.


Or with Kubernetes. In your deployment YAML file. Do:

txt ... ... env: - name: SONARQUBE_HOME value: /sonarqube-data ... ... volumeMounts: - name: app-volume mountPath: /sonarqube-data

And the name in the volumeMounts property points to a volume in the volumes section of the Kubernetes deployment YAML file. This again will make Sonarqube use the /sonarqube-data mountPath for creating extenions, conf and so forth folders, then save data therein.

And voila your Sonarqube data is thereby persisted.

I hope this will help others.

N.B. Notice that the YAML and Docker run examples are not exhaustive. They focus on the issue of persisting Sonarqube data.