Building images without using cached layers
By default, when we build an image, Docker will try to use the cached layers so that it takes less time to build. However, at times, it is necessary to build from scratch. For example, you will need to force a system update, such as the yum -y
update. Let's see how we can do that in this recipe.
Getting ready
Get a Dockerfile to build the image. For this example, we will be using the following Dockerfile, which installs the nginx
web server in an Alpine Linux container:
FROM alpine:3.8 RUN apk add --update nginx && mkdir /tmp/nginx && rm -rf /var/cache/apk/* EXPOSE 80 443 CMD ["nginx", "-g", "daemon off;"]
How to do it...
While building the image, pass the -no-cache
option as follows:
$ docker image build -t test --no-cache - < Dockerfile

How it works...
The --no-cache
option will discard any cached layer and build one Dockerfile from scratch.
There's more...
Sometimes, we also want to discard the cache after a few instructions...