使用管道命令的 Docker ENTRYPOINT

使用管道命令的 Docker ENTRYPOINT

我正在尝试创建一个使用以下组合的 dockerfile負責rtlamr-收集收集有关我的公用事业仪表的信息。我无法ENTRYPOINT在 docker 中获取rtlamr | rtlamr-collect

这是我的dockerfile

FROM golang:latest AS build
RUN CGO_ENABLED=0 GOOS=linux go install github.com/bemasher/rtlamr-collect@latest
RUN CGO_ENABLED=0 GOOS=linux go install github.com/bemasher/rtlamr@latest

# Use a multi-stage build to reduce our image size
FROM alpine:latest AS runtime
COPY --from=build /go/bin/rtlamr /usr/bin/rtlamr
COPY --from=build /go/bin/rtlamr-collect /usr/bin/rtlamr-collect

# Really wish this would just work as expected:
ENTRYPOINT ["rtlamr | rtlamr-collect"]

作为一种解决方法,我发现我可以创建一个简单的 shell 脚本并将其作为入口点,但这并不理想,因为需要一个额外的文件。不过,我并不反对以某种方式动态创建额外文件的解决方案:

在职的dockerfile

FROM golang:latest AS build
RUN CGO_ENABLED=0 GOOS=linux go install github.com/bemasher/rtlamr-collect@latest
RUN CGO_ENABLED=0 GOOS=linux go install github.com/bemasher/rtlamr@latest

# Use a multi-stage build to reduce our image size
FROM alpine:latest AS runtime
COPY --from=build /go/bin/rtlamr /usr/bin/rtlamr
COPY --from=build /go/bin/rtlamr-collect /usr/bin/rtlamr-collect

# I cannot for the life of me get `rtlamr | rtlamr-collect` to work as entry
# point to work around this limitation I created this small shell script that
# basically does it, and set that as the entry point which works.
ADD run.sh /usr/bin/run.sh
RUN chmod 777 /usr/bin/run.sh
ENTRYPOINT ["/usr/bin/run.sh"]

内容run.sh

#!/bin/sh
rtlamr | rtlamr-collect

我相信一个可以玩的简单测试用例可能是这样的:

dockerfile

FROM alpine:latest
ENTRYPOINT ["echo 'hello world' | grep ell"]

尝试运行此程序显示:

docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "echo 'hello world' | grep ell": executable file not found in $PATH: unknown.

但是如果你登录到 Docker Container Shell 并执行相同的命令,它将按预期工作:

/ # echo 'hello world' | grep ell
hello world

答案1

感谢 Docker Discord Server 中的 enchbladexp 帮助提出解决方案:

FROM golang:latest AS build
RUN CGO_ENABLED=0 GOOS=linux go install github.com/bemasher/rtlamr-collect@latest
RUN CGO_ENABLED=0 GOOS=linux go install github.com/bemasher/rtlamr@latest

# Use a multi-stage build to reduce our image size
FROM alpine:latest AS runtime
COPY --from=build /go/bin/rtlamr /usr/bin/rtlamr
COPY --from=build /go/bin/rtlamr-collect /usr/bin/rtlamr-collect

# Fully qualify the paths to the binaries
ENTRYPOINT ["/bin/sh", "-c", "/usr/bin/rtlamr | /usr/bin/rtlamr-collect"]

根本原因似乎是,无论出于什么原因,我必须完全限定二进制文件的路径,并提供/bin/sh入口点。

答案2

根据命令的复杂性,您可以将其嵌入如下Dockerfile内容:

FROM alpine:latest

RUN \
    echo "#!/bin/ash" > /entrypoint.sh &&\
    echo "echo 'hello world' | grep ell" >> /entrypoint.sh &&\
    chmod 755 /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

输出:

PS C:\Users\me\Desktop\test> docker build -t test ./
PS C:\Users\me\Desktop\test> docker run -it test:latest
hello world

如果您需要更复杂的东西,请告诉我,我会再次深入研究我以前的工作。

相关内容