在Google Run的Docker容器中运行LimaCharlie Adapter遇文件名过长错误
问题:Docker运行LimaCharlie Adapter时触发“文件名过长”错误
尝试创建Docker容器运行LimaCharlie Adapter接收syslog,运行时出现以下错误:
PS C:\Users\admin\code\syslog-to-limacharlie-docker-main> docker run syslog-to-limacharlie-docker-main /bin/sh: 0: cannot open ./opt/lc_adapter syslog client_options.identity.installation_key=1-2-3-4-5 client_options.identity.oid=6-7-8-9-0 client_options.platform=text client_options.hostname=MySensor client_options.sensor_seed_key=MySensor port=8080 iface=0.0.0.0 is_udp=true: File name too long
原Dockerfile内容:
# Specify Ubuntu FROM ubuntu:latest # Update RUN apt-get update \ && apt-get install -y apt-utils file # Open port EXPOSE 8080 # Download LimaCharlie Adapter (will download as file name "64" to /opt/64) ADD https://downloads.limacharlie.io/adapter/linux/64 /opt RUN cd /opt RUN mv /opt/64 /opt/lc_adapter # Create logcollector.service file for LimaCharlie RUN chmod +x /opt/lc_adapter CMD [ "/bin/sh", "./opt/lc_adapter syslog client_options.identity.installation_key=1-2-3-4-5 client_options.identity.oid=6-7-8-9-0 client_options.platform=text client_options.hostname=MySensor client_options.sensor_seed_key=MySensor port=8080 iface=0.0.0.0 is_udp=true" ]
lc_adapter文件信息:
# file lc_adapter lc_adapter: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=2MLFP8X_NxNWUzoPPZVD/MjzxEyHS5R7MY7guaGpe/avSLCqnqTExmYGH0Zsvo/pJ5s9HhisDNA_DZr4fJT, with debug_info, not stripped
修复方案
错误根源是CMD指令的数组格式使用错误:当使用exec格式(CMD ["executable", "param1", "param2"])时,每个命令参数必须作为独立的数组元素,不能将整个命令行打包成单个参数。原Dockerfile中把./opt/lc_adapter及其所有参数放在同一个字符串里,导致/bin/sh把整个字符串当作要打开的文件名,从而触发“文件名过长”错误。
同时还存在相对路径问题:容器默认工作目录是/,./opt/lc_adapter会被解析为/./opt/lc_adapter,虽然能找到文件,但更规范的做法是使用绝对路径。
具体修复步骤:
- 拆分CMD的数组元素:将每个参数单独列为数组项
- 使用绝对路径调用lc_adapter:替换
./opt/lc_adapter为/opt/lc_adapter - 可选优化:合并RUN命令,清理APT缓存以减小镜像体积,移除无效的
RUN cd /opt指令(每个RUN是独立shell会话,该指令不会改变后续步骤的工作目录)
修正后的Dockerfile:
# Specify Ubuntu FROM ubuntu:latest # Update and install dependencies, clean up cache RUN apt-get update \ && apt-get install -y apt-utils file \ && rm -rf /var/lib/apt/lists/* # Open port EXPOSE 8080 # Download, rename and make LimaCharlie Adapter executable ADD https://downloads.limacharlie.io/adapter/linux/64 /opt/lc_adapter RUN chmod +x /opt/lc_adapter # Run LimaCharlie Adapter with syslog configuration CMD [ "/opt/lc_adapter", "syslog", "client_options.identity.installation_key=1-2-3-4-5", "client_options.identity.oid=6-7-8-9-0", "client_options.platform=text", "client_options.hostname=MySensor", "client_options.sensor_seed_key=MySensor", "port=8080", "iface=0.0.0.0", "is_udp=true" ]
内容的提问来源于stack exchange,提问作者Europa
相关产品推荐
相关产品推荐

