Browse code

Touch up "Dockerfile reference"

- add example for `docker build -f ...`
- modifiy `FROM` explaination into bullet points
- add/clarify examples for `LABEL` and `ADD` instructions
- fix review comments (PR #16109)

Signed-off-by: Charles Chan <charleswhchan@users.noreply.github.com>

Charles Chan authored on 2015/09/07 07:48:05
Showing 1 changed files
... ...
@@ -45,8 +45,8 @@ Dockerfile.
45 45
 >the build to transfer the entire contents of your hard drive to the Docker
46 46
 >daemon. 
47 47
 
48
-To use a file in the build context, the `Dockerfile` refers to the file with
49
-an instruction, for example,  a `COPY` instruction. To increase the build's
48
+To use a file in the build context, the `Dockerfile` refers to the file specified
49
+in an instruction, for example,  a `COPY` instruction. To increase the build's
50 50
 performance, exclude files and directories by adding a `.dockerignore` file to
51 51
 the context directory.  For information about how to [create a `.dockerignore`
52 52
 file](#dockerignore-file) see the documentation on this page.
... ...
@@ -55,12 +55,15 @@ Traditionally, the `Dockerfile` is called `Dockerfile` and located in the root
55 55
 of the context. You use the `-f` flag with `docker build` to point to a Dockerfile
56 56
 anywhere in your file system.
57 57
 
58
+    $ docker build -f /path/to/a/Dockerfile .
59
+
58 60
 You can specify a repository and tag at which to save the new image if
59 61
 the build succeeds:
60 62
 
61 63
     $ docker build -t shykes/myapp .
62 64
 
63
-The Docker daemon will run your steps one-by-one, committing the result
65
+The Docker daemon runs the instructions in the `Dockerfile` one-by-one,
66
+committing the result of each instruction
64 67
 to a new image if necessary, before finally outputting the ID of your
65 68
 new image. The Docker daemon will automatically clean up the context you
66 69
 sent.
... ...
@@ -69,10 +72,11 @@ Note that each instruction is run independently, and causes a new image
69 69
 to be created - so `RUN cd /tmp` will not have any effect on the next
70 70
 instructions.
71 71
 
72
-Whenever possible, Docker will re-use the intermediate images,
73
-accelerating `docker build` significantly (indicated by `Using cache` -
74
-see the [`Dockerfile` Best Practices
75
-guide](/articles/dockerfile_best-practices/#build-cache) for more information):
72
+Whenever possible, Docker will re-use the intermediate images (cache),
73
+to accelerate the `docker build` process significantly. This is indicated by
74
+the `Using cache` message in the console output.
75
+(For more information, see the [Build cache section](/articles/dockerfile_best-practices/#build-cache)) in the
76
+`Dockerfile` best practices guide:
76 77
 
77 78
     $ docker build -t SvenDowideit/ambassador .
78 79
     Uploading context 10.24 kB
... ...
@@ -97,7 +101,7 @@ Here is the format of the `Dockerfile`:
97 97
     # Comment
98 98
     INSTRUCTION arguments
99 99
 
100
-The Instruction is not case-sensitive, however convention is for them to
100
+The instruction is not case-sensitive, however convention is for them to
101 101
 be UPPERCASE in order to distinguish them from arguments more easily.
102 102
 
103 103
 Docker runs the instructions in a `Dockerfile` in order. **The
... ...
@@ -214,7 +218,7 @@ This file causes the following build behavior:
214 214
 | `*/*/temp*`    | Exclude files starting with name `temp` from any subdirectory that is two levels below the root directory. For example, the file `/somedir/subdir/temporary.txt` is ignored. |
215 215
 | `temp?`        | Exclude the files that match the pattern in the root directory. For example, the files `tempa`, `tempb` in the root directory are ignored.                                   |
216 216
 | `*.md `        | Exclude all markdown files in the root directory.                                                                                                                            |
217
-| `!LICENSE.md`  | Exception to the Markdown files exclusion is this file,  `LICENSE.md`, Include this file in the build.                                                                       |
217
+| `!LICENSE.md`  | Exception to the Markdown files exclusion. `LICENSE.md`is included  in the build.                                                                       |
218 218
 
219 219
 The placement of  `!` exception rules influences the matching algorithm; the
220 220
 last line of the `.dockerignore` that matches a particular file determines
... ...
@@ -259,13 +263,13 @@ its first instruction. The image can be any valid image – it is especially eas
259 259
 to start by **pulling an image** from the [*Public Repositories*](
260 260
 /userguide/dockerrepos).
261 261
 
262
-`FROM` must be the first non-comment instruction in the `Dockerfile`.
262
+- `FROM` must be the first non-comment instruction in the `Dockerfile`.
263 263
 
264
-`FROM` can appear multiple times within a single `Dockerfile` in order to create
264
+- `FROM` can appear multiple times within a single `Dockerfile` in order to create
265 265
 multiple images. Simply make a note of the last image ID output by the commit
266 266
 before each new `FROM` command.
267 267
 
268
-The `tag` or `digest` values are optional. If you omit either of them, the builder
268
+- The `tag` or `digest` values are optional. If you omit either of them, the builder
269 269
 assumes a `latest` by default. The builder returns an error if it cannot match
270 270
 the `tag` value.
271 271
 
... ...
@@ -280,7 +284,7 @@ generated images.
280 280
 
281 281
 RUN has 2 forms:
282 282
 
283
-- `RUN <command>` (the command is run in a shell - `/bin/sh -c` - *shell* form)
283
+- `RUN <command>` (*shell* form, the command is run in a shell - `/bin/sh -c`)
284 284
 - `RUN ["executable", "param1", "param2"]` (*exec* form)
285 285
 
286 286
 The `RUN` instruction will execute any commands in a new layer on top of the
... ...
@@ -402,28 +406,31 @@ default specified in `CMD`.
402 402
 
403 403
 The `LABEL` instruction adds metadata to an image. A `LABEL` is a
404 404
 key-value pair. To include spaces within a `LABEL` value, use quotes and
405
-backslashes as you would in command-line parsing.
405
+backslashes as you would in command-line parsing. A few usage examples:
406 406
 
407 407
     LABEL "com.example.vendor"="ACME Incorporated"
408
-
409
-An image can have more than one label. To specify multiple labels, separate each
410
-key-value pair with whitespace.
411
-
412 408
     LABEL com.example.label-with-value="foo"
413 409
     LABEL version="1.0"
414 410
     LABEL description="This text illustrates \
415 411
     that label-values can span multiple lines."
416 412
 
417
-Docker recommends combining labels in a single `LABEL` instruction where
413
+An image can have more than one label. To specify multiple labels,
414
+Docker recommends combining labels into a single `LABEL` instruction where
418 415
 possible. Each `LABEL` instruction produces a new layer which can result in an
419
-inefficient image if you use many labels. This example results in four image
420
-layers. 
416
+inefficient image if you use many labels. This example results in a single image
417
+layer. 
421 418
 
422 419
     LABEL multi.label1="value1" multi.label2="value2" other="value3"
420
+
421
+The above can also be written as:
422
+
423
+    LABEL multi.label1="value1" \
424
+          multi.label2="value2" \
425
+          other="value3"
423 426
     
424
-Labels are additive including `LABEL`s in `FROM` images. As the system
425
-encounters and then applies a new label, new `key`s override any previous labels
426
-with identical keys.    
427
+Labels are additive including `LABEL`s in `FROM` images. If Docker
428
+encounters a label/key that already exists, the new value overrides any previous
429
+labels with identical keys.
427 430
 
428 431
 To view an image's labels, use the `docker inspect` command.
429 432
 
... ...
@@ -484,14 +491,14 @@ and
484 484
     ENV myCat fluffy
485 485
 
486 486
 will yield the same net results in the final container, but the first form 
487
-does it all in one layer.
487
+is preferred because it produces a single cache layer.
488 488
 
489 489
 The environment variables set using `ENV` will persist when a container is run
490 490
 from the resulting image. You can view the values using `docker inspect`, and
491 491
 change them using `docker run --env <key>=<value>`.
492 492
 
493 493
 > **Note**:
494
-> Environment persistence can cause unexpected effects. For example,
494
+> Environment persistence can cause unexpected side effects. For example,
495 495
 > setting `ENV DEBIAN_FRONTEND noninteractive` may confuse apt-get
496 496
 > users on a Debian-based image. To set a value for a single command, use
497 497
 > `RUN <key>=<value> <command>`.
... ...
@@ -512,16 +519,16 @@ directories then they must be relative to the source directory that is
512 512
 being built (the context of the build).
513 513
 
514 514
 Each `<src>` may contain wildcards and matching will be done using Go's
515
-[filepath.Match](http://golang.org/pkg/path/filepath#Match) rules.
516
-For most command line uses this should act as expected, for example:
515
+[filepath.Match](http://golang.org/pkg/path/filepath#Match) rules. For example:
517 516
 
518 517
     ADD hom* /mydir/        # adds all files starting with "hom"
519
-    ADD hom?.txt /mydir/    # ? is replaced with any single character
518
+    ADD hom?.txt /mydir/    # ? is replaced with any single character, e.g., "home.txt"
520 519
 
521 520
 The `<dest>` is an absolute path, or a path relative to `WORKDIR`, into which
522 521
 the source will be copied inside the destination container.
523 522
 
524
-    ADD test aDir/          # adds "test" to `WORKDIR`/aDir/
523
+    ADD test relativeDir/          # adds "test" to `WORKDIR`/relativeDir/
524
+    ADD test /absoluteDir          # adds "test" to /absoluteDir
525 525
 
526 526
 All new files and directories are created with a UID and GID of 0.
527 527
 
... ...
@@ -554,7 +561,7 @@ of whether or not the file has changed and the cache should be updated.
554 554
 guide](/articles/dockerfile_best-practices/#build-cache) for more information.
555 555
 
556 556
 
557
-The copy obeys the following rules:
557
+`ADD` obeys the following rules:
558 558
 
559 559
 - The `<src>` path must be inside the *context* of the build;
560 560
   you cannot `ADD ../something /something`, because the first step of a
... ...
@@ -573,6 +580,7 @@ The copy obeys the following rules:
573 573
 
574 574
 - If `<src>` is a directory, the entire contents of the directory are copied, 
575 575
   including filesystem metadata. 
576
+
576 577
 > **Note**:
577 578
 > The directory itself is not copied, just its contents.
578 579
 
... ...
@@ -615,16 +623,16 @@ Multiple `<src>` resource may be specified but they must be relative
615 615
 to the source directory that is being built (the context of the build).
616 616
 
617 617
 Each `<src>` may contain wildcards and matching will be done using Go's
618
-[filepath.Match](http://golang.org/pkg/path/filepath#Match) rules.
619
-For most command line uses this should act as expected, for example:
618
+[filepath.Match](http://golang.org/pkg/path/filepath#Match) rules. For example:
620 619
 
621 620
     COPY hom* /mydir/        # adds all files starting with "hom"
622
-    COPY hom?.txt /mydir/    # ? is replaced with any single character
621
+    COPY hom?.txt /mydir/    # ? is replaced with any single character, e.g., "home.txt"
623 622
 
624 623
 The `<dest>` is an absolute path, or a path relative to `WORKDIR`, into which
625 624
 the source will be copied inside the destination container.
626 625
 
627
-    COPY test aDir/          # adds "test" to `WORKDIR`/aDir/
626
+    COPY test relativeDir/   # adds "test" to `WORKDIR`/relativeDir/
627
+    COPY test /absoluteDir   # adds "test" to /absoluteDir
628 628
 
629 629
 All new files and directories are created with a UID and GID of 0.
630 630
 
... ...
@@ -632,7 +640,7 @@ All new files and directories are created with a UID and GID of 0.
632 632
 > If you build using STDIN (`docker build - < somefile`), there is no
633 633
 > build context, so `COPY` can't be used.
634 634
 
635
-The copy obeys the following rules:
635
+`COPY` obeys the following rules:
636 636
 
637 637
 - The `<src>` path must be inside the *context* of the build;
638 638
   you cannot `COPY ../something /something`, because the first step of a
... ...
@@ -641,6 +649,7 @@ The copy obeys the following rules:
641 641
 
642 642
 - If `<src>` is a directory, the entire contents of the directory are copied, 
643 643
   including filesystem metadata. 
644
+
644 645
 > **Note**:
645 646
 > The directory itself is not copied, just its contents.
646 647
 
... ...
@@ -664,7 +673,7 @@ The copy obeys the following rules:
664 664
 ENTRYPOINT has two forms:
665 665
 
666 666
 - `ENTRYPOINT ["executable", "param1", "param2"]`
667
-  (the preferred *exec* form)
667
+  (*exec* form, preferred)
668 668
 - `ENTRYPOINT command param1 param2`
669 669
   (*shell* form)
670 670