From the course: Docker: Build and Optimize Docker Images
Building images using caching options - Docker Tutorial
From the course: Docker: Build and Optimize Docker Images
Building images using caching options
- [Instructor] Controlling docker's layer cache from the CLI gives you the flexibility to balance build speed, freshness, and disc usage. There are three essential commands every developer should master. First up, you can add the no cache option when you build an image using the docker build command. Then we tag the image with the image name and version, and of course we add a period representing the build context. This instructs docker to re-execute every docker file instruction from scratch, ignoring any existing cache layers. This is useful when you need to pick up the very latest updates or ensure that no stale layers linger. If you look at the output in the terminal, you'll see that there are five layers that need to be built. The first is the from instruction to set the base image. Next, we have work dur, which is cached even though we use the no cache option. This is because docker build will still verify and reuse metadata layers if nothing has changed. Even when you pass the no cache option, the rest of the layers should not be cached, so they're rebuilt from scratch. Even without skipping all caching, cache layers won't automatically incorporate updates to your base image. To ensure you're building atop the newest upstream image, we use our second essential command, which is docker build and the pull option. Then of course, the image tag and build context. The poll flag makes Docker fetch the latest version of each from image before starting the build. You can use this to ensure that your base image is up to date. Our base image is already up to date, so it won't be pulled again. You can also share cached layers. Sharing cache between local development and CI or between different hosts helps accelerate cold builds. To do this, we use our third and final command, type docker build, and then the cache from option. Then enter the image name and the previous version we want to reuse, which is 2.3.1. After that, enter the current image version, which is 2.3.2, and then add a period for the build context. Docker will compare each docker file step against the layers in the previous image version and reuse them when possible. In the output, you can see that the builder is importing the cache manifest from our previous image, and all layers are cached except for the from instruction. The from instruction in a docker file is not directly cached like other instructions because it defines the base image, which is the foundational layer of the image with the build options, no cache pull and cache from you gain full command line mastery over docker's cache keeping builds fast and up to date.