Docker构建时newrelic-infra安装失败,/etc文件添加异常求助
解决New Relic Infrastructure Agent安装时的D-Bus连接错误
首先咱们先理清问题:你看到的Failed to connect to bus: No such file or directory错误,和/etc/newrelic-infra.yml文件无关——从你的构建日志里能看到,cat /etc/newrelic-infra.yml已经成功输出了license key,说明文件是存在且正确的。真正的问题出在New Relic Infra Agent的post-install脚本上:它试图通过systemd的D-Bus套接字启动服务,但你的基础镜像openresty/openresty:latest-xenial默认没有运行systemd,也没有初始化D-Bus环境。
下面是两种可行的解决方案:
方案一:预初始化D-Bus环境再安装
在安装newrelic-infra之前,手动创建D-Bus所需的目录并启动后台守护进程,让post-install脚本能找到总线:
修改你的Dockerfile中安装newrelic-infra的部分,添加以下步骤:
# 创建D-Bus运行目录并启动守护进程 RUN mkdir -p /var/run/dbus RUN dbus-daemon --system --fork # 现在安装newrelic-infra RUN DEBIAN_FRONTEND=noninteractive apt-get install -yq newrelic-infra
方案二:跳过服务启动,手动配置Agent
如果你不需要在构建镜像时启动Agent(毕竟容器启动时通常会自己管理进程),可以直接跳过post-install脚本里的服务启动步骤:
- 先安装newrelic-infra但不配置它:
RUN DEBIAN_FRONTEND=noninteractive apt-get install -yq newrelic-infra --no-configure
- 手动完成配置(因为我们已经有了
/etc/newrelic-infra.yml):
# 手动执行配置步骤,跳过服务启动 RUN dpkg --configure newrelic-infra --force-confold
--force-confold参数会让dpkg保留现有配置文件,并且跳过可能触发服务启动的步骤。
额外建议
- 把多个RUN指令合并成一个,减少镜像层数,同时避免缓存导致的不可控问题:
RUN apt-get update && \ apt-get install -y apt-transport-https apt-utils && \ echo "license_key: 64553f3xxxxxxxxx" > /etc/newrelic-infra.yml && \ curl https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg | apt-key add - && \ printf "deb [arch=amd64] https://download.newrelic.com/infrastructure_agent/linux/apt xenial main" > /etc/apt/sources.list.d/newrelic-infra.list && \ apt-get update
- 容器运行时,New Relic Agent需要以正确的方式启动,通常你需要在容器启动命令里加上
/usr/bin/newrelic-infra -config /etc/newrelic-infra.yml,而不是依赖systemd。
内容的提问来源于stack exchange,提问作者silverdagger
相关产品推荐
相关产品推荐

