我正在尝试运行基于 SonarQube 图像的 AWS CodeBuild 项目。
通过
buildspec.yaml
,我想使用 SonarQube API 执行一些操作(例如,通过 /api/user_tokens/generate
API 为用户生成令牌),但我无法使用 localhost
访问应用程序。./codebuild_build.sh -i sonarqube:latest -a ./output -b ./buildspec.yaml
这是
buildspec.yaml
:
version: 0.2
phases:
install:
on-failure: ABORT
run-as: root
commands:
- apt update
- apt install -y net-tools curl
post_build:
commands:
- netstat -tuln
- curl http://localhost:9000
我得到了什么:
...
agent_1 | [Container] 2023/07/26 07:48:16 Phase complete: INSTALL State: SUCCEEDED
agent_1 | [Container] 2023/07/26 07:48:16 Phase context status code: Message:
agent_1 | [Container] 2023/07/26 07:48:16 Entering phase PRE_BUILD
agent_1 | [Container] 2023/07/26 07:48:16 Phase complete: PRE_BUILD State: SUCCEEDED
agent_1 | [Container] 2023/07/26 07:48:16 Phase context status code: Message:
agent_1 | [Container] 2023/07/26 07:48:16 Entering phase BUILD
agent_1 | [Container] 2023/07/26 07:48:16 Phase complete: BUILD State: SUCCEEDED
agent_1 | [Container] 2023/07/26 07:48:16 Phase context status code: Message:
agent_1 | [Container] 2023/07/26 07:48:16 Entering phase POST_BUILD
agent_1 | [Container] 2023/07/26 07:48:16 Running command netstat -tuln
agent_1 | Active Internet connections (only servers)
agent_1 | Proto Recv-Q Send-Q Local Address Foreign Address State
agent_1 | tcp 0 0 127.0.0.11:46439 0.0.0.0:* LISTEN
agent_1 | udp 0 0 127.0.0.11:55056 0.0.0.0:*
agent_1 |
agent_1 | [Container] 2023/07/26 07:48:16 Running command curl http://localhost:9000
agent_1 | % Total % Received % Xferd Average Speed Time Time Time Current
agent_1 | Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
agent_1 | curl: (7) Failed to connect to localhost port 9000 after 0 ms: Connection refused
agent_1 |
agent_1 | [Container] 2023/07/26 07:48:16 Command did not exit successfully curl http://localhost:9000 exit status 7
agent_1 | [Container] 2023/07/26 07:48:16 Phase complete: POST_BUILD State: FAILED
agent_1 | [Container] 2023/07/26 07:48:16 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: curl http://localhost:9000. Reason: exit status 7
agent-resources_agent_1 exited with code 0
正如您通过
netstat
命令看到的,端口 9000(SonarQube 默认端口)上没有任何内容,并且出现地址 127.0.0.11
,而不是预期的 127.0.0.1
或 0.0.0.0
。 事实上,如果我使用以下命令在本地运行 docker 容器,SonarQube 图像没有任何问题:
docker run -d --name sonarqube sonarqube:latest
然后我运行这个命令:
docker exec -it -u 0 sonarqube bash -c "apt update; apt install -y net-tools curl; netstat -tuln; curl http://localhost:9000"
我通过监听端口 9000 的应用程序获得了预期的输出。事实上,
curl
显示了 html SonarQube 页面:
...
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:40501 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:37211 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9092 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN <--------
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/apple-touch-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon-180x180.png">
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<meta name="application-name" content="SonarQube" />
<meta name="msapplication-TileColor" content="#FFFFFF" />
<meta name="msapplication-TileImage" content="/mstile-512x512.png" />
<title>SonarQube</title>
...
如何在 CodeBuild 上实现相同的目标?我做错了什么?
预先感谢
我找到了解决方法。
由于
entrypoint.sh
的作用不仅仅是执行 lib/sonarqube.sh
,因此我可以手动执行该文件,而无需传递 entrypoint.sh
文件。请注意,sonarqube 不能作为 root
执行。 buildspec
应该是这样的:
version: 0.2
phases:
install:
on-failure: ABORT
run-as: root
commands:
- apt update
- apt install -y net-tools curl
build:
run-as: sonarqube
commands:
- java -jar $SONARQUBE_HOME/lib/sonarqube.jar -Dsonar.log.console=true &
- while ! curl -s http://localhost:9000 -o /dev/null; do echo "SonarQube is still starting.."; sleep 5; done
post_build:
run-as: root
commands:
- netstat -tuln
- curl http://localhost:9000
等待更优雅的解决方案,希望它对某人有所帮助。