经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Docker » 查看文章
使用Docker安装SonarQube的详细教程
来源:jb51  时间:2021/10/11 17:56:39  对本文有异议

Docker安装SonarQube的教程如下所示:

1.拉取镜像

1.1拉取相关镜像并运行

1.1.1拉取相关镜像

  1. # 拉取sonarqube镜像
  2. $ docker pull sonarqube:9.1.0-community (推荐使用) / $ docker pull sonarqube:7.6-community
  3. # 拉取postgres镜像
  4. $ docker pull postgres:9.6.23

1.1.2运行镜像

  1. # 运行postgres数据库
  2. $ docker run --name postgresqldb --restart=always -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=123456 -d postgres:9.6.23
  3.  
  4. # 进入postgres容器,创建用户名和密码
  5. $ docker exec -it postgresqldb bash
  6.  
  7. # 登录数据库
  8. psql -U root -W
  9. # 创建用户名和密码
  10. create user sonar with password 'sonar';
  11. create database sonar owner sonar;
  12. grant all privileges on database sonar to sonar;
  13.  
  14. # 不连接postgres数据库运行命令(不推荐)
  15. docker run --name sonarqube --restart=always -p 9000:9000 -d naumy/hitrend-sonarqube:v1.0
  16.  
  17. # 运行sonarqube容器
  18. docker run -d --name sonarqube --restart=always -p 9000:9000 -e sonar.jdbc.username=sonar -e sonar.jdbc.password=sonar -e sonar.jdbc.url=jdbc:postgresql://139.198.176.140:5432/sonar sonarqube:9.1.0-community

接着访问:http://localhost:9000/ 就可以了,默认管理员用户和密码为:admin/admin

image-20211008222658468

嵌入式数据库应仅用于评估目的、嵌入式数据库无法扩展,不支持升级到SonarQube的较新版本,也不支持将数据从中迁移到其他数据库引擎。

1.2保存并提交已修改的镜像

  1. # 保存已经修的镜像
  2. docker commit -a "naumy" -m "安装中文插件" 19f1cc24dc98 hitrend-sonarqube:v1.0
  3. # 把旧镜像的名字,改成仓库要求的新版名字
  4. docker tag hitrend-sonarqube:v1.0 naumy/hitrend-sonarqube:v1.0
  5. # 登录到docker hub
  6. docker login
  7. # 推送
  8. docker push naumy/hitrend-sonarqube:v1.0

image-20211005120955961

2.安装成功

image-20211003205325696 

3.插件安装

3.1安装Chinese插件

SonarQube提供了强大的插件管理功能,以中文语言包为示例,讲解如何安装插件:

登录成功后,选择Administration-Marketplace-Plugins,在搜索框输入Chinese就可以选择安装了。

image-20211003205550266

当状态显示为Install Pending时,说明插件安装完成,点击Restart Server即可生效。

之后,就显示为中文了。

同时安装findbug插件

image-20211003205917633

4.docker安装gitlab

4.1.Gitlab镜像拉取

  1. # gitlab-ce为稳定版本,后面不填写版本则默认pull最新latest版本
  2. $ docker pull gitlab/gitlab-ce

4.2运行gitlab镜像

  1. $ docker run -d -p 443:443 -p 80:80 -p 222:22 --name gitlab --restart always -v /home/gitlab/config:/etc/gitlab -v /home/gitlab/logs:/var/log/gitlab -v /home/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce
  2.  
  3. # -d:后台运行
  4. # -p:将容器内部端口向外映射
  5. # --name:命名容器名称
  6. # -v:将容器内数据文件夹或者日志、配置等文件夹挂载到宿主机指定目录

按上面的方式,gitlab容器运行没问题,但在gitlab上创建项目的时候,生成项目的URL访问地址是按容器的hostname来生成的,也就是容器的id。

作为gitlab服务器,我们需要一个固定的URL访问地址,于是需要配置gitlab.rb(宿主机路径:/home/gitlab/config/gitlab.rb)。

  1. # gitlab.rb文件内容默认全是注释
  2. $ vim /home/gitlab/config/gitlab.rb
  1. # 配置http协议所使用的访问地址,不加端口号默认为80
  2. external_url 'http://192.168.199.231'
  3.  
  4. # 配置ssh协议所使用的访问地址和端口
  5. gitlab_rails['gitlab_ssh_host'] = '192.168.199.231'
  6. gitlab_rails['gitlab_shell_ssh_port'] = 222 # 此端口是run时22端口映射的222端口
  7. :wq #保存配置文件并退出

image-20211006212109889

  1. # 重启gitlab容器
  2. $ docker restart gitlab

