Docker build is not using cache

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)