"Extending" Dockerfile修改文件,导致sonarqube失败

"Extending" Dockerfile to modify a file, caused sonarqube to fail

我正在尝试将 Sonarqube 与 CloudRun 结合使用,为此我需要支持在启动 docker 图像时使用环境变量 PORT。所以我试着 "extend" 我的 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 时失败了

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

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

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

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

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