Member-only story
Docker — Container Image Caching
Docker caches existing images and reuses them when building new images. During the build process, if a certain image layer already exists, it will be used directly without recreating it.
Image Caching
Let’s take a look at the following example:
FROM ubuntu
RUN apt-get update && apt-get install -y vim
COPY testfile /
Build output:
$ docker build -t ububtu-with-vim .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM ubuntu
---> 1f6ddc1b2547
Step 2/3 : RUN apt-get update && apt-get install -y vim
---> Using cache
---> ff719f2880f9
Step 3/3 : COPY testfile /
---> 0f3ffa9582cd
Successfully built 0f3ffa9582cd
Successfully tagged ububtu-with-vim:latest
Pay attention to this line:
Step 2/3 : RUN apt-get update && apt-get install -y vim
---> Using cache
Since the same RUN the command has been executed before, this time it is used directly. Then the process is to start the temporary container, copy the testfile
, submit the new image layer, and delete the temporary container.
If you run the same docker build command with the same Dockerfile , the output will look like this: