2

I’m trying to run Sonarqube in a Docker container on a Centos 7 server using docker-compose. Everything works as expected using named volumes as configured in this docker-compose.yml file:

version: "3"

services:
  sonarqube:
    image: sonarqube
    ports:
      - "9000:9000"
    networks:
      - sonarnet
    environment:
      - sonar.jdbc.url=jdbc:postgresql://db:5432/sonar
    volumes:
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_bundled_plugins:/opt/sonarqube/lib/bundled-plugins

  db:
    image: postgres
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

networks:
  sonarnet:
    driver: bridge

volumes:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_bundled_plugins:
  postgresql:
  postgresql_data:

However, my /var/lib/docker/volumes directory is not large enough to house the named volumes. So, I changed the docker-compose.yml file to use bind mount volumes as shown below.

version: "3"

services:
  sonarqube:
    image: sonarqube
    ports:
      - "9000:9000"
    networks:
      - sonarnet
    environment:
      - sonar.jdbc.url=jdbc:postgresql://db:5432/sonar
    volumes:
      - /data/sonarqube/conf:/opt/sonarqube/conf
      - /data/sonarqube/data:/opt/sonarqube/data
      - /data/sonarqube/extensions:/opt/sonarqube/extensions
      - /data/sonarqube/bundled_plugins:/opt/sonarqube/lib/bundled-plugins

  db:
    image: postgres
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - /data/postgresql:/var/lib/postgresql
      - /data/postgresql_data:/var/lib/postgresql/data

networks:
  sonarnet:
    driver: bridge

However, after running docker-compose up -d, the app starts up but none of the bind mount volumes are written to. As a result, the Sonarqube plugins are not loaded and the sonar postgreSQL database is not initialized. I thought it may be a selinux issue, but I temporarily disabled it with no success. I’m unsure what to look at next.

Bob C.
  • 141
  • 2
  • 15
  • Good recommendations below. However I ended up creating a new sizable logical volume on disk. I then moved all my docker named volumes from the _/var/lib/docker/volumes_ directory to a directory on the new logical volume. Finally I added a sym-link pointing _/var/lib/docker/volumes_ to the new directory holding all the docker volumes. I now have plenty of capacity for my named docker volumes and my Sonarqube docker-compose file works as expected. – Bob C. Dec 28 '18 at 21:20

3 Answers3

2

I think my answer from "How to persist configuration & analytics across container invocations in Sonarqube docker image" would help you as well.

For good measure I have also pasted it in here:


.....

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.


Try it out BobC and let me know.

Have a great day.

Lars Bingchong
  • 323
  • 3
  • 12
1

The below code will help you in a single command I hope so.

Create a new docker-compose file named as docker-compose.yaml,

version: "3"
services:
  sonarqube:
    image: sonarqube:8.2-community
    depends_on:
      - db
    ports:
      - "9000:9000"
    networks:
      - sonarqubenet
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonarqube
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
      - sonarqube_temp:/opt/sonarqube/temp
    restart: on-failure
    container_name: sonarqube
  db:
    image: postgres
    networks:
      - sonarqubenet
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data
    restart: on-failure
    container_name: postgresql

networks:
  sonarqubenet:
    driver: bridge

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  sonarqube_temp:
  postgresql:
  postgresql_data:

Then, execute the command,

$ docker-compose up -d

$ docker container ps
Lakshmikandan
  • 4,301
  • 3
  • 28
  • 37
0

Sounds like the container is running and, as you mentioned, Sonarqube starts-up. When it starts, is it showing that it's using the H2 in memory db? After running docker-compose up -d, use docker logs -f <container_name> to see what's happening on Sonarqube startup. To simplify viewing your logs with a known name, I suggest you also add a container name to your Sonarqube service. For example, container_name: sonarqube.

Also, while I know the plan is to deprecate the use of environment variables for the username, password and jdbc connection, I've had better luck in docker-compose using environment variables rather than the corresponding property value. For the connection string, try: SONARQUBE_JDBC_URL: jdbc:postgresql://db/sonar without specifying the default port for postgres.

ewilan
  • 638
  • 6
  • 16