Question: Sonarqube official docker image, is not persisting any configuration changes like: `creating users, changing root password or even installing new plugins`. 

Once the container is restarted, all the configuration changes disappear and the installed plugins are lost. Even the projects' keys and their previous QA analytics data is unavailable after a restart. 

How can we persist the data when using Sonarqube's official docker image?

Answer: 
* 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](https://github.com/SonarSource/docker-sonarqube/blob/442f950dae232fca8e1e919e017270971cee46f6/4.5.6/Dockerfile#L37%20Sonarqube%20Dockerfile).

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

```
Since Sonarqube v7.9 , Mysql is not supported. One needs to use postgresql. Install Postgresql and configure to run on host ip rather than localhost, private ip is preferred.

Reference: <https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04>

```
postgres=# create database sonar;
postgres=# create user sonar with encrypted password 'mypass';
postgres=# grant all privileges on database sonar to sonar;

```

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=mypass   

-e SONARQUBE\_JDBC\_URL=jdbc:postgresql://{host/private ip only}:5432/sonar   

-v /server\_data/sonarqube/data:/opt/sonarqube/data   

-v /server\_data/sonarqube/extensions:/opt/sonarqube/extensions   

sonarqube

You may face this error when you do "docker logs container\_id"

> 
> ERROR: [1] bootstrap checks failed [1]: max virtual memory areas
> vm.max\_map\_count [65530] is too low, increase to at least [262144]
> 
> 
> 

This is the fix, run on your host

```
sysctl -w vm.max_map_count=262144

```

In order to add hostname
edit `/etc/postgresql/10/main/postgresql.conf`

In order to add docker as client for postgres edit `/etc/postgresql/10/main/pg_hba.conf`

10 - postgres version used