mea*_*our 29 docker dockerfile
我正在编写一个dockerfile,并希望将"ls"命令的输出放入变量中,如下所示:
$file = ls /tmp/dir
Run Code Online (Sandbox Code Playgroud)
这里,"dir"里面只有一个文件.
dockerfile中的以下RUN指令不起作用
RUN $file = ls /tmp/dir
Run Code Online (Sandbox Code Playgroud)
Pab*_* EM 72
只需突出显示评论中给出的答案,如果您使用的是现代版本的 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")
Run Code Online (Sandbox Code Playgroud)
例如:
docker build --progress=plain .
Run Code Online (Sandbox Code Playgroud)
在最新版本的 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
Run Code Online (Sandbox Code Playgroud)
正如 @venimus 在评论中指出的,您可能还需要,--no-cache因为缓存的容器不显示任何输出。
And*_*inn 49
您无法保存变量以供以后在其他Dockerfile命令中使用(如果这是您的意图).这是因为每个都RUN发生在一个新的shell中.
但是,如果您只想捕获输出,则ls应该能够在一个RUN复合命令中执行此操作.例如:
RUN file="$(ls -1 /tmp/dir)" && echo $file
Run Code Online (Sandbox Code Playgroud)
或者只使用子shell内联:
RUN echo $(ls -1 /tmp/dir)
Run Code Online (Sandbox Code Playgroud)
希望这有助于您的理解.如果您有实际的错误或问题要解决,我可以扩展这个而不是假设的答案.
一个完整的例子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)
Run Code Online (Sandbox Code Playgroud)
在构建时,您应该看到步骤3和4输出变量(其中包含步骤2 中的列表file1和file2创建):
$ docker build --no-cache -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
Run Code Online (Sandbox Code Playgroud)
docker compose替代:
docker build --progress=plain .
Run Code Online (Sandbox Code Playgroud)
是
BUILDKIT_PROGRESS=plain docker compose build
Run Code Online (Sandbox Code Playgroud)
或者在 compose.yml 文件中
services:
build:
progress: plain
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36284 次 |
| 最近记录: |