AdSense

網頁

2024/2/16

Docker 安裝SonarQube Community Edition

Docker安裝SonarQube Community Edition(社群版/免費版)的方式如下。


安裝環境

  • macOS Ventura 13.0.1
  • Docker version 24.0.2


安裝

這邊使用Docker Compose安裝,docker-compose.yml內容如下:

docker-compose.yml

version: "3"

services:
  sonarqube:
    image: sonarqube:9-community
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    ports:
      - "9000:9000"
  db:
    image: postgres:14
    container_name: sonar-postgres
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
    volumes:
      - sonar_postgresql:/var/lib/postgresql
      - sonar_postgresql_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  sonar_postgresql:
  sonar_postgresql_data:

說明:

  •     image: sonarqube:9-community
    安裝的SonarQube image,版本為9-community
  •     depends_on:
          - db
    sonarqube依賴下面的db服務。
  •     environment:
          SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
          SONAR_JDBC_USERNAME: sonar
          SONAR_JDBC_PASSWORD: sonar
    SonarQube使用的資料庫,即下面db的連線資訊。
  •     volumes:
          - sonarqube_data:/opt/sonarqube/data
          - sonarqube_extensions:/opt/sonarqube/extensions
          - sonarqube_logs:/opt/sonarqube/logs
    設定SonqrQube容器的volume路徑。
  •     ports:
          - "9000:9000"
    SonqrQube容器的port對映到主機的9000。
  •   db:
        image: postgres:14
        container_name: sonar-postgres
    SonarQube所使用的資料庫服務,使用postgres:14 image建立PostgreSQL容器;容器名稱為sonar-postgres
  • volumes:
      sonarqube_data:
      sonarqube_extensions:
      sonarqube_logs:
      sonar_postgresql:
      sonar_postgresql_data:

    設定SonqrQube和PorgreSQL容器的volumes。


接著在docker-compose.yml所在目錄執行docker-compose up -d建立容器。

% docker-compose up -d
[+] Running 24/24
 ✔ db 14 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                                                                                                                                                                                   20.0s
   ✔ f546e941f15b Pull complete                                                                                                                                                                                                                                            5.5s
   ✔ 926c64b890ad Pull complete                                                                                                                                                                                                                                            5.5s
   ✔ eca757527cc4 Pull complete                                                                                                                                                                                                                                            5.6s
   ✔ cffbe79d4ccf Pull complete                                                                                                                                                                                                                                            5.6s
   ✔ 948961f25f1c Pull complete                                                                                                                                                                                                                                            5.9s
   ✔ de342bb98309 Pull complete                                                                                                                                                                                                                                            6.0s
   ✔ 3793b15c294c Pull complete                                                                                                                                                                                                                                            6.0s
   ✔ 75dcc1faad83 Pull complete                                                                                                                                                                                                                                            6.0s
   ✔ 7cf615f30719 Pull complete                                                                                                                                                                                                                                           13.1s
   ✔ e4e30a075b47 Pull complete                                                                                                                                                                                                                                           13.2s
   ✔ 88d16b638ec5 Pull complete                                                                                                                                                                                                                                           13.2s
   ✔ 421e3f2047fd Pull complete                                                                                                                                                                                                                                           13.2s
   ✔ ac9e8ed60049 Pull complete                                                                                                                                                                                                                                           13.2s
   ✔ 3d48d57e0e87 Pull complete                                                                                                                                                                                                                                           13.2s
 ✔ sonarqube 8 layers [⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                                                                                                                                                                                   34.2s
   ✔ b90a30ba7a05 Pull complete                                                                                                                                                                                                                                           11.1s
   ✔ 4d031894e4c0 Pull complete                                                                                                                                                                                                                                           12.6s
   ✔ 8a0007a9ef9b Pull complete                                                                                                                                                                                                                                           14.1s
   ✔ 917c6cc534cf Pull complete                                                                                                                                                                                                                                           14.1s
   ✔ 0d7a0f038262 Pull complete                                                                                                                                                                                                                                           14.2s
   ✔ e1a9adfdcdb9 Pull complete                                                                                                                                                                                                                                           26.8s
   ✔ fde640df4e7a Pull complete                                                                                                                                                                                                                                           26.8s
   ✔ 4f4fb700ef54 Pull complete                                                                                                                                                                                                                                           26.9s
[+] Running 8/8
 ✔ Network sonarqube_default                 Created                                                                                                                                                                                                                       0.0s
 ✔ Volume "sonarqube_sonarqube_logs"         Created                                                                                                                                                                                                                       0.0s
 ✔ Volume "sonarqube_sonar_postgresql"       Created                                                                                                                                                                                                                       0.0s
 ✔ Volume "sonarqube_sonar_postgresql_data"  Created                                                                                                                                                                                                                       0.0s
 ✔ Volume "sonarqube_sonarqube_data"         Created                                                                                                                                                                                                                       0.0s
 ✔ Volume "sonarqube_sonarqube_extensions"   Created                                                                                                                                                                                                                       0.0s
 ✔ Container sonar-postgres                  Started                                                                                                                                                                                                                       0.7s
 ✔ Container sonarqube-sonarqube-1           Started

執行完後輸入docker ps檢視運行的SonqrQube和PostgreSQL容器。

% docker ps
CONTAINER ID   IMAGE                   COMMAND                   CREATED          STATUS          PORTS                    NAMES
b13110599dd8   sonarqube:9-community   "/opt/sonarqube/dock…"   44 seconds ago   Up 43 seconds   0.0.0.0:9000->9000/tcp   sonarqube-sonarqube-1
32520ee235eb   postgres:14             "docker-entrypoint.s…"   44 seconds ago   Up 43 seconds   5432/tcp                 sonar-postgres


登入SonqrQube

在瀏覽器輸入http://localhost:9000進入SonarQube的登入頁面,預設帳密為admin/admin。



登入後修改密碼,例如12345



SonarQube首頁。




停止

docker-compose.yml所在目錄執行docker-compose stop即可停止SonarQube即資料庫容器。

% docker-compose stop
[+] Stopping 2/2
  Container sonarqube-sonarqube-1  Stopped                                                                                                                                                                                                                                2.0s
  Container sonar-postgres         Stopped


解除安裝

docker-compose.yml所在目錄執行docker-compose down即可移除SonarQube即資料庫容器。


沒有留言:

AdSense