Closes #3636
Signed-off-by: Doug Davis <dug@us.ibm.com>
| ... | ... |
@@ -75,6 +75,43 @@ Here’s an example from the [`buildpack-deps` image](https://github.com/docker- |
| 75 | 75 |
mercurial \ |
| 76 | 76 |
subversion |
| 77 | 77 |
|
| 78 |
+### Build Cache |
|
| 79 |
+ |
|
| 80 |
+During the process of building an image Docker will step through the |
|
| 81 |
+instructions in your `Dockerfile` executing each in the order specified. |
|
| 82 |
+As each instruction is examined Docker will look for an existing image in its |
|
| 83 |
+cache that it can reuse, rather than creating a new (duplicate) image. |
|
| 84 |
+If you do not want to use the cache at all you can use the ` --no-cache=true` |
|
| 85 |
+option on the `docker build` command. |
|
| 86 |
+ |
|
| 87 |
+However, if you do let Docker use its cache then it is very important to |
|
| 88 |
+understand when it will, and will not, find a matching image. The basic rules |
|
| 89 |
+that Docker will follow are outlined below: |
|
| 90 |
+ |
|
| 91 |
+* Starting with a base image that is already in the cache, the next |
|
| 92 |
+instruction is compared against all child images derived from that base |
|
| 93 |
+image to see if one of them was built using the exact same instruction. If |
|
| 94 |
+not, the cache is invalidated. |
|
| 95 |
+ |
|
| 96 |
+* In most cases simply comparing the instruction in the `Dockerfile` with one |
|
| 97 |
+of the child images is sufficient. However, certain instructions require |
|
| 98 |
+a little more examination and explanation. |
|
| 99 |
+ |
|
| 100 |
+* In the case of the `ADD` and `COPY` instructions, the contents of the file(s) |
|
| 101 |
+being put into the image are examined. Specifically, a checksum is done |
|
| 102 |
+of the file(s) and then that checksum is used during the cache lookup. |
|
| 103 |
+If anything has changed in the file(s), including its metadata, |
|
| 104 |
+then the cache is invalidated. |
|
| 105 |
+ |
|
| 106 |
+* Aside from the `ADD` and `COPY` commands cache checking will not look at the |
|
| 107 |
+files in the container to determine a cache match. For example, when processing |
|
| 108 |
+a `RUN apt-get -y update` command the files updated in the container |
|
| 109 |
+will not be examined to determine if a cache hit exists. In that case just |
|
| 110 |
+the command string itself will be used to find a match. |
|
| 111 |
+ |
|
| 112 |
+Once the cache is invalidated, all subsequent `Dockerfile` commands will |
|
| 113 |
+generate new images and the cache will not be used. |
|
| 114 |
+ |
|
| 78 | 115 |
## The `Dockerfile` instructions |
| 79 | 116 |
|
| 80 | 117 |
This section contains specific recommendations for the correct usage of the |
| ... | ... |
@@ -336,4 +373,5 @@ These Official Repos have exemplary `Dockerfile`s: |
| 336 | 336 |
* [Dockerfile Reference](https://docs.docker.com/reference/builder/#onbuild) |
| 337 | 337 |
* [More about Base Images](https://docs.docker.com/articles/baseimages/) |
| 338 | 338 |
* [More about Automated Builds](https://docs.docker.com/docker-hub/builds/) |
| 339 |
-* [Guidelines for Creating Official Repositories](https://docs.docker.com/docker-hub/official_repos/) |
|
| 340 | 339 |
\ No newline at end of file |
| 340 |
+* [Guidelines for Creating Official |
|
| 341 |
+Repositories](https://docs.docker.com/docker-hub/official_repos/) |
| ... | ... |
@@ -13,8 +13,8 @@ successively. |
| 13 | 13 |
|
| 14 | 14 |
This page discusses the specifics of all the instructions you can use in your |
| 15 | 15 |
`Dockerfile`. To further help you write a clear, readable, maintainable |
| 16 |
-`Dockerfile`, we've also written a [`Dockerfile` Best Practices guide](/articles/dockerfile_best-practices). |
|
| 17 |
- |
|
| 16 |
+`Dockerfile`, we've also written a [`Dockerfile` Best Practices |
|
| 17 |
+guide](/articles/dockerfile_best-practices). |
|
| 18 | 18 |
|
| 19 | 19 |
## Usage |
| 20 | 20 |
|
| ... | ... |
@@ -60,7 +60,9 @@ to be created - so `RUN cd /tmp` will not have any effect on the next |
| 60 | 60 |
instructions. |
| 61 | 61 |
|
| 62 | 62 |
Whenever possible, Docker will re-use the intermediate images, |
| 63 |
-accelerating `docker build` significantly (indicated by `Using cache`): |
|
| 63 |
+accelerating `docker build` significantly (indicated by `Using cache` - |
|
| 64 |
+see the [`Dockerfile` Best Practices |
|
| 65 |
+guide](/articles/dockerfile_best-practices/#build-cache) for more information): |
|
| 64 | 66 |
|
| 65 | 67 |
$ sudo docker build -t SvenDowideit/ambassador . |
| 66 | 68 |
Uploading context 10.24 kB |
| ... | ... |
@@ -192,10 +194,13 @@ commands using a base image that does not contain `/bin/sh`. |
| 192 | 192 |
> you must use double-quotes (") around words not single-quotes (').
|
| 193 | 193 |
|
| 194 | 194 |
The cache for `RUN` instructions isn't invalidated automatically during |
| 195 |
-the next build. The cache for an instruction like `RUN apt-get |
|
| 196 |
-dist-upgrade -y` will be reused during the next build. The cache for |
|
| 197 |
-`RUN` instructions can be invalidated by using the `--no-cache` flag, |
|
| 198 |
-for example `docker build --no-cache`. |
|
| 195 |
+the next build. The cache for an instruction like |
|
| 196 |
+`RUN apt-get dist-upgrade -y` will be reused during the next build. The |
|
| 197 |
+cache for `RUN` instructions can be invalidated by using the `--no-cache` |
|
| 198 |
+flag, for example `docker build --no-cache`. |
|
| 199 |
+ |
|
| 200 |
+See the [`Dockerfile` Best Practices |
|
| 201 |
+guide](/articles/dockerfile_best-practices/#build-cache) for more information. |
|
| 199 | 202 |
|
| 200 | 203 |
The cache for `RUN` instructions can be invalidated by `ADD` instructions. See |
| 201 | 204 |
[below](#add) for details. |
| ... | ... |
@@ -325,6 +330,9 @@ have permissions of 600. |
| 325 | 325 |
> The first encountered `ADD` instruction will invalidate the cache for all |
| 326 | 326 |
> following instructions from the Dockerfile if the contents of `<src>` have |
| 327 | 327 |
> changed. This includes invalidating the cache for `RUN` instructions. |
| 328 |
+> See the [`Dockerfile` Best Practices |
|
| 329 |
+guide](/articles/dockerfile_best-practices/#build-cache) for more information. |
|
| 330 |
+ |
|
| 328 | 331 |
|
| 329 | 332 |
The copy obeys the following rules: |
| 330 | 333 |
|