11---
22title : Optimize cache usage in builds
3- description : An overview on how to optimize cache utilization in Docker builds.
3+ description : An overview of how to optimize cache utilization in Docker builds.
44keywords : build, buildkit, buildx, guide, tutorial, mounts, cache mounts, bind mounts
55aliases :
66 - /build/guide/mounts/
77---
88
99When building with Docker, a layer is reused from the build cache if the
10- instruction and the files it depends on hasn 't changed since it was previously
10+ instruction and the files it depends on haven 't changed since it was previously
1111built. Reusing layers from the cache speeds up the build process because Docker
1212doesn't have to rebuild the layer again.
1313
1414Here are a few techniques you can use to optimize build caching and speed up
1515the build process:
1616
1717- [ Order your layers] ( #order-your-layers ) : Putting the commands in your
18- Dockerfile into a logical order can help you avoid unnecessary cache
18+ Dockerfile in a logical order can help you avoid unnecessary cache
1919 invalidation.
2020- [ Keep the context small] ( #keep-the-context-small ) : The context is the set of
2121 files and directories that are sent to the builder to process a build
@@ -32,7 +32,7 @@ the build process:
3232 a package manager. Having a persistent cache for packages means that even if
3333 you rebuild a layer, you only download new or changed packages.
3434- [ Use an external cache] ( #use-an-external-cache ) : An external cache lets you
35- store build cache at a remote location. The external cache image can be
35+ store the build cache at a remote location. The external cache image can be
3636 shared between multiple builds, and across different environments.
3737
3838## Order your layers
@@ -59,7 +59,7 @@ This Dockerfile is rather inefficient. Updating any file causes a reinstall of
5959all dependencies every time you build the Docker image even if the dependencies
6060didn't change since last time.
6161
62- Instead, the ` COPY ` command can be split in two. First, copy over the package
62+ Instead, the ` COPY ` command can be split into two. First, copy over the package
6363management files (in this case, ` package.json ` and ` yarn.lock ` ). Then, install
6464the dependencies. Finally, copy over the project source code, which is subject
6565to frequent change.
@@ -92,7 +92,7 @@ node_modules
9292tmp*
9393```
9494
95- Ignore- rules specified in the ` .dockerignore ` file apply to the entire build
95+ Ignore rules specified in the ` .dockerignore ` file apply to the entire build
9696context, including subdirectories. This means it's a rather coarse-grained
9797mechanism, but it's a good way to exclude files and directories that you know
9898you don't need in the build context, such as temporary files, log files, and
@@ -127,7 +127,7 @@ instruction is done executing, the mounted files are not persisted in the final
127127image, or in the build cache. Only the output of the ` go build ` command
128128remains.
129129
130- The ` COPY ` and ` ADD ` instructions in a Dockerfile lets you copy files from the
130+ The ` COPY ` and ` ADD ` instructions in a Dockerfile let you copy files from the
131131build context into the build container. Using bind mounts is beneficial for
132132build cache optimization because you're not adding unnecessary layers to the
133133cache. If you have build context that's on the larger side, and it's only used
@@ -341,7 +341,7 @@ jobs:
341341 cache-to : type=registry,ref=user/app:buildcache,mode=max
342342` ` `
343343
344- This setup tells BuildKit to look for cache in the ` user/app:buildcache` image.
344+ This setup tells BuildKit to look for the cache in the ` user/app:buildcache` image.
345345And when the build is done, the new build cache is pushed to the same image,
346346overwriting the old cache.
347347
0 commit comments