“Caching” intermediate Docker build

I’m learning to use Docker and I’ve come across a minor annoyance. Whenever I make a change to the Dockerfile,I run docker build -t tag . which goes through the entire Dockerfile as it should. This takes a good 5-6 minutes due to the dependencies in my project. Sometimes a command that I run will cause an error, or there will be a mistake in the Dockerfile. While the fix may take a couple seconds, I have to rebuild the entire thing which decreases my productivity. Is there a way to “continue from where the build last failed” after editing the Dockerfile? Thanks.

  • Build many dockerfiles on jenkins project
  • How to deploy a Nodejs application in a docker container
  • Slow meteor build performance in docker container
  • Docker image is not listed though I built using maven docker plugin
  • How to download base docker image for creating hello world docker image?
  • Provide static IP to docker containers via docker-compose
  • How to setup a simple reverse proxy in docker?
  • Docker, mount volumes as readonly
  • Can I clean /var/lib/docker/tmp?
  • Restart failed Selenium browser
  • Sharing directories in a Docker container both with a Dockerfile and after the container is running
  • Run app inside docker container as non-root user with capabilities
  • 2 Solutions collect form web for ““Caching” intermediate Docker build”

    This is called the “build cache” and it is already a feature of Docker. Docker’s builder will only use the cache up until the point where your Dockerfile has changed. There are some edge cases when using COPY or ADD directives that will cause the build cache to be invalidated (since it hashes files to determine if any have changed, and invalidates the cache if so). This means that if you are using COPY foo /foo and you have changed that file, the build cache will be invalidated. Also, if you do COPY . /opt/bar/ (meaning, you copy the entire directory to somewhere), even some small change like a Vim swap file or Dockerfile change will invalidate the cache!

    The behavior of not using the build cache at all is invoked using --no-cache in your docker build command.

    So basically, it’s there, and you’re using it, just that you’re probably changing the Dockerfile at a very early point or hitting that lesser known edge case with a COPY/ADD directive, and the builder is invalidating everything after that point. And just to answer the question before you ask it, it would be very hard or impossible to continue using the cache after a change has invalidated the cache. Meaning, if you change your first Dockerfile line and invalidate the build cache, it is basically impossible to use the build cache past that point.

    Is there a way to “continue from where the build last failed” after editing the Dockerfile?

    No (as L0j1k’s answer explains well)

    That is why the best practice is to organize your Dockerfile from the stablest commands (the one which will never have to be changed/modified) to the most specific commands (the ones you might have to change quite a bit).

    That way, your modifications will trigger only a build on the last few lines of your Dockerfile, instead of going through everything again, because you changed one of the first lines.

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