此时项目的仓库地址就变了。如果ssh端口地址不是默认的22,就会加上ssh:// 协议头
打开浏览器输入ip地址(因为我的gitlab端口为80,所以浏览器url不用输入端口号,如果端口号不是80,则打开为:ip:端口号)

4.3设置root用户名和密码

进入目录 /home/gitlab/config/initial_root_password,查看密码

xwCsS7lMYx+8x3o6KIBw+Ia6Lg3VqvtHLzxzYfPNtxk=

或者进入gitlab容器后修改密码。

  1. root@ba96cb6a1f47:/# gitlab-rails console
  2. --------------------------------------------------------------------------------
  3. Ruby: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux]
  4. GitLab: 14.3.2 (92acfb1b8a9) FOSS
  5. GitLab Shell: 13.21.1
  6. PostgreSQL: 12.7
  7. --------------------------------------------------------------------------------
  8.  
  9. irb(main):005:0> user = User.where(id: 1).first
  10. => #<User id:1 @root>
  11. irb(main):006:0> user.password=12345678
  12. => 12345678
  13. irb(main):007:0> user.password_confirmation=12345678
  14. => 12345678
  15. irb(main):008:0> user.save!
  16. Enqueued ActionMailer::MailDeliveryJob (Job ID: 4fc2d685-2fd6-41d9-893e-2dabc7c3b366) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", {:args=>[#<GlobalID:0x00007fc6c59b5b48 @uri=#<URI::GID gid://gitlab/User/1>>]}
  17. => true
  18. irb(main):009:0> quit

image-20211006113044081

运行后的效果图

4.4保存镜像并推送dockerhub

  1. # 保存已经修的镜像
  2. docker commit -a "naumy" -m "初始化gitlab" ba96cb6a1f47 gitlab:v1.0
  3. docker commit -a "naumy" -m "sonarqube:7.6-community " e70c6cbe2e0b sonarqube-7.6-community:v1.0
  4. docker tag sonarqube-7.6-community:v1.0 naumy/sonarqube-7.6-community:v1.0
  5. docker push naumy/sonarqube-7.6-community:v1.0
  6. # 把旧镜像的名字,改成仓库要求的新版名字
  7. docker tag gitlab:v1.0 naumy/gitlab:v1.0
  8. # 登录到docker hub
  9. docker login
  10. # 推送
  11. docker push naumy/gitlab:v1.0

5.碰到的问题

5.1虚拟内存不够

启动容器后过了十几秒。容器自动退出。

Error: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

  1. 运行容器后,容器马上退出。
  2.  
  3. # 使用命令查看运行日志
  4. docker logs 容器名称/容器ID

image-20211005110639500

在/etc/sysctl.conf文件最后添加一行

  1. vm.max_map_count=262144

image-20211005110922585

执行/sbin/sysctl -p立即生效

image-20211005111046958

6.整合Sonar和gitlab

6.1安装Gitlab-runner

6.1.1获取gitlab-Token

进入gitlab后,选择runner,进行相应的Token获取。

image-20211008170417198

6.1.2安装gitlab-runner

  1. # 拉取镜像
  2. docker pull gitlab/gitlab-runner:v13.2.4
  3.  
  4. # 创建容器映射目录
  5. mkdir -p /dwz/docker-volume/gitlab-runner/config
  6.  
  7. # 创建容器并运行
  8. docker run -d --name gitlab-runner --restart always -v /dwz/docker-volume/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:v13.2.4

进入gitlab-runner容器后,配置相应的参数设置:

  1. docker exec -it gitlab-runner gitlab-runner register -n --url http://139.198.166.208 --registration-token 9zEbBYXSyqJqpNb9QSNh --executor docker --description "Docker Runner" --docker-image "sonarsource/sonar-scanner-cli:latest" --docker-volumes /var/run/docker.sock:/var/run/docker.sock

再次加载gitlab页面,出现runner配置项。

image-20211008170819704

6.2设置sonarqube的用户名和密码

设置当前的sonarqube的用户面和密码为admin和123456

6.3进行项目分析(手动添加项目)

image-20211006200645346

是否需要集成自己喜欢的CI,使用gitlab进行持续集成和持续部署。

image-20211009183044360

第一步 选择需要检测项目代码类型:

image-20211009183751728

新建配置文件sonar-project.properties:

  1. sonar.projectKey=gitlab-sonorqube
  2. sonar.qualitygate.wait=true
  3. sonar.language=py

image-20211009183240599

第二步:添加环境变量

image-20211009183832036

image-20211006202645805

令牌密钥:b23fe46d142fcfb052b05d5b3fd6fc823df0b682

按照要求添加相应的环境变量:

image-20211009163010867

