Creating a Dockerfile - docker starts from scratch

2019-07-05 04:09发布

问题:

I am trying to build a dockerfile - iteratively adding lines and testing. My understanding was that docker will cache the lines that have already been built and start from the new lines I had added. The case seems to be that it just builds from scratch each time I call build on my container. Is this normal? If not - what am I doing wrong?

回答1:

As demas said, if you're simply appending lines, the previous lines will be cached.

However, if anywhere in your Dockerfile you have a line like

ADD . /some/path

then Docker will assume that that line has changed even if it was only the Dockerfile that changed. So that line and anything after it will never be cached, unless nothing in the folder you're adding has changed.

You should be able to see whether this is happening by paying close attention to the output of the docker build command.

As a side note: a consequence of this is that if you're building a Dockerfile, you generally want to add the files in the directory as late as possible, doing any preparations beforehand. Of course, you will end up having to do things to your files (like some kind of build process) which is unfortunately hard to cache.



回答2:

If I understood correctly you can look at Docker as version control system where each line in your Dockerfile is a commit to the container.

If you add new line to your Dockerfile, Docker get the last revision of container and make a new commit. If you add line on the middle of your Dockerfile, Docker get one of the previous revisions and make new commit to this part of the tree.