Browse code

Adds Docker Image v1 Spec Documention

Many iterations have gone into documenting a v1 specification of Docker's Image
format.

v1 Image spec: clarify parent field

- metalivedev pointed out that the description was ambiguous, so I've removed
mention that it was randomly generated. It IS the ID of the parent image.

Updated v1 image specificatino documentation

- More complete details and deprication notifications for each field
in the JSON metadata of an image.
- Details on the format for packaging combined Image JSON + Filesystem
Changeset archives for all layers of an image.

Clarify description of an image "Layer" in v1 spec

Updated intro of image v1 spec

Updated image v1 spec after more review

- Removed description of "Image" from the terminology section. The entire
document is meant to serve this purpose.
- Updated the definition of "Image Filesystem Changeset".
- Clarified the level of randomness needed for generating image IDs.
- Updated the description of "Image Checksum".
- Added term descriptions for "Repository" and "Tag"
- Removed extraneous/implementation-specific fields from the Image JSON
example file and field descriptions:
- removed "container_config" and "docker_version" fields.
- Added missing "author" field example and description.
- Removed extraneous/implementation-specific fields from the "config" struct
example and description:
- removed "Hostname", "Domainname", "Cpuset", "AttachStdin", "AttachStdout",
"AttachStderr", "PortSpecs", "Tty", "OpenStdin", "StdinOnce", "Image",
"NetworkDisabled", and "OnBuild".
- Updated example Image JSON config with better example values for "Env",
"Cmd", "Volumes", "WorkingDir", "Entrypoint", "CpuShares", "Memory",
"MemorySwap", and "User".
- Added notices that any fields not specified are to be considered as
implementation specific and should be ignored my implementations which
are unable to interpret them.
- Updated example of creating layer filesystem changesets to use less formal
language.
- Listed more details in the section regarding extraction of a bundle of image
layers into the root filesystem of a container.
- Updated the closing mention of Docker as an evolving implementation.

More updates to the v1 image spec

- Added line wrapping after 80 columns per line to adhere to documentation
style guides, as pointed out by @jamtur01

- Removed references to any specific docker commands, updated a few descriptions
or drop repeated statements, as pointed out by @cpuguy83

Cleanup image v1 spec draft after fredlf comments

Address comments by mmdriley on v1 image spec

Improve description of image v1 spec 'config.User`

- Improves description of image v1 specification for the 'User' runtime
parameter after recomendations by tianon.

Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)

