11

Does docker build --no-cache refresh updated remote base images or not? Documentation does not seem to specify.

Sentinel
  • 3,582
  • 1
  • 30
  • 44
  • I don't know the answer, but you can easily test this on your own. Create a simple base image, push this to a repo and use it in your dockerfile. Update the base image and build your dockerfile again. – Odyssee Oct 05 '18 at 11:57
  • @IlyasDeckers Thanks Ilyas but for various reasons I can't do this right now. If you want to test and let me know results, I will upvote and mark as answer. – Sentinel Oct 05 '18 at 12:09

4 Answers4

23

The --no-cache option will rebuild the image without using the local cached layers. However, the FROM line will reuse the already pulled base image if it exists on the build host (the from line itself may not be cached, but the image it pulls is). If you want to pull the base image again, you can use the --pull option to the build command. E.g.

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

To see all the options the build command takes, you can run

$ docker build --help

or see the documentation at https://docs.docker.com/engine/reference/commandline/build/


Here's an example for how you can test this behavior yourself:

$ # 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
BMitch
  • 231,797
  • 42
  • 475
  • 450
8

docker build --no-cache will rebuild your whole image without reusing cached layers but it will not pull the newest base image from the remote repository. It will just use your local stored image.

lvthillo
  • 28,263
  • 13
  • 94
  • 127
0

--no-cache rebuilds image without using the cache, so it is essentially a clean build.

as per the help docker build --help --no-cache Do not use cache when building the image

  • Sure, but then when you rebuild the local image, it will use the OLD cached image, right? Because the previous --no-cache did not PULL the new image, right? – Sentinel Oct 05 '18 at 12:12
0

Each dockerfile commands that we specify in the docker file like RUN, CMD, ADD create layers in your local system and this layers will be used by other docker images provided they too make use of same dockerfile command with same parameters.

When we specify docker build with "--no-cache" parameter then docker will ignore the local system docker image layers that were already available in the local system where you are building the docker and it always start the build as fresh build or from scratch and the reference count for the previous layer, if any; won't be added while building this new image layer.

You can find the layers of image by following this link Finding the layers and layer sizes for each Docker image

Viswesn
  • 4,674
  • 2
  • 28
  • 45
  • 1
    If the base image (specified by the FROM command) is available and we run the build with `--no-cache`. Then will the cached base image be used or fresh copy will be downloaded again? – variable May 08 '20 at 06:03
  • @variable you need to also include `--pull`. See the accepted answer. – badsyntax Jan 06 '21 at 13:04