Creating a Dockerfile – docker starts from scratch on each new build

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?

  • Optimising cargo build times in docker
  • Link Docker containers running on different hosts?
  • Nexus 3 Docker container and Backup
  • Canonical way to checksum downloads in a Dockerfile?
  • Communication between docker containers in differente networks
  • How to change the default location for “docker create volume” command?
  • docker-credential-gcr not found inside Docker image although executable is there
  • Communication between multiple docker-compose projects
  • Remote Docker and volume binding
  • Unable to run cygwin in Windows Docker Container
  • Cannot link to a running container started by docker-compose
  • Docker can't find a script that I COPY to an image when I try to invoke it twice via / && / (but once works!)
  • 2 Solutions collect form web for “Creating a Dockerfile – docker starts from scratch on each new build”

    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.

    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.

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