Docker是否构建--no-cache实际上下载并刷新基本映像?

时间:2018-10-05 11:33:31

标签: docker

docker是否构建--no-cache刷新已更新远程基础映像?文档似乎没有指定。

4 个答案:

答案 0 :(得分:3)

--no-cache选项将重建图像,而无需使用本地缓存的图层。但是,FROM行将重用已经拉出的基本映像(如果它已存在于构建主机上)(from行本身可能不会被缓存,但是它拉出的映像是已缓存的)。如果要再次拉出基本映像,可以在构建命令中使用--pull选项。例如

$ docker build --no-cache --pull -t new-image-name:latest .

要查看构建命令采用的所有选项,可以运行

$ docker build --help

或查看https://docs.docker.com/engine/reference/commandline/build/

上的文档

以下是您自己可以测试此行为的示例:

$ # very simple Dockerfile
$ cat df.test
FROM busybox:latest
RUN echo hello >test.txt

$ # pull an older version of busybox
$ docker pull busybox:1.29.2
1.29.2: Pulling from library/busybox
8c5a7da1afbc: Pull complete
Digest: sha256:cb63aa0641a885f54de20f61d152187419e8f6b159ed11a251a09d115fdff9bd
Status: Downloaded newer image for busybox:1.29.2

$ # retag that locally as latest
$ docker tag busybox:1.29.2 busybox:latest

$ # run the build, note the image id at the end of each build step
$ DOCKER_BUILDKIT=0 docker build --no-cache -f df.test .
Sending build context to Docker daemon  23.04kB
Step 1/2 : FROM busybox:latest
 ---> e1ddd7948a1c
Step 2/2 : RUN echo hello >test.txt
 ---> Running in dba83fef49f9
Removing intermediate container dba83fef49f9
 ---> 1f824ff05612
Successfully built 1f824ff05612

$ # rerun the build, note step 1 keeps the same id and never pulled a new latest
$ DOCKER_BUILDKIT=0 docker build --no-cache -f df.test .
Sending build context to Docker daemon  23.04kB
Step 1/2 : FROM busybox:latest
 ---> e1ddd7948a1c
Step 2/2 : RUN echo hello >test.txt
 ---> Running in 73df884b0f48
Removing intermediate container 73df884b0f48
 ---> e5870de6c24f
Successfully built e5870de6c24f

$ # run with --pull and see docker update the latest image, new container id from step 1
$ DOCKER_BUILDKIT=0 docker build --no-cache --pull -f df.test .
Sending build context to Docker daemon  23.04kB
Step 1/2 : FROM busybox:latest
latest: Pulling from library/busybox
Digest: sha256:2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812
Status: Downloaded newer image for busybox:latest
 ---> 59788edf1f3e
Step 2/2 : RUN echo hello >test.txt
 ---> Running in 7204116ecbf4
Removing intermediate container 7204116ecbf4
 ---> 2c6d8c48661b
Successfully built 2c6d8c48661b

$ # one last run now that busybox:latest is updated shows the pull has nothing to do
$ DOCKER_BUILDKIT=0 docker build --no-cache --pull -f df.test .
Sending build context to Docker daemon  23.04kB
Step 1/2 : FROM busybox:latest
latest: Pulling from library/busybox
Digest: sha256:2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812
Status: Image is up to date for busybox:latest
 ---> 59788edf1f3e
Step 2/2 : RUN echo hello >test.txt
 ---> Running in f37e19024e99
Removing intermediate container f37e19024e99
 ---> 044a5d4011c4
Successfully built 044a5d4011c4

答案 1 :(得分:2)

docker build --no-cache将在不重复使用缓存的层的情况下重建整个映像,但不会从远程存储库中提取最新的基本映像。它将仅使用您本地存储的图像。

答案 2 :(得分:0)

-no-cache在不使用缓存的情况下重建映像,因此它本质上是干净的构建。

按照help docker build --help --no-cache构建映像时不使用缓存

答案 3 :(得分:0)

我们在docker文件中指定的每个dockerfile命令(例如RUN,CMD,ADD)都会在本地系统中创建图层,并且其他Docker映像也会使用此图层,前提是它们也使用具有相同参数的相同dockerfile命令。

当我们使用“ --no-cache”参数指定docker build时,docker将忽略正在构建docker的本地系统中已经可用的本地系统docker映像层,并且它始终以全新的构建方式开始构建或从头开始以及上一层的引用计数(如果有);构建新图像图层时不会添加。

您可以通过以下链接找到图像层 Finding the layers and layer sizes for each Docker image

相关问题