简体   繁体   English

如何使SonarQube(在docker容器中运行)从Jenkins Pipeline连接到localhost:9000?

[英]How to get SonarQube (running in docker container) to connect to localhost:9000 from Jenkins Pipeline?

I am running SonarQube on both java and javascript code inside a CentOS VM. 我在CentOS VM内的java和javascript代码上都运行SonarQube。 I pulled the sonarqube docker image and am running the container. 我拉了sonarqube码头工人图像并正在运行容器。 I exposed port 9000 and am able to open up localhost:9000 and interact with the sonarqube gui. 我暴露了端口9000,并且能够打开localhost:9000并与sonarqube gui进行交互。 When I run the sonar-scanner from the command line on the JavaScript code using this command: 当我使用以下命令从JavaScript代码的命令行运行声纳扫描仪时:

sonar-scanner -Dsonar.projectKey=javascript_code -Dsonar.sources=. -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=admin 

I get a fully analyzed project. 我得到了一个经过全面分析的项目。 As well as the JavaScript code I can run it on the Java code using the maven build. 除了JavaScript代码,我还可以使用Maven构建在Java代码上运行它。 Both work correctly from the command line. 两者都可以从命令行正常工作。

When I add it into a Jenkins Pipeline I do the exact same commands and get the following: 当我将其添加到Jenkins管道中时,我会执行完全相同的命令并获得以下信息:

Error: SonarQube server [http://localhost:9000] can not be reached

Caused by: Fail to get bootstrap index from server

Caused by: Failed to connect to localhost:9000

Caused by: Connection refused (Connection refused)

I also tried when starting the container to use --network="host" instead of publishing port 9000, but it still does not connect from the Jenkins Pipeline. 启动容器时,我也尝试使用--network =“ host”代替发布端口9000,但它仍然无法通过Jenkins Pipeline连接。 Everything works correctly when I am running the commands from the Command Line, just when I try to put it into a Jenkins Pipeline. 当我从命令行运行命令时,一切正常,就像我尝试将其放入Jenkins Pipeline一样。

Any suggestions on how to fix this? 对于如何解决这个问题,有任何的建议吗? Thanks. 谢谢。

*Edit: Using the sonarqube plugin I was able to get my analysis to run as a Jenkins Job. *编辑:使用sonarqube插件,我能够将分析作为Jenkins Job运行。 However, when I try to add the analysis into the pipeline I get the same SonarQube server cannot be reached error. 但是,当我尝试将分析添加到管道中时,无法得到相同的SonarQube服务器错误。 It says it skips my project because the project has been banned form the build due to previous failures and gives me this error message: 它说它跳过了我的项目,因为由于先前的失败,该项目已被禁止从构建中进行,并给我以下错误消息:

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.5.0.1254:sonar (default-cli) on project sonarqubeproj: 
Unable to execute SonarQube: Fail to get bootstrap index from server: unexpected end of stream on Connection{localhost:9000, proxy=DIRECT hostAddress=localhost/127.0.0.1:9000 cipherSuite=none protocol=http/1.1}: \n not found: limit=0 content=… -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.5.0.1254:sonar (default-cli) on project feedback-tool-ui: Unable to execute SonarQube

The problem you are facing is most likely related to how you run jenkins. 您面临的问题很可能与如何运行jenkins有关。

I assume you also run jenkins via docker. 我假设您也通过docker运行jenkins。 this means that in the context of jenkins, localhost is not your host system, but the jenkins container. 这意味着在jenkins的上下文中,localhost不是您的主机系统,而是jenkins容器。 This means you need to adapt your sonarqube-url to reference the host system. 这意味着您需要调整您的sonarqube-url以引用主机系统。

Therefore you have multiple options: 因此,您有多种选择:

  1. use the public ip of the host system 使用主机系统的公共IP
  2. use the docker ip of the host system 使用主机系统的docker ip
  3. resolve the ip via dns. 通过DNS解析IP。 docker has an internal DNS and if those container run in the same network, you can normally reach them by containername. docker有一个内部DNS,如果这些容器在同一网络中运行,则通常可以通过containername访问它们。

sidenote: i am not sure if you are using docker-compose or kubernetes, but if you are not using it, you might also want to dive into docker-compose as it is just a simple yaml-config in which you can easily start up servers. 旁注:我不确定您是使用docker-compose还是kubernetes,但如果您不使用它,您可能还想深入研究docker-compose,因为它只是一个简单的yaml-config,您可以在其中轻松启动服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM