gpt4 book ai didi

docker - Docker会建立--no-cache实际下载并刷新基本镜像吗?

转载 作者:行者123 更新时间:2023-12-02 17:38:06 26 4
gpt4 key购买 nike

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

最佳答案

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

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

要查看build命令采用的所有选项,可以运行
$ 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

关于docker - Docker会建立--no-cache实际下载并刷新基本镜像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52664744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com