1

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.

1

1 Answer 1

3

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'

Sign up to request clarification or add additional context in comments.

3 Comments

Would you share why you are using ONBUILD? I've never seen a good example of this in the wild. Thanks!
@seanmcl It is in an image that comes form docker: github.com/docker-library/python/blob/… see also registry.hub.docker.com/_/python This allows to just put your python file next to the Dockerfile and you run the app.
Thanks so much. You saved me from hours of pointless rebuilding because my ADD statement was the first line in the Dockerfile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.