Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker build is not using cache

docker build is not using it's cache.

docker build -f Dockerfile . 

generates the same output that this does:

docker build -f Dockerfile --no-cache . 

I am modifying the Dockerfile, adding commands at the end of the file. So the previous layers should be cached and valid.

I've got plenty of disk space.

Any ideas?

Docker version 17.06.1-ce, build 874a737

Dockerfile:

FROM registry:5000/base/python:xenial  RUN mkdir /code COPY . /code  RUN apt-get update && \     apt-get install -y \     vim \     less  COPY /etc/foo /etc/foo  ENTRYPOINT ["/bin/bash"] 
like image 410
Mike Muldoon Avatar asked Aug 29 '17 00:08

Mike Muldoon


People also ask

Does docker build cache?

Docker's build-cache is a handy feature. It speeds up Docker builds due to reusing previously created layers. You can use the --no-cache option to disable caching or use a custom Docker build argument to enforce rebuilding from a certain step.

What is -- no cache in docker build?

An engineer can run a Docker build with the '–no-cache' option, which completely ignores all cache and thus makes every build take as much time as the first. That approach is clearly binary – the cache is used or not used.

How do I create a Dockerfile without cache?

When you use the Docker build command to build a Docker image, you can simply use the --no-cache option which will allow you to instruct daemon to not look for already existing image layers and simply force clean build of an image.

Does docker pull cache?

Pulling cached images After you configure the Docker daemon to use the Container Registry cache, Docker performs the following steps when you pull a public Docker Hub image with a docker pull command: The Docker daemon checks the Container Registry cache and fetches the images if it exists.


1 Answers

From your Dockerfile, if you append lines to your Dockerfile, or change the code being built, there's only one line that could be cached:

RUN mkdir /code 

After that, you perform a:

COPY . /code 

Since you have changed your Dockerfile, the contents of . have changed (the Dockerfile is part of .) and therefore the COPY needs to be performed again, generating a new layer. Once you generate a new layer, every following layer no longer has a cache and needs to be rebuild.

To improve caching, consider placing the lines that change less towards the top of your Dockerfile. That would leave the COPY . /code line at the very end of the file since it will change almost every time.

You should also include files you don't need in a .dockerignore file to avoid their changes breaking the cache. E.g. the .dockerignore could contain:

Dockerfile .git 

Or I tend to use an inverse file, ignore everything, and then reinclude the specific files I need to build my app:

* !Makefile !app/ 

The above would include only the Makefile and app directory when doing a COPY . /code, and everything else would be ignored.

like image 71
BMitch Avatar answered Sep 22 '22 10:09

BMitch