SonarQube lost data

I successfully started SonarQube using docker image, which points on Postgres DB. and then I tried with docker-compose up down many times, to make sure all data were saved, and everything went fine.

Then we apply for the license!
But, after a few days. we take docker-compose down, and up again data were lost, while it should be stored in a local volume. Note[other images in the same docker-compose file like Jenkins, doesn’t lose the data]

Can you please advise us ? what happened ? did we lose the data at all? how we can avoid this again ?

here is the docker-copose.yml:

version: "3"

services:
  sonarqube:
    image: sonarqube:lts-developer
    restart: unless-stopped
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
        - sonarqube_conf:/var/lib/docker/volumes/sonarqube_conf
        - sonarqube_data:/var/lib/docker/volumes/sonarqube_data
        - sonarqube_extensions:/var/lib/docker/volumes/sonarqube_extensions
        - sonarqube_bundled-plugins:/var/lib/docker/volumes/bundled-plugins
    ports:
      - "9000:9000"
      - "9092:9092"
  db:
    image: postgres:latest
    restart: unless-stopped
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
      POSTGRES_DB: sonar
    volumes:
      - postgresql:/var/lib/docker/volumes/postgresql_data
      - postgresql_data:/var/lib/docker/volumes/postgresql_data/_data

  jenkins:
    image: jenkins:2.60.3
    restart: unless-stopped
    privileged: true
    user: root
    ports:
      - 8083:8080
      - 50003:50000
    container_name: jenkins-ci-cd
    volumes:
      - jenkins_data:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
volumes:
  postgresql_data:
  sonarqube_bundled-plugins:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  sonarqube_conf:
  jenkins_data:
  postgresql:

Hello Abed and welcome to the community!

To me it looks like your problem is here:

  db:
    image: postgres:latest
    volumes:
      - postgresql:/var/lib/docker/volumes/postgresql_data
      - postgresql_data:/var/lib/docker/volumes/postgresql_data/_data

/var/lib/docker/volumes/postgresql_data and /var/lib/docker/volumes/postgresql_data/_data are not the directories where the Postgres image stores its data internally. It should likely be /var/lib/postgresql and /var/lib/postgresql/data. So my guess is that the database content always stayed inside of the container and is likely gone.

On an unrelated note, using postgres:latest could become a problem once a new major version of Postgres is released, since the database is not migrated automatically and the service will refuse to start. I would recommend using a fixed version like postgres:13.5.
The jenkins:2.60.3 image seems to be deprecated for multiple years according to the doc. Running it as root and mounting the Docker socket into the container is quite dangerous. If an attacker is able to exploit a vulnerability in the service, they are also able to control the host system.

2 Likes