Skip to content

Instantly share code, notes, and snippets.

@Warchant
Last active May 28, 2024 20:24
Show Gist options
  • Save Warchant/0d0f0104fe7adf3b310937d2db67b512 to your computer and use it in GitHub Desktop.
Save Warchant/0d0f0104fe7adf3b310937d2db67b512 to your computer and use it in GitHub Desktop.
docker-compose file to setup production-ready sonarqube
version: "3"
services:
sonarqube:
image: sonarqube
expose:
- 9000
ports:
- "127.0.0.1:9000:9000"
networks:
- sonarnet
environment:
- sonar.jdbc.url=jdbc:postgresql://db:5432/sonar
- sonar.jdbc.username=sonar
- sonar.jdbc.password=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:
volumes:
sonarqube_conf:
sonarqube_data:
sonarqube_extensions:
sonarqube_bundled-plugins:
postgresql:
postgresql_data:
@isbkch
Copy link

isbkch commented Jul 9, 2019

Stuck in a restart loop...

@carlosen14
Copy link

not working

carlos@carlos-lenovo:~/test/sonarqube$ docker-compose up
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Recreating sonarqube      ... done
Recreating sonarqube_db_1 ... done
Attaching to sonarqube_sonarqube_1, sonarqube_db_1
sonarqube_1  | 2019.07.19 17:07:52 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/sonarqube/temp
sonarqube_1  | 2019.07.19 17:07:52 INFO  app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
sonarqube_1  | 2019.07.19 17:07:52 INFO  app[][o.s.a.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/opt/sonarqube/elasticsearch]: /opt/sonarqube/elasticsearch/bin/elasticsearch
db_1         | 2019-07-19 17:07:52.936 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1         | 2019-07-19 17:07:52.937 UTC [1] LOG:  listening on IPv6 address "::", port 5432
sonarqube_1  | 2019.07.19 17:07:52 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
db_1         | 2019-07-19 17:07:52.947 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
sonarqube_1  | 2019.07.19 17:07:52 INFO  app[][o.e.p.PluginsService] no modules loaded
sonarqube_1  | 2019.07.19 17:07:52 INFO  app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
sonarqube_1  | OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
db_1         | 2019-07-19 17:07:52.966 UTC [24] LOG:  database system was shut down at 2019-07-19 16:47:36 UTC
db_1         | 2019-07-19 17:07:52.972 UTC [1] LOG:  database system is ready to accept connections
sonarqube_1  | ERROR: [1] bootstrap checks failed
sonarqube_1  | [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
sonarqube_1  | 2019.07.19 17:07:56 WARN  app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [es]: 78
sonarqube_1  | 2019.07.19 17:07:56 INFO  app[][o.s.a.SchedulerImpl] Process[es] is stopped
sonarqube_1  | 2019.07.19 17:07:56 INFO  app[][o.s.a.SchedulerImpl] SonarQube is stopped
sonarqube_sonarqube_1 exited with code 0

@Warchant
Copy link
Author

@carlosen14 pay attention to errors it prints:

sonarqube_1  | ERROR: [1] bootstrap checks failed
sonarqube_1  | [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

@carlosen14
Copy link

Thanks for pointing me @Warchant ..

here is the info for linux.

@carlosen14
Copy link

Now it works..

Just to let you know these are deprecate

      - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
      - SONARQUBE_JDBC_USERNAME=sonar
      - SONARQUBE_JDBC_PASSWORD=sonar

This is the new way

    - sonar.jdbc.username=sonar
    - sonar.jdbc.password=sonar
    - sonar.jdbc.url=jdbc:postgresql://db/sonar

@hoffic-cz
Copy link

For me, it just exits without telling me much about what happened:

root@sonarqube:~/sonarqube# docker-compose up
Recreating sonarqube_db_1 ... done
Recreating sonarqube_sonarqube_1 ... done
Attaching to sonarqube_db_1, sonarqube_sonarqube_1
db_1         | 2019-07-21 09:11:48.587 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1         | 2019-07-21 09:11:48.587 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1         | 2019-07-21 09:11:48.589 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1         | 2019-07-21 09:11:48.610 UTC [22] LOG:  database system was shut down at 2019-07-21 09:09:45 UTC
db_1         | 2019-07-21 09:11:48.614 UTC [1] LOG:  database system is ready to accept connections
sonarqube_1  | 09:11:50.031 [main] WARN org.sonar.application.config.AppSettingsLoaderImpl - Configuration file not found: /opt/sonarqube/conf/sonar.properties
sonarqube_1  | 2019.07.21 09:11:50 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/sonarqube/temp
sonarqube_1  | 2019.07.21 09:11:50 INFO  app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
sonarqube_1  | 2019.07.21 09:11:50 INFO  app[][o.s.a.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/opt/sonarqube/elasticsearch]: /opt/sonarqube/elasticsearch/bin/elasticsearch
sonarqube_1  | 2019.07.21 09:11:50 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
sonarqube_1  | 2019.07.21 09:11:50 INFO  app[][o.e.p.PluginsService] no modules loaded
sonarqube_1  | 2019.07.21 09:11:50 INFO  app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
sonarqube_1  | OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
sonarqube_1  | 2019.07.21 09:11:54 WARN  app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [es]: 1
sonarqube_1  | 2019.07.21 09:11:54 INFO  app[][o.s.a.SchedulerImpl] Process[es] is stopped
sonarqube_1  | 2019.07.21 09:11:54 INFO  app[][o.s.a.SchedulerImpl] SonarQube is stopped
sonarqube_sonarqube_1 exited with code 0

@carlosen14
Copy link

@hoffic-cz check dir permissions, you need others able to write the sonar folder.. i couldn't solve it, so i mount as docker volumes, not as folder paths.. =/

@tidusete
Copy link

Good Job!

@wrlyonsjr
Copy link

@hoffic-cz I had the same problem because the user in the container (named "sonarqube") had no write permission to the mount location on the host. I believe docker-compose was creating those directories as root. I solved it by creating those directories manually and opening up the permissions. I'll bet there's a more secure way involving making a sonarqube user on the host machine.

@partypeopleland
Copy link

thanks a lot!
i can use it on docker-for-windows now.

@jpirir
Copy link

jpirir commented Mar 27, 2020

In a small instance, add
sudo sysctl -w vm.max_map_count=262144
elastic/elasticsearch-docker#92

and append to the compose

      ulimits:
          nofile:
              soft: 65536
              hard: 65536

@Pingolus
Copy link

Thanks everybody.

If you need to disable seccomp, you can add this environment property (not recommanded but needed if your system kernel is not compliant with) :
- sonar.search.javaAdditionalOpts=-Dbootstrap.system_call_filter=false

My updated docker-compose file :

version: "3"

services:
  sonarqube:
    image: sonarqube:8.2-community
    container_name: sonarqube
    hostname: sonarqube
    expose:
      - 9000
    user: "999"
    ports:
      - <your-host-port>:9000
    networks:
      - sonarnet
    environment:
    - sonar.jdbc.username=sonar
    - sonar.jdbc.password=<your-sonar-pwd>
    - sonar.jdbc.url=jdbc:postgresql://db/sonar
    - sonar.search.javaAdditionalOpts=-Dbootstrap.system_call_filter=false
    volumes:
      - <your-volume-path>:/opt/sonarqube/logs
      - <your-volume-path>:/opt/sonarqube/data
      - <your-volume-path>:/opt/sonarqube/extensions

  db:
    image: postgres:12.0
    container_name: sonarqube-pg
    hostname: sonarqube-pg
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=<your-sonar-pwd>
    volumes:
      - <your-volume-path>:/var/lib/postgresql
      - <your-volume-path>:/var/lib/postgresql/data

networks:
  sonarnet:

@gustinsi
Copy link

gustinsi commented Nov 23, 2020

Thanks everyone!

I had a few issues to fix. In the end, this worked for me:

version: "3"

services:
  sonarqube:
    image: sonarqube:8.5.1-community
    container_name: sonarqube
    hostname: sonarqube
    ports:
      - 9000:9000
    environment:
      - sonar.jdbc.username=sonar
      - sonar.jdbc.password=<your-sonar-pswd>
      - sonar.search.javaAdditionalOpts=-Dbootstrap.system_call_filter=false
    volumes:
      - <your-volume-path>/logs:/opt/sonarqube/logs
      - <your-volume-path>/data:/opt/sonarqube/data
      - <your-volume-path>/extensions:/opt/sonarqube/extensions

  db:
    image: postgres:13.1
    container_name: db
    hostname: db
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=<your-sonar-pswd>
    volumes:
      - pg_db:/var/lib/postgresql
      - pg_data:/var/lib/postgresql/data
    ulimits:
      nofile:
        soft: 65536
        hard: 65536

volumes:
  pg_db:
    driver: local
  pg_data:
    driver: local

@humbertodias
Copy link

Execute it before

sysctl -w vm.max_map_count=262144

Ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/vm-max-map-count.html

@xts-velkumars
Copy link

@gustinsi, working charm

@jeffqev
Copy link

jeffqev commented May 24, 2021

If you want, you can also scan with docker Other (for JS, TS, Go, Python, PHP, ...) using sonarsource/sonar-scanner-cli

in your src folder
docker run --rm -it -v $(pwd):/usr/src --network=host sonarsource/sonar-scanner-cli bash

And just copy the sonarqube commands in the container bash
sonar-scanner \
-Dsonar.projectKey=yourproyectkey\
-Dsonar.sources=. \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.login=yourproyectlogin

@wellingtonlorindo
Copy link

Execute it before

sysctl -w vm.max_map_count=262144

Ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/vm-max-map-count.html

It works! Thanks a lot @humbertodias

@iguilhermeluis
Copy link

It works! Thanks

@AnantDevop
Copy link

at org.elasticsearch.env.NodeMetadata.upgradeToCurrentVersion(NodeMetadata.java:80)
sonarqube | at org.elasticsearch.env.NodeEnvironment.loadNodeMetadata(NodeEnvironment.java:482)
sonarqube | at org.elasticsearch.env.NodeEnvironment.(NodeEnvironment.java:356)
sonarqube | at org.elasticsearch.node.Node.(Node.java:427)
sonarqube | at org.elasticsearch.node.Node.(Node.java:309)
sonarqube | at org.elasticsearch.bootstrap.Bootstrap$5.(Bootstrap.java:234)
sonarqube | at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:234)
sonarqube | at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:434)
sonarqube | at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:166)
sonarqube | at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:157)
sonarqube | at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:77)
sonarqube | at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:112)
sonarqube | at org.elasticsearch.cli.Command.main(Command.java:77)
sonarqube | at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:122)
sonarqube | at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:80)
sonarqube | For complete error details, refer to the log at /opt/sonarqube/logs/sonarqube.log
sonarqube | 2022.11.18 02:36:00 WARN app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [es]: 1
sonarqube | 2022.11.18 02:36:00 INFO app[][o.s.a.SchedulerImpl] Process[es] is stopped
sonarqube | 2022.11.18 02:36:00 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped
sonarqube | 2022.11.18 02:37:01 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/sonarqube/temp
sonarqube | 2022.11.18 02:37:01 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on [HTTP: 127.0.0.1:9001, TCP: 127.0.0.1:44793]
sonarqube | 2022.11.18 02:37:01 INFO app[][o.s.a.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/opt/sonarqube/elasticsearch]: /opt/sonarqube/elasticsearch/bin/elasticsearch

getting this error can anyone help

@aawoyemi142
Copy link

aawoyemi142 commented Nov 19, 2022 via email

@itsecforu
Copy link

how to correct update tag of sonarqube image version ?

I edited compose.yml and started docker stack up -c docker-compose.yml sonarqube, but not works

@aawoyemi142
Copy link

aawoyemi142 commented Jan 10, 2023 via email

@kozla13
Copy link

kozla13 commented May 5, 2023

how to set the init admin username and password ?

@aawoyemi142
Copy link

aawoyemi142 commented May 5, 2023 via email

@kozla13
Copy link

kozla13 commented May 5, 2023

i want just to automate some stuff for local development

@Warchant
Copy link
Author

Warchant commented May 9, 2023 via email

@itsecforu
Copy link

itsecforu commented May 11, 2023

$ docker ps -q | xargs  docker stats --no-stream
$ k8s_database_harbor-harbor-database-0_harbor_62dc87ee-0df7-4359-9425-bf135bcc79a3_0  
                    910.31%  ```

@FlorianWolters
Copy link

FlorianWolters commented Jun 20, 2023

Just my 2 cents:

  1. The "official" docker-compose.yml usage example for Sonarqube with PostgreSQL can be found here.

  2. I was able to start SonarQube v10 + PostgreSQL v15.3 on a Windows 11 machine with the following docker-compose.yml file.

  3. Edit: The alternative for SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true seems to be the following:

    1. Create (or edit) the file %USERPROFILE%\.wslconfig.

    2. Add the following:

      [wsl2]
      kernelCommandLine="sysctl.vm.max_map_count=262144"
      

docker-compose.yml:

version: "3"

services:
  sonarqube:
    image: sonarqube:10.0.0-community
    hostname: sonarqube
    container_name: local-sonarqube
    depends_on:
      - db
    environment:
      - SONAR_JDBC_URL=jdbc:postgresql://db:5432/sonar
      - SONAR_JDBC_USERNAME=sonar
      - SONAR_JDBC_PASSWORD=sonar
      # The following (commented out) setting is a workaround for the following error:
      #
      #     max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
      #
      # It is better to add the following to the file `%USERPROFILE%\.wslconfig` on Windows instead:
      #
      #     [wsl2]
      #     kernelCommandLine="sysctl.vm.max_map_count=262144"
      #- SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    ports:
      - "9000:9000"
  db:
    image: postgres:15.3
    hostname: postgres
    container_name: local-postgres-for-sonarqube
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
      - POSTGRES_DB=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

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

@fabi6ferreira
Copy link

Thank you @Warchant . It works for me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment