使用Docker安裝SonarQube的詳細(xì)教程
Docker安裝SonarQube的教程如下所示:
1.拉取鏡像
1.1拉取相關(guān)鏡像并運(yùn)行
1.1.1拉取相關(guān)鏡像
# 拉取sonarqube鏡像 $ docker pull sonarqube:9.1.0-community (推薦使用) / $ docker pull sonarqube:7.6-community # 拉取postgres鏡像 $ docker pull postgres:9.6.23
1.1.2運(yùn)行鏡像
# 運(yùn)行postgres數(shù)據(jù)庫 $ docker run --name postgresqldb --restart=always -p 5432:5432 \ -e POSTGRES_USER=root \ -e POSTGRES_PASSWORD=123456 \ -d postgres:9.6.23 # 進(jìn)入postgres容器,創(chuàng)建用戶名和密碼 $ docker exec -it postgresqldb bash # 登錄數(shù)據(jù)庫 psql -U root -W # 創(chuàng)建用戶名和密碼 create user sonar with password 'sonar'; create database sonar owner sonar; grant all privileges on database sonar to sonar; # 不連接postgres數(shù)據(jù)庫運(yùn)行命令(不推薦) docker run --name sonarqube --restart=always -p 9000:9000 -d naumy/hitrend-sonarqube:v1.0 # 運(yùn)行sonarqube容器 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/ 就可以了,默認(rèn)管理員用戶和密碼為:admin/admin
。
嵌入式數(shù)據(jù)庫應(yīng)僅用于評(píng)估目的、嵌入式數(shù)據(jù)庫無法擴(kuò)展,不支持升級(jí)到SonarQube的較新版本,也不支持將數(shù)據(jù)從中遷移到其他數(shù)據(jù)庫引擎。
1.2保存并提交已修改的鏡像
# 保存已經(jīng)修的鏡像 docker commit -a "naumy" -m "安裝中文插件" 19f1cc24dc98 hitrend-sonarqube:v1.0 # 把舊鏡像的名字,改成倉庫要求的新版名字 docker tag hitrend-sonarqube:v1.0 naumy/hitrend-sonarqube:v1.0 # 登錄到docker hub docker login # 推送 docker push naumy/hitrend-sonarqube:v1.0
2.安裝成功
3.插件安裝
3.1安裝Chinese插件
SonarQube
提供了強(qiáng)大的插件管理功能,以中文語言包為示例,講解如何安裝插件:
登錄成功后,選擇Administration-Marketplace-Plugins
,在搜索框輸入Chinese
就可以選擇安裝了。
當(dāng)狀態(tài)顯示為Install Pending
時(shí),說明插件安裝完成,點(diǎn)擊Restart Server
即可生效。
之后,就顯示為中文了。
同時(shí)安裝findbug插件
4.docker安裝gitlab
4.1.Gitlab鏡像拉取
# gitlab-ce為穩(wěn)定版本,后面不填寫版本則默認(rèn)pull最新latest版本 $ docker pull gitlab/gitlab-ce
4.2運(yùn)行g(shù)itlab鏡像
$ 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 # -d:后臺(tái)運(yùn)行 # -p:將容器內(nèi)部端口向外映射 # --name:命名容器名稱 # -v:將容器內(nèi)數(shù)據(jù)文件夾或者日志、配置等文件夾掛載到宿主機(jī)指定目錄
按上面的方式,gitlab容器運(yùn)行沒問題,但在gitlab上創(chuàng)建項(xiàng)目的時(shí)候,生成項(xiàng)目的URL訪問地址是按容器的hostname來生成的,也就是容器的id。
作為gitlab服務(wù)器,我們需要一個(gè)固定的URL訪問地址,于是需要配置gitlab.rb(宿主機(jī)路徑:/home/gitlab/config/gitlab.rb)。
# gitlab.rb文件內(nèi)容默認(rèn)全是注釋 $ vim /home/gitlab/config/gitlab.rb
# 配置http協(xié)議所使用的訪問地址,不加端口號(hào)默認(rèn)為80 external_url 'http://192.168.199.231' # 配置ssh協(xié)議所使用的訪問地址和端口 gitlab_rails['gitlab_ssh_host'] = '192.168.199.231' gitlab_rails['gitlab_shell_ssh_port'] = 222 # 此端口是run時(shí)22端口映射的222端口 :wq #保存配置文件并退出
# 重啟gitlab容器 $ docker restart gitlab
此時(shí)項(xiàng)目的倉庫地址就變了。如果ssh端口地址不是默認(rèn)的22,就會(huì)加上ssh:// 協(xié)議頭
打開瀏覽器輸入ip地址(因?yàn)槲业膅itlab端口為80,所以瀏覽器url不用輸入端口號(hào),如果端口號(hào)不是80,則打開為:ip:端口號(hào))
4.3設(shè)置root用戶名和密碼
進(jìn)入目錄 /home/gitlab/config/initial_root_password,查看密碼
xwCsS7lMYx+8x3o6KIBw+Ia6Lg3VqvtHLzxzYfPNtxk=
或者進(jìn)入gitlab容器后修改密碼。
root@ba96cb6a1f47:/# gitlab-rails console -------------------------------------------------------------------------------- Ruby: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux] GitLab: 14.3.2 (92acfb1b8a9) FOSS GitLab Shell: 13.21.1 PostgreSQL: 12.7 -------------------------------------------------------------------------------- irb(main):005:0> user = User.where(id: 1).first => #<User id:1 @root> irb(main):006:0> user.password=12345678 => 12345678 irb(main):007:0> user.password_confirmation=12345678 => 12345678 irb(main):008:0> user.save! 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>>]} => true irb(main):009:0> quit
運(yùn)行后的效果圖
4.4保存鏡像并推送dockerhub
# 保存已經(jīng)修的鏡像 docker commit -a "naumy" -m "初始化gitlab" ba96cb6a1f47 gitlab:v1.0 docker commit -a "naumy" -m "sonarqube:7.6-community " e70c6cbe2e0b sonarqube-7.6-community:v1.0 docker tag sonarqube-7.6-community:v1.0 naumy/sonarqube-7.6-community:v1.0 docker push naumy/sonarqube-7.6-community:v1.0 # 把舊鏡像的名字,改成倉庫要求的新版名字 docker tag gitlab:v1.0 naumy/gitlab:v1.0 # 登錄到docker hub docker login # 推送 docker push naumy/gitlab:v1.0
5.碰到的問題
5.1虛擬內(nèi)存不夠
啟動(dòng)容器后過了十幾秒。容器自動(dòng)退出。
Error: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
運(yùn)行容器后,容器馬上退出。 # 使用命令查看運(yùn)行日志 docker logs 容器名稱/容器ID
在/etc/sysctl.conf文件最后添加一行
vm.max_map_count=262144
執(zhí)行/sbin/sysctl -p
立即生效
6.整合Sonar和gitlab
6.1安裝Gitlab-runner
6.1.1獲取gitlab-Token
進(jìn)入gitlab后,選擇runner,進(jìn)行相應(yīng)的Token獲取。
6.1.2安裝gitlab-runner
# 拉取鏡像 docker pull gitlab/gitlab-runner:v13.2.4 # 創(chuàng)建容器映射目錄 mkdir -p /dwz/docker-volume/gitlab-runner/config # 創(chuàng)建容器并運(yùn)行 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
進(jìn)入gitlab-runner容器后,配置相應(yīng)的參數(shù)設(shè)置:
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頁面,出現(xiàn)runner配置項(xiàng)。
6.2設(shè)置sonarqube的用戶名和密碼
設(shè)置當(dāng)前的sonarqube的用戶面和密碼為admin和123456
6.3進(jìn)行項(xiàng)目分析(手動(dòng)添加項(xiàng)目)
是否需要集成自己喜歡的CI,使用gitlab進(jìn)行持續(xù)集成和持續(xù)部署。
第一步 選擇需要檢測項(xiàng)目代碼類型:
新建配置文件sonar-project.properties:
sonar.projectKey=gitlab-sonorqube sonar.qualitygate.wait=true sonar.language=py
第二步:添加環(huán)境變量
令牌密鑰:b23fe46d142fcfb052b05d5b3fd6fc823df0b682
按照要求添加相應(yīng)的環(huán)境變量:
6.4進(jìn)行CI/CD(sonar與gitlab)
6.4.1版本為sonarqube-7.6-community
創(chuàng)建一個(gè)gitlab項(xiàng)目,實(shí)驗(yàn)使用的項(xiàng)目為python項(xiàng)目。
.gitlab-ci.yml文件內(nèi)容為
# This file is a template, and might need editing before it works on your project. # To contribute improvements to CI/CD templates, please follow the Development guide at: # https://docs.gitlab.com/ee/development/cicd/templates.html # This specific template is located at: # https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml # This is a sample GitLab CI/CD configuration file that should run without any modifications. # It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts, # it uses echo commands to simulate the pipeline execution. # # A pipeline is composed of independent jobs that run scripts, grouped into stages. # Stages run in sequential order, but jobs within stages run in parallel. # # For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages stages: # List of stages for jobs, and their order of execution - build - test - deploy build-job: # This job runs in the build stage, which runs first. stage: build script: - echo "Compiling the code..." - echo "Compile complete." unit-test-job: # This job runs in the test stage. stage: test # It only starts when the job in the build stage completes successfully. script: - echo "Running unit tests... This will take about 60 seconds." - sleep 60 - echo "Code coverage is 90%" lint-test-job: # This job also runs in the test stage. stage: test # It can run at the same time as unit-test-job (in parallel). script: - echo "Linting code... This will take about 10 seconds." - sleep 10 - echo "No lint issues found." deploy-job: # This job runs in the deploy stage. stage: deploy # It only runs when *both* jobs in the test stage complete successfully. script: - echo "Deploying application..." - echo "Application successfully deployed." image: name: sonarsource/sonar-scanner-cli:latest entrypoint: [""] sonarqube-check: script: - 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=. allow_failure: true only: - main # or the name of your main branch
提交代碼后,可以獲取到相應(yīng)的測試信息。
https://sm.ms/image/ykYPlDgZVvuhzsq
6.4.2版本為sonarqube-9.1-community
.gitlab-ci.yml文件內(nèi)容為
sonarqube-check: image: name: sonarsource/sonar-scanner-cli:latest entrypoint: [""] variables: SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task cache: key: "${CI_JOB_NAME}" paths: - .sonar/cache script: - 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=. allow_failure: true only: - main # or the name of your main branch
提交代碼后gitlab會(huì)自動(dòng)進(jìn)行CI/CD:
點(diǎn)入進(jìn)去后查看相應(yīng)的狀態(tài)和內(nèi)容是否符合需求:
運(yùn)行完成后,將看到對(duì)應(yīng)的測試分析結(jié)果:
6.5在整合過程中碰到的問題
配置文件寫錯(cuò):
使用的python代碼,所以后續(xù)將使用py作為語言選擇。
7.總結(jié)
當(dāng)前使用的工具有:
sonarqube:9.1.0-community 、gitlab/gitlab-runner:v13.2.4 、postgres:9.6.23 、gitlab/gitlab-ce、sonarsource/sonar-scanner-cli:latest
開發(fā)人員提交代碼到gitlab倉庫后,觸發(fā)master分支自動(dòng)合并任務(wù),并進(jìn)行代碼掃描(可改成其他測試分支),掃面結(jié)果返回到sonarqube平臺(tái)。
到此這篇關(guān)于Docker安裝SonarQube的文章就介紹到這了,更多相關(guān)Docker安裝SonarQube內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Docker 容器指定自定義網(wǎng)段的固定IP/靜態(tài)IP地址
這篇文章主要介紹了Docker 容器指定自定義網(wǎng)段的固定IP/靜態(tài)IP地址的相關(guān)資料,需要的朋友可以參考下2016-12-12Docker 手動(dòng)配置容器網(wǎng)絡(luò)實(shí)例詳解
這篇文章主要介紹了Docker 手動(dòng)配置容器網(wǎng)絡(luò)實(shí)例詳解的相關(guān)資料,這里有具體實(shí)現(xiàn),需要的朋友可以參考下2016-11-11Windows10安裝WSL2 Ubuntu20.04并設(shè)置docker環(huán)境的方法
這篇文章主要介紹了Windows10安裝WSL2 Ubuntu20.04并設(shè)置docker環(huán)境的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05docker使用alpine構(gòu)建jdk21鏡像的詳細(xì)步驟
這篇文章主要給大家介紹了關(guān)于docker使用alpine構(gòu)建jdk21鏡像的詳細(xì)步驟,鏡像中包含了應(yīng)用程序所需要的運(yùn)行環(huán)境,函數(shù)庫,配置,以及應(yīng)用本身等各種文件,這些文件分層打包而成,需要的朋友可以參考下2024-03-03docker?搭建?vulhub?靶場環(huán)境的詳細(xì)過程
Vulhub是一個(gè)基于docker和docker-compose的漏洞環(huán)境集合,進(jìn)入對(duì)應(yīng)目錄并執(zhí)行一條語句即可啟動(dòng)一個(gè)全新的漏洞環(huán)境,讓漏洞復(fù)現(xiàn)變得更加簡單,讓安全研究者更加專注于漏洞原理本身,這篇文章給大家介紹docker?搭建?vulhub?靶場環(huán)境的過程,感興趣的朋友一起看看吧2022-08-08詳解docker私有倉庫搭建與使用實(shí)戰(zhàn)
這篇文章主要介紹了詳解docker私有倉庫搭建與使用實(shí)戰(zhàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02