本文将使用镜像安装的方式,使用Easypack项目中自定义简化了的alpine版Sonarqube镜像以及官方的MySQL5.7.18版本的镜像,同时提供docker-compose的方式直接使用Sonarqube.
项目 | 详细 |
---|---|
版本 | 5.6.5 |
Sonarqube可以使用内置H2数据库,无需使用其它,只需要Sonarqube的镜像即可。
docker run --rm --name sonarqube \
-p 9000:9000 -p 9092:9092 \
liumiaocn/sonarqube:5.6.5
[root@liumiaocn ~]# docker run --rm --name sonarqube \
> -p 9000:9000 -p 9092:9092 \
> liumiaocn/sonarqube:5.6.5
Unable to find image 'liumiaocn/sonarqube:5.6.5' locally
Trying to pull repository docker.io/liumiaocn/sonarqube ...
5.6.5: Pulling from docker.io/liumiaocn/sonarqube
b7f33cc0b48e: Already exists
43a564ae36a3: Already exists
b294f0e7874b: Already exists
4263c171a989: Pull complete
e1cff609e875: Pull complete
Digest: sha256:290b471df3ad0776be83dfea4d09a561fa5c43023432cabea7dae7329cc32888
Status: Downloaded newer image for docker.io/liumiaocn/sonarqube:5.6.5
......
Sonarqube主页 | http://192.168.32.131:9000/ |
---|
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bHfZddUo-1571539731317)(https://img-blog.csdn.net/20170707063007856?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1bWlhb2Nu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]
基于镜像方式的Sonarqube,我们需要Sonarqube的镜像和MySQL的镜像。
项目 | 版本 | 获取方式 |
---|---|---|
Sonarqube镜像 | 5.6.5 | docker pull liumiaocn/sonarqube:5.6.5 |
mysql镜像 | 5.7.18 | docker pull liumiaocn/mysql:5.7.18 |
关于MySQL的简单使用可以参看下面这篇文章以便迅速入门
项目 | http://blog.csdn.net/liumiaocn/article/details/56485588 |
---|
[root@liumiaocn ~]# docker pull liumiaocn/mysql:5.7.18
5.7.18: Pulling from liumiaocn/mysql
9f0706ba7422: Already exists
2290e155d2d0: Already exists
547981b8269f: Already exists
2c9d42ed2f48: Already exists
55e3122f1297: Already exists
abc10bd84060: Already exists
c0a5ce64f2b0: Already exists
c4595eab8e90: Already exists
098988cead35: Already exists
300ca5fa5eea: Already exists
43fdc4e3e690: Already exists
Digest: sha256:10f1b1348a120f81d161392bf9c9714f3c5366414037f8fc2fe67f39b819240e
Status: Downloaded newer image for liumiaocn/mysql:5.7.18
[root@liumiaocn ~]#
[root@liumiaocn ~]# docker pull liumiaocn/sonarqube:5.6.5
Trying to pull repository docker.io/liumiaocn/sonarqube ...
5.6.5: Pulling from docker.io/liumiaocn/sonarqube
Digest: sha256:290b471df3ad0776be83dfea4d09a561fa5c43023432cabea7dae7329cc32888
Status: Image is up to date for docker.io/liumiaocn/sonarqube:5.6.5
[root@liumiaocn ~]#
因为前面确认的时候已经取过了,所以提示Image is up to date for docker.io/liumiaocn/sonarqube:5.6.5,说明本地已经是最新的镜像了。
docker-compose的安装非常简单,有两种方式,可以参照:
项目 | http://blog.csdn.net/liumiaocn/article/details/52148274 |
---|
由于事前请创建如下目录
[root@liumiaocn ~]# mkdir -p home/local/mysql/data/ /home/local/mysql/conf.d/ /home/local/sonar/data/ /home/local/sonar/extensions/ /home/local/sonar/conf/
[root@liumiaocn ~]#
docker-compose内容如下
[root@liumiaocn sonar]# cat docker-compose.yml
version: '2'
services:
# database service: mysql
mysql:
image: liumiaocn/mysql:5.7.18
ports:
- "3306:3306"
volumes:
- /home/local/mysql/data/:/var/lib/mysql
- /home/local/mysql/conf.d/:/etc/mysql/conf.d
environment:
- MYSQL_ROOT_PASSWORD=hello123
- MYSQL_DATABASE=sonarqube
restart: "no"
# Security service: sonarqube
sonarqube:
image: liumiaocn/sonarqube:5.6.5
ports:
- "9000:9000"
volumes:
- /home/local/sonar/data/:/usr/share/sonarqube/data
- /home/local/sonar/extensions/:/usr/share/sonarqube/extensions
- /home/local/sonar/conf/:/usr/share/sonarqube/conf
environment:
- SONARQUBE_JDBC_USERNAME=root
- SONARQUBE_JDBC_PASSWORD=hello123
- SONARQUBE_JDBC_URL=jdbc:mysql://mysql:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
links:
- mysql:mysql
depends_on:
- mysql
restart: "no"
[root@liumiaocn sonar]#
使用docker-compose方式启动只需要一条命令即可,当然前提是docker-compose已经安装完毕,在docker-compose还没有跟docker合体之前,目前需要分别安装,可以参看如下文章进行安装:
文章 | 链接 |
---|---|
docker-compose的安装和设定 | http://blog.csdn.net/liumiaocn/article/details/52148274 |
一般的做法是:安装设定mysql->创建sonarqube数据库->安装设定sonarqube并做好数据库的关联->启动sonarqube,为了简化对应,现在只需要启动sonarqube之后再重新启动一下即可。
使用docker-compose启动sonar服务
[root@liumiaocn sonar]# docker-compose up -d
Creating network "sonar_default" with the default driver
Creating sonar_mysql_1 ...
Creating sonar_mysql_1 ... done
Creating sonar_sonarqube_1 ...
Creating sonar_sonarqube_1 ... done
[root@liumiaocn sonar]#
docker-compose restart
细心的用户会发现下面提示的"Embedded database would be used…"的信息没有了,那是因为实际的数据已经在mysql里面保存了。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9faB3r6f-1571539731318)(https://img-blog.csdn.net/20170707065551419?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1bWlhb2Nu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]
通过确认,发现已经在mysql中创建了sonarqube相关的数据库和表了。数据库是在mysql启动的时候创建的,而表是在sonarqube容器启动与之链接后在sonarqube初期化的时候创建的,二者都存在说明操作已经成功完成。
[root@liumiaocn sonar]# docker exec -it sonar_mysql_1 sh
# mysql -uroot -phello123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sonarqube |
| sys |
+--------------------+
5 rows in set (0.02 sec)
mysql> use sonarqube
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_sonarqube |
+---------------------------+
| active_dashboards |
| active_rule_parameters |
| active_rules |
| activities |
| authors |
| ce_activity |
| ce_queue |
| dashboards |
| duplications_index |
| events |
| file_sources |
| group_roles |
| groups |
| groups_users |
| issue_changes |
| issue_filter_favourites |
| issue_filters |
| issues |
| loaded_templates |
| manual_measures |
| measure_filter_favourites |
| measure_filters |
| metrics |
| notifications |
| perm_templates_groups |
| perm_templates_users |
| permission_templates |
| project_links |
| project_measures |
| project_qprofiles |
| projects |
| properties |
| quality_gate_conditions |
| quality_gates |
| resource_index |
| rules |
| rules_parameters |
| rules_profiles |
| schema_migrations |
| snapshots |
| user_roles |
| user_tokens |
| users |
| widget_properties |
| widgets |
+---------------------------+
45 rows in set (0.00 sec)
mysql>
Easypack只是将官方的镜像进行整理不重新作新的东西,主要是为了标准化推广官方镜像,这样后期的升级也会非常顺畅,而且由于使用方式比较标准,出现问题时从社区也更容易得到帮助和反馈, You are not alone. Sonarqube在DevOps的实践中像Jenkins那样起到一个非常重要的作用,对代码进行扫描,检查代码的"七宗罪",再结合其他工具,合理利用,能给质量控制方面起到很好的作用。