Jenkins (四) - 搭建 Docker SonarQube
拉取 SonarQube
$ docker pull sonarqube
拉取 postgres
$ $ docker pull postgres
运行 postgres
$ docker run -itd \
-e TZ=Asia/Shanghai
-e POSTGRES_USER=tester \
-e POSTGRES_PASSWORD=123456 \
-p 5432:5432 \
-v /home/tester/data/docker/postgresql/data:/var/lib/postgresql/data \
--name postgres postgres
运行 SonarQube
SonarQube 默认使用h2
数据库,通过配置 SONAR_JDBC_URL
, SONAR_JDBC_PASSWORD
, SONAR_JDBC_USERNAME
替换为postgres.
$ docker run -d \
--name sonarqube \
-e TZ=Asia/Shanghai \
-e SONAR_JDBC_USERNAME=tester \
-e SONAR_JDBC_PASSWORD=123456 \
-e SONAR_JDBC_URL=jdbc:postgresql://192.168.56.102:5432/sonarqube \
-v /home/tester/data/docker/sonarqube/extensions:/opt/sonarqube/extensions \
-v /home/tester/data/docker/sonarqube/logs:/opt/sonarqube/logs \
-v /home/tester/data/docker/sonarqube/data:/opt/sonarqube/data \
-v /home/tester/data/docker/sonarqube/conf:/opt/sonarqube/conf \
-p 9000:9000 \
--name sonarqube sonarqube
查看sonarqube是否已运行成功
$ docker ps
启动可能会出错,出错查看文末
启动失败
验证postgres连接
登入SonarQube
http://192.168.56.102:9000
默认账号密码: admin/admin
更改密码为 123456
启动失败
第一次启动一般都会失败,可以通过查看web.log
, es.log
分析原因。
$ cd /home/tester/data/docker/sonarqube/logs
$ ls -l
web.log
2024.04.04 01:21:42 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on [HTTP: 127.0.0.1:9001, TCP: 127.0.0.1:37975]
2024.04.04 01:21:42 INFO app[][o.s.a.ProcessLauncherImpl] Launch process[ELASTICSEARCH] from [/opt/sonarqube/elasticsearch]: /opt/java/openjdk/bin/java -Xms4m -Xmx64m -XX:+UseSerialGC -Dcli.name=server -Dcli.script=./bin/elasticsearch -Dcli.libs=lib/tools/server-cli -Des.path.home=/opt/sonarqube/elasticsearch -Des.path.conf=/opt/sonarqube/temp/conf/es -Des.distribution.type=tar -cp /opt/sonarqube/elasticsearch/lib/*:/opt/sonarqube/elasticsearch/lib/cli-launcher/* org.elasticsearch.launcher.CliToolLauncher
2024.04.04 01:21:42 INFO app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
2024.04.04 01:21:50 WARN app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [ElasticSearch]: 78
2024.04.04 01:21:50 INFO app[][o.s.a.SchedulerImpl] Process[ElasticSearch] is stopped
2024.04.04 01:21:50 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped
如果遇到es启动失败,多半是由于vm.max_map_count 过小。记得查看es.log,通常es.log会有对应的错误提示。
es.log
2024.04.04 01:21:50 INFO es[][o.e.t.TransportService] publish_address {127.0.0.1:37975}, bound_addresses {127.0.0.1:37975}
2024.04.04 01:21:50 INFO es[][o.e.b.BootstrapChecks] explicitly enforcing bootstrap checks
2024.04.04 01:21:50 ERROR es[][o.e.b.Elasticsearch] node validation exception
[1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch. For more information see [https://www.elastic.co/guide/en/elasticsearch/reference/8.11/bootstrap-checks.html]
bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]; for more information see [https://www.elastic.co/guide/en/elasticsearch/reference/8.11/_maximum_map_count_check.html]
2024.04.04 01:21:50 WARN es[][o.e.n.Node] unexpected exception while waiting for http server to close
java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Can't move to stopped state when not started
at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:?]
at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:?]
at org.elasticsearch.node.Node.prepareForClose(Node.java:1776) ~[elasticsearch-8.11.0.jar:?]
at org.elasticsearch.bootstrap.Elasticsearch.shutdown(Elasticsearch.java:468) ~[elasticsearch-8.11.0.jar:?]
at java.lang.Thread.run(Unknown Source) ~[?:?]
Caused by: java.lang.IllegalStateException: Can't move to stopped state when not started
at org.elasticsearch.common.component.Lifecycle.canMoveToStopped(Lifecycle.java:128) ~[elasticsearch-8.11.0.jar:?]
at org.elasticsearch.common.component.AbstractLifecycleComponent.stop(AbstractLifecycleComponent.java:73) ~[elasticsearch-8.11.0.jar:?]
at org.elasticsearch.node.Node.lambda$prepareForClose$59(Node.java:1768) ~[elasticsearch-8.11.0.jar:?]
at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:?]
解决方案
- 临时修改
# 查看vm.max_map_count
$ sudo sysctl -a | grep vm.max_map_count
vm.max_map_count = 65530
# 更新vm.max_map_count
$ sudo sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144
- 永久修改
$ vim /etc/sysctl.conf
Reference: 来自官方说明
Continuing from the previous point, to use mmap effectively, Elasticsearch also requires the ability to create many memory-mapped areas. The maximum map count check checks that the kernel allows a process to have at least 262,144 memory-mapped areas and is enforced on Linux only. To pass the maximum map count check, you must configure vm.max_map_count via sysctl to be at least 262144.