使用docker-compose快速搭建环境: SonarQube 7.9.1 + PostgreSQL 12.0

SonarQube的LTS版本以6.7和7.9较为具有代表性,这篇文章整理了一下SonarQube LTS 7.9.1 + PostGreSQL的环境搭建方式。需要注意的是从7.9版本开始 SonarQube在所有的发行版中不再支持MySQL,另外需要Java 11的支持。

SonarQube 7.9.1

这里使用Alpine版本的SonarQube 7.9.1 和PostgreSQL 12.0进行环境搭建,docker-compose.yml如下所示

docker-compose.yml文件: 示例1

liumiaocn:sonar liumiao$ cat docker-compose.7.9.1.yml 
version: '2'

services:
  # database service: postgres
  postgres:
    image: postgres:12-alpine
    networks:
      - sonarnet
    volumes:
      - ./postgres/postgresql/:/var/lib/postgresql
      - ./postgres/data/:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    restart: "no"

    
  # Security service: sonarqube
  sonarqube:
    image: liumiaocn/sonarqube:7.9.1
    ports:
      - "9000:9000"
    networks:
      - sonarnet
    volumes:
      - ./sonar/data/:/opt/sonarqube/data
      - ./sonar/log/:/opt/sonarqube/log
      - ./sonar/extensions/:/opt/sonarqube/extensions
      - ./sonar/conf/:/opt/sonarqube/conf
    environment:
      - sonar.jdbc.url=jdbc:postgresql://postgres:5432/sonar
    restart: "no"

networks:
  sonarnet:
    driver: bridge
liumiaocn:sonar liumiao$ 

docker-compose.yml文件: 示例2

liumiaocn:sonar liumiao$ cat docker-compose.7.9.1.yml
version: '2'

services:
  # database service: postgres
  postgres:
    image: postgres:12-alpine
    volumes:
      - ./postgres/postgresql/:/var/lib/postgresql
      - ./postgres/data/:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    restart: "no"

    
  # Security service: sonarqube
  sonarqube:
    image: liumiaocn/sonarqube:7.9.1
    ports:
      - "9000:9000"
    volumes:
      - ./sonar/data/:/opt/sonarqube/data
      - ./sonar/log/:/opt/sonarqube/log
      - ./sonar/extensions/:/opt/sonarqube/extensions
      - ./sonar/conf/:/opt/sonarqube/conf
    environment:
      - sonar.jdbc.url=jdbc:postgresql://postgres:5432/sonar
    restart: "no"
liumiaocn:sonar liumiao$ 

启动服务

liumiaocn:sonar liumiao$ docker-compose -f docker-compose.7.9.1.yml up -d
Creating network "sonar_default" with the default driver
Creating sonar_sonarqube_1 ... done
Creating sonar_postgres_1  ... done
liumiaocn:sonar liumiao$ 

结果确认

docker容器启动之后,可以使用docker-compose ps命令确认服务运行状态

liumiaocn:sonar liumiao$ docker-compose -f docker-compose.7.9.1.yml ps
      Name                     Command              State           Ports         
----------------------------------------------------------------------------------
sonar_postgres_1    docker-entrypoint.sh postgres   Up      5432/tcp              
sonar_sonarqube_1   ./bin/run.sh                    Up      0.0.0.0:9000->9000/tcp
liumiaocn:sonar liumiao$ 

使用docker-compose快速搭建环境: SonarQube 7.9.1 + PostgreSQL 12.0_第1张图片
使用docker-compose快速搭建环境: SonarQube 7.9.1 + PostgreSQL 12.0_第2张图片

执行示例

这里以Angular的demo应用为例,使用Sonar-Scanner进行扫描,此处使用的Sonar-Scanner的版本为4.2,Java版本为 JDK8,可以看到已经提示在SonarQube 8之后Sonar-Scanner也将会要求使用Java 11 或者以上版本。

执行日志示例

/data/jenkins/workspace/angular-pipeline-job/demo # /usr/local/share/sonar/bin/sonar-scanner '-Dsonar.projectKey=demo' '-Dsonar.sources=
src' '-Dsonar.host.url=http://192.168.31.242:9000' '-Dsonar.login=admin' '-Dsonar.password=admin'
INFO: Scanner configuration file: /usr/local/share/sonar/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 4.2.0.1873
INFO: Java 1.8.0_212 IcedTea (64-bit)
INFO: Linux 4.9.87-linuxkit-aufs amd64
INFO: User cache: /root/.sonar/cache
INFO: SonarQube server 7.9.1
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
WARN: SonarScanner will require Java 11+ to run starting in SonarQube 8.x
INFO: Load global settings
INFO: Load global settings (done) | time=687ms
INFO: Server id: 46AF5D23-AW3zxFXCfmhe-zhloQSg
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=449ms
INFO: Load/download plugins (done) | time=1523ms
INFO: Process project properties
INFO: Project key: demo
INFO: Base dir: /data/jenkins/workspace/angular-pipeline-job/demo
INFO: Working dir: /data/jenkins/workspace/angular-pipeline-job/demo/.scannerwork
INFO: Load project settings for component key: 'demo'
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=840ms
INFO: Load active rules
INFO: Load active rules (done) | time=1773ms
WARN: SCM provider autodetection failed. Please use "sonar.scm.provider" to define SCM of your project, or disable the SCM Sensor in the project settings.
INFO: Indexing files...
INFO: Project configuration:
INFO: 13 files indexed
INFO: Quality profile for ts: Sonar way
INFO: ------------- Run sensors on module demo
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=389ms
INFO: Sensor SonarTS [typescript]
INFO: Load project repositories
INFO: Load project repositories (done) | time=332ms
INFO: Analyzing 8 typescript file(s) with the following configuration file /data/jenkins/workspace/angular-pipeline-job/demo/tsconfig.json
INFO: 8 files analyzed out of 8
INFO: Sensor SonarTS [typescript] (done) | time=14007ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=49ms
INFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.
INFO: 5 files had no CPD blocks
INFO: Calculating CPD for 3 files
INFO: CPD calculation finished
INFO: Analysis report generated in 183ms, dir size=20 KB
INFO: Analysis report compressed in 1412ms, zip size=13 KB
INFO: Analysis report uploaded in 2278ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.31.242:9000/dashboard?id=demo
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://192.168.31.242:9000/api/ce/task?id=AW31c1LgGJZg-xbI0uFy
INFO: Analysis total time: 23.985 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 28.369s
INFO: Final Memory: 9M/63M
INFO: ------------------------------------------------------------------------
/data/jenkins/workspace/angular-pipeline-job/demo #

执行结果示例

使用docker-compose快速搭建环境: SonarQube 7.9.1 + PostgreSQL 12.0_第3张图片

你可能感兴趣的:(sonarqube)