Josh Hawn authored on 2014/12/08 16:25:31
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,573 @@
0
+# Docker Image Specification v1.0.0
1
+
2
+An *Image* is an ordered collection of root filesystem changes and the
3
+corresponding execution parameters for use within a container runtime. This
4
+specification outlines the format of these filesystem changes and corresponding
5
+parameters and describes how to create and use them for use with a container
6
+runtime and execution tool.
7
+
8
+## Terminology
9
+
10
+This specification uses the following terms:
11
+
12
+<dl>
13
+    <dt>
14
+        Layer
15
+    </dt>
16
+    <dd>
17
+        Images are composed of <i>layers</i>. <i>Image layer</i> is a general
18
+        term which may be used to refer to one or both of the following:
19
+
20
+        <ol>
21
+            <li>The metadata for the layer, described in the JSON format.</li>
22
+            <li>The filesystem changes described by a layer.</li>
23
+        </ol>
24
+
25
+        To refer to the former you may use the term <i>Layer JSON</i> or
26
+        <i>Layer Metadata</i>. To refer to the latter you may use the term
27
+        <i>Image Filesystem Changeset</i> or <i>Image Diff</i>.
28
+    </dd>
29
+    <dt>
30
+        Image JSON
31
+    </dt>
32
+    <dd>
33
+        Each layer has an associated A JSON structure which describes some
34
+        basic information about the image such as date created, author, and the
35
+        ID of its parent image as well as execution/runtime configuration like
36
+        its entry point, default arguments, CPU/memory shares, networking, and
37
+        volumes.
38
+    </dd>
39
+    <dt>
40
+        Image Filesystem Changeset
41
+    </dt>
42
+    <dd>
43
+        Each layer has an archive of the files which have been added, changed,
44
+        or deleted relative to its parent layer. Using a layer-based or union
45
+        filesystem such as AUFS, or by computing the diff from filesystem
46
+        snapshots, the filesystem changeset can be used to present a series of
47
+        image layers as if they were one cohesive filesystem.
48
+    </dd>
49
+    <dt>
50
+        Image ID <a name="id_desc"></a>
51
+    </dt>
52
+    <dd>
53
+        Each layer is given an ID upon its creation. It is 
54
+        represented as a hexidecimal encoding of 256 bits, e.g.,
55
+        <code>a9561eb1b190625c9adb5a9513e72c4dedafc1cb2d4c5236c9a6957ec7dfd5a9</code>.
56
+        Image IDs should be sufficiently random so as to be globally unique.
57
+        32 bytes read from <code>/dev/urandom</code> is sufficient for all
58
+        practical purposes. Alternatively, an image ID may be derived as a
59
+        cryptographic hash of image contents as the result is considered
60
+        indistinguishable from random. The choice is left up to implementors.
61
+    </dd>
62
+    <dt>
63
+        Image Parent
64
+    </dt>
65
+    <dd>
66
+        Most layer metadata structs contain a <code>parent</code> field which
67
+        refers to the Image from which another directly descends. An image
68
+        contains a separate JSON metadata file and set of changes relative to
69
+        the filesystem of its parent image. <i>Image Ancestor</i> and
70
+        <i>Image Descendant</i> are also common terms.
71
+    </dd>
72
+    <dt>
73
+        Image Checksum
74
+    </dt>
75
+    <dd>
76
+        Layer metadata structs contain a cryptographic hash of the contents of
77
+        the layer's filesystem changeset. Though the set of changes exists as a
78
+        simple Tar archive, two archives with identical filenames and content
79
+        will have different SHA digests if the last-access or last-modified
80
+        times of any entries differ. For this reason, image checksums are
81
+        generated using the TarSum algorithm which produces a cryptographic
82
+        hash of file contents and selected headers only. Details of this
83
+        algorithm are described in the separate [TarSum specification](https://github.com/docker/docker/blob/master/pkg/tarsum/tarsum_spec.md).
84
+    </dd>
85
+    <dt>
86
+        Tag
87
+    </dt>
88
+    <dd>
89
+        A tag serves to map a descriptive, user-given name to any single image
90
+        ID. An image name suffix (the name component after <code>:</code>) is
91
+        often referred to as a tag as well, though it strictly refers to the
92
+        full name of an image. Acceptable values for a tag suffix are
93
+        implementation specific, but they SHOULD be limited to the set of
94
+        alphanumeric characters <code>[a-zA-z0-9]</code>, punctuation
95
+        characters <code>[._-]</code>, and MUST NOT contain a <code>:</code>
96
+        character.
97
+    </dd>
98
+    <dt>
99
+        Repository
100
+    </dt>
101
+    <dd>
102
+        A collection of tags grouped under a common prefix (the name component
103
+        before <code>:</code>). For example, in an image tagged with the name
104
+        <code>my-app:3.1.4</code>, <code>my-app</code> is the <i>Repository</i>
105
+        component of the name. Acceptable values for repository name are
106
+        implementation specific, but they SHOULD be limited to the set of
107
+        alphanumeric characters <code>[a-zA-z0-9]</code>, and punctuation
108
+        characters <code>[._-]</code>, however it MAY contain additional
109
+        <code>/</code> and <code>:</code> characters for organizational
110
+        purposes, with the last <code>:</code> character being interpreted
111
+        dividing the repository component of the name from the tag suffic
112
+        component.
113
+    </dd>
114
+</dl>
115
+
116
+## Image JSON Schema
117
+
118
+Here is an example image JSON file:
119
+
120
+```
121
+{  
122
+    "id": "a9561eb1b190625c9adb5a9513e72c4dedafc1cb2d4c5236c9a6957ec7dfd5a9",
123
+    "parent": "c6e3cedcda2e3982a1a6760e178355e8e65f7b80e4e5248743fa3549d284e024",
124
+    "checksum": "tarsum.v1+sha256:e58fcf7418d2390dec8e8fb69d88c06ec07039d651fedc3aa72af9972e7d046b",
125
+    "created": "2014-10-13T21:19:18.674353812Z",
126
+    "author": "Alyssa P. Hacker &ltalyspdev@example.com&gt",
127
+    "architecture": "amd64",
128
+    "os": "linux",
129
+    "Size": 271828,
130
+    "config": {
131
+        "User": "alice",
132
+        "Memory": 2048,
133
+        "MemorySwap": 4096,
134
+        "CpuShares": 8,
135
+        "ExposedPorts": {  
136
+            "8080/tcp": {}
137
+        },
138
+        "Env": [  
139
+            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
140
+            "FOO=docker_is_a_really",
141
+            "BAR=great_tool_you_know"
142
+        ],
143
+        "Entrypoint": [
144
+            "/bin/my-app-binary"
145
+        ],
146
+        "Cmd": [
147
+            "--foreground",
148
+            "--config",
149
+            "/etc/my-app.d/default.cfg"
150
+        ],
151
+        "Volumes": {
152
+            "/var/job-result-data": {},
153
+            "/var/log/my-app-logs": {},
154
+        },
155
+        "WorkingDir": "/home/alice",
156
+    }
157
+}
158
+```
159
+
160
+### Image JSON Field Descriptions
161
+
162
+<dl>
163
+    <dt>
164
+        id <code>string</code>
165
+    </dt>
166
+    <dd>
167
+        Randomly generated, 256-bit, hexadecimal encoded. Uniquely identifies
168
+        the image.
169
+    </dd>
170
+    <dt>
171
+        parent <code>string</code>
172
+    </dt>
173
+    <dd>
174
+        ID of the parent image. If there is no parent image then this field
175
+        should be omitted. A collection of images may share many of the same
176
+        ancestor layers. This organizational structure is strictly a tree with
177
+        any one layer having either no parent or a single parent and zero or
178
+        more decendent layers. Cycles are not allowed and implementations
179
+        should be careful to avoid creating them or iterating through a cycle
180
+        indefinitely.
181
+    </dd>
182
+    <dt>
183
+        created <code>string</code>
184
+    </dt>
185
+    <dd>
186
+        ISO-8601 formatted combined date and time at which the image was
187
+        created.
188
+    </dd>
189
+    <dt>
190
+        author <code>string</code>
191
+    </dt>
192
+    <dd>
193
+        Gives the name and/or email address of the person or entity which
194
+        created and is responsible for maintaining the image.
195
+    </dd>
196
+    <dt>
197
+        architecture <code>string</code>
198
+    </dt>
199
+    <dd>
200
+        The CPU architecture which the binaries in this image are built to run
201
+        on. Possible values include:
202
+        <ul>
203
+            <li>386</li>
204
+            <li>amd64</li>
205
+            <li>arm</li>
206
+        </ul>
207
+        More values may be supported in the future and any of these may or may
208
+        not be supported by a given container runtime implementation.
209
+    </dd>
210
+    <dt>
211
+        os <code>string</code>
212
+    </dt>
213
+    <dd>
214
+        The name of the operating system which the image is built to run on.
215
+        Possible values include:
216
+        <ul>
217
+            <li>darwin</li>
218
+            <li>freebsd</li>
219
+            <li>linux</li>
220
+        </ul>
221
+        More values may be supported in the future and any of these may or may
222
+        not be supported by a given container runtime implementation.
223
+    </dd>
224
+    <dt>
225
+        checksum <code>string</code>
226
+    </dt>
227
+    <dd>
228
+        Image Checksum of the filesystem changeset associated with the image
229
+        layer.
230
+    </dd>
231
+    <dt>
232
+        Size <code>integer</code>
233
+    </dt>
234
+    <dd>
235
+        The size in bytes of the filesystem changeset associated with the image
236
+        layer.
237
+    </dd>
238
+    <dt>
239
+        config <code>struct</code>
240
+    </dt>
241
+    <dd>
242
+        The execution parameters which should be used as a base when running a
243
+        container using the image. This field can be <code>null</code>, in
244
+        which case any execution parameters should be specified at creation of
245
+        the image.
246
+
247
+        <h4>Container RunConfig Field Descriptions</h4>
248
+
249
+        <dl>
250
+            <dt>
251
+                User <code>string</code>
252
+            </dt>
253
+            <dd>
254
+                <p>The username or UID which the process in the container should
255
+                run as. This acts as a default value to use when the value is
256
+                not specified when creating a container.</p>
257
+
258
+                <p>All of the following are valid:</p>
259
+
260
+                <ul>
261
+                    <li><code>user</code></li>
262
+                    <li><code>uid</code></li>
263
+                    <li><code>user:group</code></li>
264
+                    <li><code>uid:gid</code></li>
265
+                    <li><code>uid:group</code></li>
266
+                    <li><code>user:gid</code></li>
267
+                </ul>
268
+
269
+                <p>If <code>group</code>/<code>gid</code> is not specified, the
270
+                default group and supplementary groups of the given
271
+                <code>user</code>/<code>uid</code> in <code>/etc/passwd</code>
272
+                from the container are applied.</p>
273
+            </dd>
274
+            <dt>
275
+                Memory <code>integer</code>
276
+            </dt>
277
+            <dd>
278
+                Memory limit (in bytes). This acts as a default value to use
279
+                when the value is not specified when creating a container.
280
+            </dd>
281
+            <dt>
282
+                MemorySwap <code>integer</code>
283
+            </dt>
284
+            <dd>
285
+                Total memory usage (memory + swap); set to <code>-1</code> to
286
+                disable swap. This acts as a default value to use when the
287
+                value is not specified when creating a container.
288
+            </dd>
289
+            <dt>
290
+                CpuShares <code>integer</code>
291
+            </dt>
292
+            <dd>
293
+                CPU shares (relative weight vs. other containers). This acts as
294
+                a default value to use when the value is not specified when
295
+                creating a container.
296
+            </dd>
297
+            <dt>
298
+                ExposedPorts <code>struct</code>
299
+            </dt>
300
+            <dd>
301
+                A set of ports to expose from a container running this image.
302
+                This JSON structure value is unusual because it is a direct
303
+                JSON serialization of the Go type
304
+                <code>map[string]struct{}</code> and is represented in JSON as
305
+                an object mapping its keys to an empty object. Here is an
306
+                example:
307
+
308
+<pre>{
309
+    "8080": {},
310
+    "53/udp": {},
311
+    "2356/tcp": {}
312
+}</pre>
313
+
314
+                Its keys can be in the format of:
315
+                <ul>
316
+                    <li>
317
+                        <code>"port/tcp"</code>
318
+                    </li>
319
+                    <li>
320
+                        <code>"port/udp"</code>
321
+                    </li>
322
+                    <li>
323
+                        <code>"port"</code>
324
+                    </li>
325
+                </ul>
326
+                with the default protocol being <code>"tcp"</code> if not
327
+                specified.
328
+
329
+                These values act as defaults and are merged with any specified
330
+                when creating a container.
331
+            </dd>
332
+            <dt>
333
+                Env <code>array of strings</code>
334
+            </dt>
335
+            <dd>
336
+                Entries are in the format of <code>VARNAME="var value"</code>.
337
+                These values act as defaults and are merged with any specified
338
+                when creating a container.
339
+            </dd>
340
+            <dt>
341
+                Entrypoint <code>array of strings</code>
342
+            </dt>
343
+            <dd>
344
+                A list of arguments to use as the command to execute when the
345
+                container starts. This value acts as a  default and is replaced
346
+                by an entrypoint specified when creating a container.
347
+            </dd>
348
+            <dt>
349
+                Cmd <code>array of strings</code>
350
+            </dt>
351
+            <dd>
352
+                Default arguments to the entry point of the container. These
353
+                values act as defaults and are replaced with any specified when
354
+                creating a container. If an <code>Entrypoint</code> value is
355
+                not specified, then the first entry of the <code>Cmd</code>
356
+                array should be interpreted as the executable to run.
357
+            </dd>
358
+            <dt>
359
+                Volumes <code>struct</code>
360
+            </dt>
361
+            <dd>
362
+                A set of directories which should be created as data volumes in
363
+                a container running this image. This JSON structure value is
364
+                unusual because it is a direct JSON serialization of the Go
365
+                type <code>map[string]struct{}</code> and is represented in
366
+                JSON as an object mapping its keys to an empty object. Here is
367
+                an example:
368
+<pre>{
369
+    "/var/my-app-data/": {},
370
+    "/etc/some-config.d/": {},
371
+}</pre>
372
+            </dd>
373
+            <dt>
374
+                WorkingDir <code>string</code>
375
+            </dt>
376
+            <dd>
377
+                Sets the current working directory of the entry point process
378
+                in the container. This value acts as a default and is replaced
379
+                by a working directory specified when creating a container.
380
+            </dd>
381
+        </dl>
382
+    </dd>
383
+</dl>
384
+
385
+Any extra fields in the Image JSON struct are considered implementation
386
+specific and should be ignored by any implementations which are unable to
387
+interpret them.
388
+
389
+## Creating an Image Filesystem Changeset
390
+
391
+An example of creating an Image Filesystem Changeset follows.
392
+
393
+An image root filesystem is first creating as an empty directory named with the
394
+ID of the image being created. Here is the initial empty directory structure
395
+for the changeset for an image with ID `c3167915dc9d` ([real IDs are much
396
+longer](#id_desc), but this example use a truncated one here for brevity.
397
+Implementations need not name the rootfs directory in this way but it may be
398
+convenient for keeping record of a large number of image layers.):
399
+
400
+```
401
+c3167915dc9d/
402
+```
403
+
404
+Files and directories are then created:
405
+
406
+```
407
+c3167915dc9d/
408
+    etc/
409
+        my-app-config
410
+    bin/
411
+        my-app-binary
412
+        my-app-tools
413
+```
414
+
415
+The `c3167915dc9d` directory is then committed as a plain Tar archive with
416
+entries for the following files:
417
+
418
+```
419
+etc/my-app-config
420
+bin/my-app-binary
421
+bin/my-app-tools
422
+```
423
+
424
+The TarSum checksum for the archive file is then computed and placed in the
425
+JSON metadata along with the execution parameters.
426
+
427
+To make changes to the filesystem of this container image, create a new
428
+directory named with a new ID, such as `f60c56784b83`, and initialize it with
429
+a snapshot of the parent image's root filesystem, so that the directory is
430
+identical to that of `c3167915dc9d`. NOTE: a copy-on-write or union filesystem
431
+can make this very efficient:
432
+
433
+```
434
+f60c56784b83/
435
+    etc/
436
+        my-app-config
437
+    bin/
438
+        my-app-binary
439
+        my-app-tools
440
+```
441
+
442
+This example change is going add a configuration directory at `/etc/my-app.d`
443
+which contains a default config file. There's also a change to the
444
+`my-app-tools` binary to handle the config layout change. The `f60c56784b83`
445
+directory then looks like this:
446
+
447
+```
448
+f60c56784b83/
449
+    etc/
450
+        my-app.d/
451
+            default.cfg
452
+    bin/
453
+        my-app-binary
454
+        my-app-tools
455
+```
456
+
457
+This reflects the removal of `/etc/my-app-config` and creation of a file and
458
+directory at `/etc/my-app.d/default.cfg`. `/bin/my-app-tools` has also been
459
+replaced with an updated version. Before committing this directory to a
460
+changeset, because it has a parent image, it is first compared with the
461
+directory tree of the parent snapshot, `f60c56784b83`, looking for files and
462
+directories that have been added, modified, or removed. The following changeset
463
+is found:
464
+
465
+```
466
+Added:      /etc/my-app.d/default.cfg
467
+Modified:   /bin/my-app-tools
468
+Deleted:    /etc/my-app-config
469
+```
470
+
471
+A Tar Archive is then created which contains *only* this changeset: The added
472
+and modified files and directories in their entirety, and for each deleted item
473
+an entry for an empty file at the same location but with the basename of the
474
+deleted file or directory prefixed with `.wh.`. The filenames prefixed with
475
+`.wh.` are known as "whiteout" files. NOTE: For this reason, it is not possible
476
+to create an image root filesystem which contains a file or directory with a
477
+name beginning with `.wh.`. The resulting Tar archive for `f60c56784b83` has
478
+the following entries:
479
+
480
+```
481
+/etc/my-app.d/default.cfg
482
+/bin/my-app-tools
483
+/etc/.wh.my-app-config
484
+```
485
+
486
+Any given image is likely to be composed of several of these Image Filesystem
487
+Changeset tar archives.
488
+
489
+## Combined Image JSON + Filesystem Changeset Format
490
+
491
+There is also a format for a single archive which contains complete information
492
+about an image, including:
493
+
494
+    - repository names/tags
495
+    - all image layer JSON files
496
+    - all tar archives of each layer filesystem changesets
497
+
498
+For example, here's what the full archive of `library/busybox` is (displayed in
499
+`tree` format):
500
+
501
+```
502
+.
503
+├── 5785b62b697b99a5af6cd5d0aabc804d5748abbb6d3d07da5d1d3795f2dcc83e
504
+│   ├── VERSION
505
+│   ├── json
506
+│   └── layer.tar
507
+├── a7b8b41220991bfc754d7ad445ad27b7f272ab8b4a2c175b9512b97471d02a8a
508
+│   ├── VERSION
509
+│   ├── json
510
+│   └── layer.tar
511
+├── a936027c5ca8bf8f517923169a233e391cbb38469a75de8383b5228dc2d26ceb
512
+│   ├── VERSION
513
+│   ├── json
514
+│   └── layer.tar
515
+├── f60c56784b832dd990022afc120b8136ab3da9528094752ae13fe63a2d28dc8c
516
+│   ├── VERSION
517
+│   ├── json
518
+│   └── layer.tar
519
+└── repositories
520
+```
521
+
522
+There are one or more directories named with the ID for each layer in a full
523
+image. Each of these directories contains 3 files:
524
+
525
+    * `VERSION` - The schema version of the `json` file
526
+    * `json` - The JSON metadata for an image layer
527
+    * `layer.tar` - The Tar archive of the filesystem changeset for an image
528
+      layer.
529
+
530
+The content of the `VERSION` files is simply the semantic version of the JSON
531
+metadata schema:
532
+
533
+```
534
+1.0
535
+```
536
+
537
+And the `repositories` file is another JSON file which describes names/tags:
538
+
539
+```
540
+{  
541
+    "busybox":{  
542
+        "latest":"5785b62b697b99a5af6cd5d0aabc804d5748abbb6d3d07da5d1d3795f2dcc83e"
543
+    }
544
+}
545
+```
546
+
547
+Every key in this object is the name of a repository, and maps to a collection
548
+of tag suffixes. Each tag maps to the ID of the image represented by that tag.
549
+
550
+## Loading an Image Filesystem Changeset
551
+
552
+Unpacking a bundle of image layer JSON files and their corresponding filesystem
553
+changesets can be done using a series of steps:
554
+
555
+1. Follow the parent IDs of image layers to find the root ancestor (an image
556
+with no parent ID specified).
557
+2. For every image layer, in order from root ancestor and descending down,
558
+extract the contents of that layer's filesystem changeset archive into a
559
+directory which will be used as the root of a container filesystem.
560
+
561
+    - Extract all contents of each archive.
562
+    - Walk the directory tree once more, removing any files with the prefix
563
+    `.wh.` and the corresponding file or directory named without this prefix.
564
+
565
+
566
+## Implementations
567
+
568
+This specification is an admittedly imperfect description of an
569
+imperfectly-understood problem. The Docker project is, in turn, an attempt to
570
+implement this specification. Our goal and our execution toward it will evolve
571
+over time, but our primary concern in this specification and in our
572
+implementation is compatibility and interoperability.