Tuesday, August 29, 2023

Docker re-build image with cache

 Docker architecture


Dockerfile

When we re-build the image using the Dockerfile with same lines for the second and sub-subsequent builds without any change to the Dockerfile, it uses Cache and the build will be very quick



Step 2/3 and Step 3/3 used cache


Start the server


Lets add a line after RUN apk add --update redis line in the Dockerfile

Dockerfile look like this


It has a new line -> RUN apk add --update gcc

Lets re-build the image using docker build .


Now you see that Step 2/4 has used cache, Step 3/4 is a new step and built for the first time. Important to note here is Step 4/4 is also newly built

If we add a new step in the Dockerfile, the steps from that point will be newly built

Lets try to re-build again and see what happens with no changes to Dockerfile


Now Step 2/4, Step 3/4 and Step 4/4 are built using cache