重定向 Dockerfile 中 运行 命令的 STDOUT

Redirect STDOUT of a RUN command in a Dockerfile

我需要在 Docker 构建会话期间将两个文件连接在一起。然后将在后续步骤中在容器中访问串联文件。

当我尝试在 Docker 文件中使用它时:

COPY ./a /tmp
COPY ./b /tmp
RUN ["ls", "/tmp"]
RUN ["cat", "/tmp/a", "/tmp/b", ">/tmp/c"]
RUN ["ls", "/tmp"]

然后尝试构建容器我收到此错误:

Step 3/13 : COPY ./a /tmp
 ---> Using cache
 ---> 253aad0eae1a
Step 4/13 : COPY ./b /tmp
 ---> Using cache
 ---> 0f86ea5731bf
Step 5/13 : RUN ["ls", "/tmp"]
 ---> Using cache
 ---> f9aae6a9e657
Step 6/13 : RUN ["cat", "/tmp/a", "/tmp/b", ">/tmp/c"]
 ---> Running in 3aed3ca981ab
Tue Oct  5 16:29:32 EDT 2021
Tue Oct  5 16:29:33 EDT 2021
cat: >/tmp/c: No such file or directory
The command 'cat /tmp/a /tmp/b >/tmp/c' returned a non-zero code: 1

如何使用 运行 命令重定向 STDOUT?

我可以使用另一个接受输出文件名作为参数的命令吗?

我知道我可以创建一个小脚本并将其添加到容器中,然后 运行 该脚本,但我希望能够在 Docker 文件中执行此操作。

您正在使用 RUNexec 来自 RUN ["executable", "param1", "param2"] 其中 shell 扩展变量、子命令、管道输出、将命令链接在一起等功能将不起作用,请使用 shell 形式的 RUN 将命令作为一个字符串输入 RUN <command>

COPY ./a /tmp
COPY ./b /tmp
RUN ls /tmp
RUN cat /tmp/a /tmp/b > /tmp/c
RUN ls /tmp