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

标签 docker

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/

相关文章:

php - Docker Nginx 连接到私有(private) docker 网络上的 PHP-FPM

Docker:将容器内写入的日志文件发送到 ELK 堆栈

mongodb - Symfony 返回 "MongoId Class not found"错误

node.js - 生成可执行文件时 Docker-compose EACCESS 错误

php - Xdebug Docker跨容器通信设置

linux - Docker macvlan 中绑定(bind)地址

c# - .net核心无法作为独立的应用程序运行。在 docker 上

docker - 无法连接到 containerd

node.js - 如何在 Nginx Docker 镜像上使用 Traefik PathPrefix?

docker - 在容器中运行本地 kibana