faq,

工具 - Docker build 执行命令(Dockerfile RUN)没有任何输出

Read in English

问题现象

下面是 Dockerfile 内容:

1
2
3
4
5
6
FROM alpine

ADD . /app/
WORKDIR /app
RUN echo "hello world"
RUN ls -l

当使用新版本 Docker 执行 docker build 的时候,发现 RUN 命令没有任何输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
❯ docker build -t test -f Dockerfile .
[+] Building 1.0s (10/10) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                        0.0s
 => => transferring dockerfile: 112B                                                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                                                           0.0s
 => => transferring context: 2B                                                                                                                                             0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                            0.0s
 => [internal] load build context                                                                                                                                           0.0s
 => => transferring context: 107B                                                                                                                                           0.0s
 => CACHED [1/5] FROM docker.io/library/alpine                                                                                                                              0.0s
 => [2/5] ADD . /app/                                                                                                                                                       0.0s
 => [3/5] WORKDIR /app                                                                                                                                                      0.0s
 => [4/5] RUN echo "hello world"                                                                                                                                            0.4s
 => [5/5] RUN ls -l                                                                                                                                                         0.4s
 => exporting to image                                                                                                                                                      0.1s
 => => exporting layers                                                                                                                                                     0.0s
 => => writing image sha256:95911a7870dbd929ced4a031c65a180310a5480de759b19a5b08556cd443775b                                                                                0.0s
 => => naming to docker.io/library/test

这在构建镜像的时候,非常不利于调试

解决方法

docker build 命令前面增加 DOCKER_BUILDKIT=0,即可跟之前的 Docker 版本一样打印命令输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
DOCKER_BUILDKIT=0 docker build -t test -f Dockerfile .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM alpine
 ---> 389fef711851
Step 2/5 : ADD . /app/
 ---> 34cccaf2b826
Step 3/5 : WORKDIR /app
 ---> Running in 65eefb594656
Removing intermediate container 65eefb594656
 ---> 07582a62e423
Step 4/5 : RUN echo "hello world"
 ---> Running in bcd94b080379
hello world
Removing intermediate container bcd94b080379
 ---> 6fbc11007cff
Step 5/5 : RUN ls -l
 ---> Running in 8d9423839270
total 4
-rw-r--r--    1 root     root            70 Apr 29 03:07 Dockerfile
Removing intermediate container 8d9423839270
 ---> 7b6aee767fab
Successfully built 7b6aee767fab
Successfully tagged test:latest

参考链接


CatchZeng
Written by CatchZeng Follow
AI (Machine Learning) and DevOps enthusiast.