Dockerfile:如何将 RUN 命令的输出重定向到变量?

问题描述 投票:0回答:5

我正在编写一个 dockerfile,并希望将“ls”命令的输出放入变量中,如下所示:

$file = ls /tmp/dir

这里,“dir”里面只有一个文件。

dockerfile 中的以下 RUN 指令不起作用

RUN $file = ls /tmp/dir
docker dockerfile
5个回答
200
投票

您无法保存变量以供以后在其他

Dockerfile
命令中使用(如果这是您的意图)。这是因为每个
RUN
都发生在新的 shell 中。

但是,如果您只想捕获

ls
的输出,您应该能够通过一个
RUN
复合命令来完成此操作。例如:

RUN file="$(ls -1 /tmp/dir)" && echo $file

或者只使用内联子shell:

RUN echo $(ls -1 /tmp/dir)

如果您有实际的错误或问题需要解决,我可以对此进行扩展,而不是假设的答案。

演示这一点的完整示例

Dockerfile
是:

FROM alpine:3.7
RUN mkdir -p /tmp/dir && touch /tmp/dir/file1 /tmp//dir/file2
RUN file="$(ls -1 /tmp/dir)" && echo $file
RUN echo $(ls -1 /tmp/dir)

构建时,您应该看到步骤 3 和 4 输出变量(其中包含步骤 2 中创建的

file1
file2
的列表)。
--progress plain
的选项强制输出显示 Docker 更高版本中的步骤:

$ docker build --no-cache --progress plain -t test .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM alpine:3.7
 ---> 3fd9065eaf02
Step 2/4 : RUN mkdir -p /tmp/dir && touch /tmp/dir/file1 /tmp//dir/file2
 ---> Running in abb2fe683e82
Removing intermediate container abb2fe683e82
 ---> 2f6dfca9385c
Step 3/4 : RUN file="$(ls -1 /tmp/dir)" && echo $file
 ---> Running in 060a285e3d8a
file1 file2
Removing intermediate container 060a285e3d8a
 ---> 2e4cc2873b8c
Step 4/4 : RUN echo $(ls -1 /tmp/dir)
 ---> Running in 528fc5d6c721
file1 file2
Removing intermediate container 528fc5d6c721
 ---> 1be7c54e1f29
Successfully built 1be7c54e1f29
Successfully tagged test:latest

74
投票

只需突出显示评论中给出的答案,如果您使用的是现代版本的 Docker(在我的例子中为 v20.10.5)并且日志没有显示预期的输出,例如,当您运行时,这可能是正确的答案

RUN ls

您应该在

--progress <string>
命令中使用选项
docker build

 --progress string         Set type of progress output (auto, plain, tty). Use plain to show container output
                            (default "auto")

例如:

docker build --progress=plain .

在最新版本的docker中,docker自带的经典构建引擎已经升级为Buildkit,它会显示不同的信息。

您应该看到如下输出:

#12 [8/8] RUN ls -alh
#12 sha256:a8cf7b9a7b1f3dc25e3a97700d4cc3d3794862437a5fe2e39683ab229474746c
#12 0.174 total 184K
#12 0.174 drwxr-xr-x    1 root     root        4.0K Mar 28 19:37 .
#12 0.174 drwxr-xr-x    1 root     root        4.0K Mar 28 19:35 ..
#12 0.174 drwxr-xr-x  374 root     root       12.0K Mar 28 19:37 node_modules
#12 0.174 -rw-r--r--    1 root     root        1.1K Mar 28 19:36 package.json
#12 0.174 -rw-r--r--    1 root     root         614 Mar 28 15:48 server.js
#12 0.174 -rw-r--r--    1 root     root      149.5K Mar 28 16:54 yarn.lock
#12 DONE 0.2s

正如 @venimus 在评论中指出的,您可能还需要

--no-cache
,因为缓存的容器不显示任何输出。

相关问题,了解更多详情。


6
投票

docker compose 替代:

docker build --progress=plain .

BUILDKIT_PROGRESS=plain docker compose build

或在 compose.yml 文件中

services:
    build:
        progress: plain

3
投票

我无法让 Andy(或任何其他)的方法在 Dockerfile 本身中工作,因此我将 Dockerfile 入口点设置为包含以下内容的 bash 文件:

#!/bin/bash
file="$(conda list --explicit)" && echo $file
echo $(conda list --explicit)

注意第二种方法不会渲染换行符,所以我发现第一种方法 - 通过

$file
变量进行回显 - 优越。


2
投票

另一个选项是将结果保存在临时文件中,并在后续的 RUN 语句中使用它,甚至使用 COPY --from 命令将其复制到另一个步骤:

这是一个例子:

FROM alpine:3.17.0
WORKDIR /output

# here we can store result of any command
# or even store multiple variables in a same file
RUN echo "2 + 3" >> /output/temp_file

# will print 5, almost anything can be retrieved from a
# text file in bash and be used in following commands
RUN echo $(( $(cat /output/temp_file) ))

CMD tail -f /dev/null
© www.soinside.com 2019 - 2024. All rights reserved.