6.4进行CI/CD(sonar与gitlab)

6.4.1版本为sonarqube-7.6-community

创建一个gitlab项目,实验使用的项目为python项目。

image-20211008171646440

.gitlab-ci.yml文件内容为

  1. # This file is a template, and might need editing before it works on your project.
  2. # To contribute improvements to CI/CD templates, please follow the Development guide at:
  3. # https://docs.gitlab.com/ee/development/cicd/templates.html
  4. # This specific template is located at:
  5. # https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml
  6.  
  7. # This is a sample GitLab CI/CD configuration file that should run without any modifications.
  8. # It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
  9. # it uses echo commands to simulate the pipeline execution.
  10. #
  11. # A pipeline is composed of independent jobs that run scripts, grouped into stages.
  12. # Stages run in sequential order, but jobs within stages run in parallel.
  13. #
  14. # For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
  15.  
  16. stages: # List of stages for jobs, and their order of execution
  17. - build
  18. - test
  19. - deploy
  20.  
  21. build-job: # This job runs in the build stage, which runs first.
  22. stage: build
  23. script:
  24. - echo "Compiling the code..."
  25. - echo "Compile complete."
  26.  
  27. unit-test-job: # This job runs in the test stage.
  28. stage: test # It only starts when the job in the build stage completes successfully.
  29. script:
  30. - echo "Running unit tests... This will take about 60 seconds."
  31. - sleep 60
  32. - echo "Code coverage is 90%"
  33.  
  34. lint-test-job: # This job also runs in the test stage.
  35. stage: test # It can run at the same time as unit-test-job (in parallel).
  36. script:
  37. - echo "Linting code... This will take about 10 seconds."
  38. - sleep 10
  39. - echo "No lint issues found."
  40.  
  41. deploy-job: # This job runs in the deploy stage.
  42. stage: deploy # It only runs when *both* jobs in the test stage complete successfully.
  43. script:
  44. - echo "Deploying application..."
  45. - echo "Application successfully deployed."
  46. image:
  47. name: sonarsource/sonar-scanner-cli:latest
  48. entrypoint: [""]
  49.  
  50. sonarqube-check:
  51. script:
  52. - sonar-scanner -X -Dsonar.projectKey=gitlab-sonorqube -Dsonar.host.url=http://139.198.176.140:9000 -Dsonar.login=cbd26f998beeb61d7a991e0282efc430b020d9f1 -Dsonar.login=admin -Dsonar.password=admin -Dsonar.language=py -Dsonar.java.binaries=build/ -Dsonar.projectVersion=1.0 -Dsonar.sources=.
  53. allow_failure: true
  54. only:
  55. - main # or the name of your main branch

提交代码后,可以获取到相应的测试信息。

https://sm.ms/image/ykYPlDgZVvuhzsq

6.4.2版本为sonarqube-9.1-community

.gitlab-ci.yml文件内容为

  1. sonarqube-check:
  2. image:
  3. name: sonarsource/sonar-scanner-cli:latest
  4. entrypoint: [""]
  5. variables:
  6. SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
  7. GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
  8. cache:
  9. key: "${CI_JOB_NAME}"
  10. paths:
  11. - .sonar/cache
  12. script:
  13. - sonar-scanner -X -Dsonar.projectKey=gitlab-sonorqube -Dsonar.host.url=http://139.198.176.140:9000 -Dsonar.login=7f9e3408ac11e0699e2f8afdb21a662cc8ab2698 -Dsonar.login=admin -Dsonar.password=123456 -Dsonar.language=py -Dsonar.java.binaries=build/ -Dsonar.projectVersion=1.0 -Dsonar.sources=.
  14. allow_failure: true
  15. only:
  16. - main # or the name of your main branch

提交代码后gitlab会自动进行CI/CD:

image-20211009174921078

点入进去后查看相应的状态和内容是否符合需求:

image-20211009174902725

运行完成后,将看到对应的测试分析结果:

image-20211009163521482

6.5在整合过程中碰到的问题

配置文件写错:

使用的python代码,所以后续将使用py作为语言选择。

image-20211009190159197

image-20211009190324553

7.总结

当前使用的工具有:

sonarqube:9.1.0-community 、gitlab/gitlab-runner:v13.2.4 、postgres:9.6.23 、gitlab/gitlab-ce、sonarsource/sonar-scanner-cli:latest

image-20211009163631709

开发人员提交代码到gitlab仓库后,触发master分支自动合并任务,并进行代码扫描(可改成其他测试分支),扫面结果返回到sonarqube平台。

image-20211009175746382

到此这篇关于Docker安装SonarQube的文章就介绍到这了,更多相关Docker安装SonarQube内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号