【问题标题】:"Extending" Dockerfile to modify a file, caused sonarqube to fail“扩展” Dockerfile 以修改文件,导致 sonarqube 失败
【发布时间】:2020-03-11 04:50:54
【问题描述】:

我正在尝试将 Sonarqube 与 CloudRun 一起使用,为此我需要在启动 docker 映像时支持使用环境变量 PORT。所以我试图“扩展”我的Dockerfile,比如:

FROM sonarqube:7.9-community
WORKDIR $SONARQUBE_HOME
COPY sonar.properties $SONARQUBE_HOME
COPY run.sh ./bin/
EXPOSE 8080
ENTRYPOINT ["./bin/run.sh"]

我修改了 sonar.properties 以包含如下一行:

sonar.web.port=__PORT__

然后我将run.sh修改为:

sed "s/__PORT__/$PORT/g" ./sonar.properties > conf/sonar.properties

并尝试像这样启动服务器:

docker run -e PORT=8080 sonarqube-custom

日志显示没有错...

2019.11.15 02:55:04 INFO  web[][o.s.s.q.ProjectsInWarningDaemon] Counting number of projects in warning is enabled.
2019.11.15 02:55:04 INFO  web[][o.s.s.p.p.PlatformLevelStartup] Running Community Edition
2019.11.15 02:55:04 INFO  web[][o.s.s.p.Platform] WebServer is operational
2019.11.15 02:55:04 INFO  app[][o.s.a.SchedulerImpl] Process[web] is up
2019.11.15 02:55:04 INFO  app[][o.s.a.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/opt/sonarqube]: /usr/local/openjdk-11/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/sonarqube/temp --add-opens=java.base/java.util=ALL-UNNAMED -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Dhttp.nonProxyHosts=localhost|127.*|[::1] -cp ./lib/common/*:/opt/sonarqube/lib/jdbc/h2/h2-1.3.176.jar org.sonar.ce.app.CeServer /opt/sonarqube/temp/sq-process3988720795271274831properties
2019.11.15 02:55:04 INFO  web[][o.s.s.q.ProjectsInWarningDaemon] Counting number of projects in warning will be disabled as there are no more projects in warning.
2019.11.15 02:55:05 INFO  ce[][o.s.p.ProcessEntryPoint] Starting ce
2019.11.15 02:55:05 INFO  ce[][o.s.ce.app.CeServer] Compute Engine starting up...
2019.11.15 02:55:06 INFO  ce[][o.e.p.PluginsService] no modules loaded
2019.11.15 02:55:06 INFO  ce[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2019.11.15 02:55:06 INFO  ce[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2019.11.15 02:55:06 INFO  ce[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2019.11.15 02:55:07 INFO  ce[][o.s.s.e.EsClientProvider] Connected to local Elasticsearch: [127.0.0.1:9001]
2019.11.15 02:55:07 INFO  ce[][o.sonar.db.Database] Create JDBC data source for jdbc:h2:tcp://127.0.0.1:9092/sonar
2019.11.15 02:55:07 WARN  ce[][o.s.db.dialect.H2] H2 database should be used for evaluation purpose only.
2019.11.15 02:55:08 INFO  ce[][o.s.s.p.ServerFileSystemImpl] SonarQube home: /opt/sonarqube
2019.11.15 02:55:08 INFO  ce[][o.s.c.c.CePluginRepository] Load plugins
2019.11.15 02:55:10 INFO  ce[][o.s.c.c.ComputeEngineContainerImpl] Running Community edition
2019.11.15 02:55:10 INFO  ce[][o.s.ce.app.CeServer] Compute Engine is operational
2019.11.15 02:55:10 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
2019.11.15 02:55:10 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up

但是当我尝试访问 URL localhost:8080 时失败了

【问题讨论】:

  • 你在使用 MACos 作为声纳容器吗?
  • 如果您以docker run -e PORT=8080 sonarqube-custom 启动您的容器,您将无法联系localhost:8080 上的任何内容,因为您还没有发布任何端口(例如使用-p 选项到@ 987654334@).
  • @ThanhNguyenVan 是的,我现在在 Mac 上
  • Sonarqube 使用默认端口:9000,你能分享run.sh 文件吗?
  • 尝试docker run -p "8080:8080" -e PORT=8080sonarqube-custom,它会将主机8080发布到容器8080。我认为,您不需要更改并添加sonar.web.port=__PORT__,让sonarqube监听任何端口并发布特定主机端口到容器。

标签: docker sonarqube


【解决方案1】:

如果您以 docker run -e PORT=8080 sonarqube-custom 启动容器,您将无法联系 localhost:8080 上的任何内容,因为您尚未发布任何端口(例如,使用 -p 选项泊坞窗运行)。

请务必注意,您可能根本不需要修改 sonarqube 配置:您只需 docker run -p 8080:9000 ... 在主机上的端口 8080 上公开服务,尽管它在容器内的端口 9000 上运行。

【讨论】:

  • 最后一个有用的东西——EXPOSE 命令并没有像你想象的那样做。它标记该端口,以便如果您将-P 传递给docker run,它将选择一个随机端口映射到该端口。在过去的六年里,我仍然没有找到-P 命令或 EXPOSE 的实际用途。
  • 我认为我需要 PORT 补丁,因为 CloudRun 需要使用 env var 设置端口
【解决方案2】:

您需要了解expose 标志(-e) 和publish 标志(-p) 之间的区别。想象你的容器就像一台计算机,你的 docker stack(服务的集合)就像一个网络。你有一个计算机防火墙和一个网络防火墙。

当您expose您的端口时,计算机防火墙会打开该端口以允许进入流量。因此,只要不暴露端口,监听端口的服务就只能从容器(或我们参考中的计算机)内部访问。此配置对于您希望仅从网络内部访问您的服务的情况很有帮助。例如,与业务逻辑层交互但不应暴露于开放互联网的数据库。当它暴露时,来自同一堆栈的其他容器(或同一网络上的其他计算机)的流量可以访问该服务,但由于网络防火墙不允许任何入口流量,因此无法从其他网络访问它。

这就是端口publish的来源。当你exposepublish你的端口时,现在不仅可以从容器中的其他服务访问在网络上监听的服务堆栈(网络中的其他计算机)也来自网络外部。此配置适用于用户与之交互的服务。例如,您的应用程序的用户界面层。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多