Skip to content

Instantly share code, notes, and snippets.

@shichi-at-nttr
Last active May 22, 2024 11:49
Show Gist options
  • Save shichi-at-nttr/f7d2571a04bddf885eb2e3a415378c17 to your computer and use it in GitHub Desktop.
Save shichi-at-nttr/f7d2571a04bddf885eb2e3a415378c17 to your computer and use it in GitHub Desktop.
SonarQube on Docker (データを永続化して使う)

静的コード解析ツールSonarQubeをDockerで動かす

Dockerを終了しても解析結果とSonarQubeのプラグイン情報を保持するためのdocker-compose

SonarQubeの起動

docker-compose.yml を置いたディレクトリで以下を実行する。

$ docker-compose up -d

起動まで数分かかるのでゆっくり待つ。 心配であれば -d オプションを付けずに実行すると、起動状況が表示される。 (この場合は ^C で終了する)

SonarQube画面の表示

http://localhost:9000/

でアクセスする。 ログインは「 admin / admin

初回実行時に、 画面上部メニュー「Administration」 →「System」プルダウンから「Update Center」を選択 →「Available」 →Japanese Pack を Install →画面上部に戻って「Restart」 →確認ダイアログで「Restart」

で日本語化される。

コード解析の実行

コード解析自体は別のプログラム「sonar-scanner」により行い、 上でセットアップしたサーバに結果を送信する。 macOSの場合には、homebrewを使ってインストール可能。

$ brew install sonar-scanner

コード解析対象のディレクトリに sonar-project.properties ファイルを置き、 以下を実行する。

$ sonar-scanner

解析完了後に

http://localhost:9000/

で解析結果を確認する。

SonarQubeの終了

docker-compose にて -d オプション付きで起動した場合には、 docker-compose.yml を置いたディレクトリで以下を実行する。

$ docker-compose down

(-d なしで起動した場合には ^C で終了する)

その他

初回起動時に docker-compose.yml と同じディレクトリに作成される ./data/ 配下に永続化データが保存される。

  • ./data/mysql/* : MySQLデータ実体
  • ./data/plugin/* : SonarQubeのプラグイン(日本語化など)

日本語化した状態でも sonar-project.properties に記載したプロジェクト名が 日本語の場合には画面表示上は文字化けしてしまう。

version: '2'
services:
mysql-sonarqube:
container_name: mysql-sonarqube
image: mysql
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
volumes:
- ./data/mysql:/var/lib/mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=sonar
- MYSQL_USER=sonar
- MYSQL_PASSWORD=sonar
sonarqube:
container_name: sonarqube
image: sonarqube
links:
- mysql-sonarqube:mysql
volumes:
- ./data/plugin:/opt/sonarqube/extensions/plugins
ports:
- "9000:9000"
- "9092:9092"
environment:
- SONARQUBE_JDBC_USERNAME=sonar
- SONARQUBE_JDBC_PASSWORD=sonar
- "SONARQUBE_JDBC_URL=jdbc:mysql://mysql:3306/sonar?useUnicode=true&characterEncoding=utf8"
# must be unique in a given SonarQube instance
sonar.projectKey=(プロジェクトキー)
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=(プロジェクト名)
sonar.projectVersion=(バージョン番号/名称等)
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
sonar.exclusions=**/vendor/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment