Signed-off-by: mikelinjie <294893458@qq.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -1174,8 +1174,10 @@ To use these, simply pass them on the command line using the `--build-arg |
| 1174 | 1174 |
`ARG` variables are not persisted into the built image as `ENV` variables are. |
| 1175 | 1175 |
However, `ARG` variables do impact the build cache in similar ways. If a |
| 1176 | 1176 |
Dockerfile defines an `ARG` variable whose value is different from a previous |
| 1177 |
-build, then a "cache miss" occurs upon its first usage, not its declaration. |
|
| 1178 |
-For example, consider this Dockerfile: |
|
| 1177 |
+build, then a "cache miss" occurs upon first use of the `ARG` variable. The |
|
| 1178 |
+declaration of the `ARG` variable does not count as a use. |
|
| 1179 |
+ |
|
| 1180 |
+For example, consider these two Dockerfile: |
|
| 1179 | 1181 |
|
| 1180 | 1182 |
``` |
| 1181 | 1183 |
1 FROM ubuntu |
| ... | ... |
@@ -1183,12 +1185,17 @@ For example, consider this Dockerfile: |
| 1183 | 1183 |
3 RUN echo $CONT_IMG_VER |
| 1184 | 1184 |
``` |
| 1185 | 1185 |
|
| 1186 |
-If you specify `--build-arg CONT_IMG_VER=<value>` on the command line the |
|
| 1187 |
-specification on line 2 does not cause a cache miss; line 3 does cause a cache |
|
| 1188 |
-miss. The definition on line 2 has no impact on the resulting image. The `RUN` |
|
| 1189 |
-on line 3 executes a command and in doing so defines a set of environment |
|
| 1190 |
-variables, including `CONT_IMG_VER`. At that point, the `ARG` variable may |
|
| 1191 |
-impact the resulting image, so a cache miss occurs. |
|
| 1186 |
+``` |
|
| 1187 |
+1 FROM ubuntu |
|
| 1188 |
+2 ARG CONT_IMG_VER |
|
| 1189 |
+3 RUN echo hello |
|
| 1190 |
+``` |
|
| 1191 |
+ |
|
| 1192 |
+If you specify `--build-arg CONT_IMG_VER=<value>` on the command line, in both |
|
| 1193 |
+cases, the specification on line 2 does not cause a cache miss; line 3 does |
|
| 1194 |
+cause a cache miss.`ARG CONT_IMG_VER` causes the RUN line to be identified |
|
| 1195 |
+as the same as running `CONT_IMG_VER=<value>` echo hello, so if the `<value>` |
|
| 1196 |
+changes, we get a cache miss. |
|
| 1192 | 1197 |
|
| 1193 | 1198 |
Consider another example under the same command line: |
| 1194 | 1199 |
|
| ... | ... |
@@ -1203,6 +1210,20 @@ the variable's value in the `ENV` references the `ARG` variable and that |
| 1203 | 1203 |
variable is changed through the command line. In this example, the `ENV` |
| 1204 | 1204 |
command causes the image to include the value. |
| 1205 | 1205 |
|
| 1206 |
+If an `ENV` instruction overrides an `ARG` instruction of the same name, like |
|
| 1207 |
+this Dockerfile: |
|
| 1208 |
+ |
|
| 1209 |
+``` |
|
| 1210 |
+1 FROM ubuntu |
|
| 1211 |
+2 ARG CONT_IMG_VER |
|
| 1212 |
+3 ENV CONT_IMG_VER hello |
|
| 1213 |
+4 RUN echo $CONT_IMG_VER |
|
| 1214 |
+``` |
|
| 1215 |
+ |
|
| 1216 |
+Line 3 does not cause a cache miss because the value of `CONT_IMG_VER` is a |
|
| 1217 |
+constant (`hello`). As a result, the environment variables and values used on |
|
| 1218 |
+the `RUN` (line 4) doesn't change between builds. |
|
| 1219 |
+ |
|
| 1206 | 1220 |
## ONBUILD |
| 1207 | 1221 |
|
| 1208 | 1222 |
ONBUILD [INSTRUCTION] |