Cloud Foundry中覆盖Docker镜像启动CMD失败:OTel Collector部署问题
我正尝试将标准OTel Collector Contrib Docker镜像部署到Cloud Foundry。根据OTel收集器配置文档,计划通过环境变量注入配置,指定参数:--config=env:MY_CONFIG_IN_AN_ENVVAR。为此需要覆盖镜像的启动命令(CMD),按照Cloud Foundry文档,使用CF manifest.yaml文件配置。
镜像原CMD为:
CMD ["--config", "/etc/otelcol-contrib/config.yaml"]
我先尝试用原CMD值测试覆盖功能,对应的manifest.yaml如下:
applications: - name: occ memory: 256M disk_quota: 512M instances: 1 health-check-type: process docker: image: otel/opentelemetry-collector-contrib:0.114.0 command: "--config=/etc/otelcol-contrib/config.yaml" routes: - route: occ.some.domain.com protocol: http2
执行cf push后部署失败,报错信息如下:
2024-11-21T23:15:07.83+0100 [CELL/0] OUT Cell 36978039-f310-488f-84df-e717cacf7f21 stopping instance c8b25623-18ad-48be-76f0-fe2c 2024-11-21T23:15:07.83+0100 [CELL/0] OUT Cell 36978039-f310-488f-84df-e717cacf7f21 destroying container for instance c8b25623-18ad-48be-76f0-fe2c 2024-11-21T23:15:07.83+0100 [API/36] OUT Process has crashed with type: "web" 2024-11-21T23:15:07.85+0100 [API/36] OUT App instance exited with guid 1b878955-8f60-4c17-ba79-ed642b842fe5 payload: {"instance"=>"c8b25623-18ad-48be-76f0-fe2c", "index"=>0, "cell_id"=>"36978039-f310-488f-84df-e717cacf7f21", "reason"=>"CRASHED", "exit_description"=>"APP/PROC/WEB: Exited with status 1", "crash_count"=>1, "crash_timestamp"=>1732227307824196148, "version"=>"495c31ed-0cd1-4120-961b-3d9add207828"} 2024-11-21T23:15:07.87+0100 [CELL/0] OUT Cell 55878f2d-faab-4a94-96ba-ffe97bd7e165 creating container for instance e3175892-6acc-4b7a-4e4c-2d3e 2024-11-21T23:15:08.22+0100 [PROXY/0] OUT Exit status 137 2024-11-21T23:15:08.87+0100 [CELL/0] OUT Cell 36978039-f310-488f-84df-e717cacf7f21 successfully destroyed container for instance c8b25623-18ad-48be-76f0-fe2c 2024-11-21T23:15:12.27+0100 [CELL/0] OUT Security group rules were updated 2024-11-21T23:15:12.34+0100 [CELL/0] OUT Cell 55878f2d-faab-4a94-96ba-ffe97bd7e165 successfully created container for instance e3175892-6acc-4b7a-4e4c-2d3e 2024-11-21T23:15:12.71+0100 [APP/PROC/WEB/0] OUT Exit status 1 2024-11-21T23:15:12.71+0100 [APP/PROC/WEB/0] ERR Failed to run: no such file or directory 2024-11-21T23:15:12.71+0100 [CELL/SSHD/0] OUT Exit status 0
若不覆盖command,镜像可正常启动。请问问题出在哪里?
问题核心是你在manifest中使用了字符串格式的command,但原镜像采用的是exec格式(数组)的ENTRYPOINT+CMD组合。
原镜像的启动逻辑是:由ENTRYPOINT指定的主程序(如otelcol-contrib)加载CMD中的参数。当你用字符串形式的command: "--config=/etc/otelcol-contrib/config.yaml"时,Cloud Foundry会尝试将这个字符串当作一个独立的可执行文件去运行,而非作为参数传递给原镜像的主程序——系统自然找不到名为--config=/etc/otelcol-contrib/config.yaml的文件,因此报错“no such file or directory”。
解决方法
有两种可行的修正方式:
使用数组格式指定command,与原镜像的CMD格式保持一致:
command: ["--config=/etc/otelcol-contrib/config.yaml"]或者遵循标准exec格式拆分参数:
command: ["--config", "/etc/otelcol-contrib/config.yaml"]明确指定完整启动命令,包含主程序名和参数:
command: ["otelcol-contrib", "--config=/etc/otelcol-contrib/config.yaml"]
验证上述方法可行后,即可替换为你需要的环境变量配置:
command: ["--config=env:MY_CONFIG_IN_AN_ENVVAR"]
或者
command: ["otelcol-contrib", "--config=env:MY_CONFIG_IN_AN_ENVVAR"]
内容的提问来源于stack exchange,提问作者FloW

