Why does docker build not use the build cache?

Every time I change my Dockerfile, docker re-runs all of my commands even if I change something at the end of the file. The commands are not cached.

  • Manage a cassandra database on docker with opscenter
  • Using a Dockerfile for testing in TeamCity
  • Link nodejs app to Rethinkdb from another container
  • Use of docker tomcat with external WAR
  • PDOException SQLSTATE[HY000] [2002] No such file or directory. Whilst running laravel via isolated docker containers
  • Kubernetes on CoreOS: Proxy service max out CPU
  • Docker image for sailsjs development on macosx hangs
  • Changing permissions of added file to a Docker volume
  • AWS Discovery only tries 3 ports 5701, 5702 & 5703 while joining cluster
  • docker container exits because of “std in is not a tty”
  • how to map a local folder as the volume the docker container or image?
  • Docker : /var/run/docker.sock: no such file or directory
  • One Solution collect form web for “Why does docker build not use the build cache?”

    In this particular case the image my image was based on contained this statement

    ONBUILD ADD . /some/path

    That means, no statement in my Dockerfile can be cached when my Dockerfile changes, because the ADD . /some/path is executed just before any of my statements are executed.

    In general: if there is an ADD . /some/path statement in the Dockerfile no statement after that statement can be cached, because the change to the Dockerfile invalidates the cache.

    My solution was to put the files I want to add into a subdirectory my_data and then add the content of the subdirectory to the path:

    ADD my_data /some/path

    Unfortunately, adding the Dockerfile to the .dockerignore does not help, because then the docker build cannot execute because it does not find the Dockerfile and you get the error Dockerfile was excluded by .dockerignore pattern 'Dockerfile'

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.