Browse code

Add overlay2 description to overlayfs-driver.md

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>

Akihiro Suda authored on 2016/06/20 17:29:21
Showing 1 changed files
... ...
@@ -25,13 +25,20 @@ using it in production Docker environments.
25 25
 Docker's `overlay` storage driver leverages several OverlayFS features to build
26 26
  and manage the on-disk structures of images and containers.
27 27
 
28
+Since version 1.12, Docker also provides `overlay2` storage driver which is much
29
+more efficient than `overlay` in terms of inode utilization. The `overlay2`
30
+driver is only compatible with Linux kernel 4.0 and later.
31
+
32
+For comparison between `overlay` vs `overlay2`, please also refer to [Select a
33
+storage driver](selectadriver.md#overlay-vs-overlay2).
34
+
28 35
 >**Note**: Since it was merged into the mainline kernel, the OverlayFS *kernel 
29 36
 >module* was renamed from "overlayfs" to "overlay". As a result you may see the
30 37
 > two terms used interchangeably in some documentation. However, this document 
31
-> uses  "OverlayFS" to refer to the overall filesystem, and `overlay` to refer 
32
-> to Docker's storage-driver.
38
+> uses  "OverlayFS" to refer to the overall filesystem, and `overlay`/`overlay2`
39
+> to refer to Docker's storage-drivers.
33 40
 
34
-## Image layering and sharing with OverlayFS
41
+## Image layering and sharing with OverlayFS (`overlay`)
35 42
 
36 43
 OverlayFS takes two directories on a single Linux host, layers one on top of 
37 44
 the other, and provides a single unified view. These directories are often 
... ...
@@ -53,52 +60,60 @@ Notice how the image layer and container layer can contain the same files. When
53 53
 obscure the existence of the same files in the image layer ("lowerdir"). The 
54 54
 container mount ("merged") presents the unified view.
55 55
 
56
-OverlayFS only works with two layers. This means that multi-layered images 
57
-cannot be implemented as multiple OverlayFS layers. Instead, each image layer 
58
-is implemented as its own directory under `/var/lib/docker/overlay`. 
59
-Hard links are then used as a space-efficient way to reference data shared with
60
- lower layers. As of Docker 1.10, image layer IDs no longer correspond to 
61
-directory names in `/var/lib/docker/`
56
+The `overlay` driver only works with two layers. This means that multi-layered
57
+images cannot be implemented as multiple OverlayFS layers. Instead, each image
58
+layer is implemented as its own directory under `/var/lib/docker/overlay`.  Hard
59
+links are then used as a space-efficient way to reference data shared with lower
60
+layers. As of Docker 1.10, image layer IDs no longer correspond to directory
61
+names in `/var/lib/docker/`
62 62
 
63 63
 To create a container, the `overlay` driver combines the directory representing
64 64
  the image's top layer plus a new directory for the container. The image's top 
65 65
 layer is the "lowerdir" in the overlay and read-only. The new directory for the
66 66
  container is the "upperdir" and is writable.
67 67
 
68
-## Example: Image and container on-disk constructs
68
+### Example: Image and container on-disk constructs (`overlay`)
69 69
 
70 70
 The following `docker pull` command shows a Docker host with downloading a 
71
-Docker image comprising four layers.
71
+Docker image comprising five layers.
72 72
 
73 73
     $ sudo docker pull ubuntu
74 74
     Using default tag: latest
75 75
     latest: Pulling from library/ubuntu
76
-    8387d9ff0016: Pull complete
77
-    3b52deaaf0ed: Pull complete
78
-    4bd501fad6de: Pull complete
76
+
77
+    5ba4f30e5bea: Pull complete
78
+    9d7d19c9dc56: Pull complete
79
+    ac6ad7efd0f9: Pull complete
80
+    e7491a747824: Pull complete
79 81
     a3ed95caeb02: Pull complete
80
-    Digest: sha256:457b05828bdb5dcc044d93d042863fba3f2158ae249a6db5ae3934307c757c54
82
+    Digest: sha256:46fb5d001b88ad904c5c732b086b596b92cfb4a4840a3abd0e35dbb6870585e4
81 83
     Status: Downloaded newer image for ubuntu:latest
82 84
 
83 85
 Each image layer has it's own directory under `/var/lib/docker/overlay/`. This 
84 86
 is where the contents of each image layer are stored. 
85 87
 
86
-The output of the command below shows the four directories that store the 
88
+The output of the command below shows the five directories that store the 
87 89
 contents of each image layer just pulled. However, as can be seen, the image 
88 90
 layer IDs do not match the directory names in `/var/lib/docker/overlay`. This 
89 91
 is normal behavior in Docker 1.10 and later.
90 92
 
91 93
     $ ls -l /var/lib/docker/overlay/
92
-    total 24
93
-    drwx------ 3 root root 4096 Oct 28 11:02 1d073211c498fd5022699b46a936b4e4bdacb04f637ad64d3475f558783f5c3e
94
-    drwx------ 3 root root 4096 Oct 28 11:02 5a4526e952f0aa24f3fcc1b6971f7744eb5465d572a48d47c492cb6bbf9cbcda
95
-    drwx------ 5 root root 4096 Oct 28 11:06 99fcaefe76ef1aa4077b90a413af57fd17d19dce4e50d7964a273aae67055235
96
-    drwx------ 3 root root 4096 Oct 28 11:01 c63fb41c2213f511f12f294dd729b9903a64d88f098c20d2350905ac1fdbcbba
94
+    total 20
95
+    drwx------ 3 root root 4096 Jun 20 16:11 38f3ed2eac129654acef11c32670b534670c3a06e483fce313d72e3e0a15baa8
96
+    drwx------ 3 root root 4096 Jun 20 16:11 55f1e14c361b90570df46371b20ce6d480c434981cbda5fd68c6ff61aa0a5358
97
+    drwx------ 3 root root 4096 Jun 20 16:11 824c8a961a4f5e8fe4f4243dab57c5be798e7fd195f6d88ab06aea92ba931654
98
+    drwx------ 3 root root 4096 Jun 20 16:11 ad0fe55125ebf599da124da175174a4b8c1878afe6907bf7c78570341f308461
99
+    drwx------ 3 root root 4096 Jun 20 16:11 edab9b5e5bf73f2997524eebeac1de4cf9c8b904fa8ad3ec43b3504196aa3801
97 100
 
98 101
 The image layer directories contain the files unique to that layer as well as 
99 102
 hard links to the data that is shared with lower layers. This allows for 
100 103
 efficient use of disk space.
101 104
 
105
+    $ ls -i /var/lib/docker/overlay/38f3ed2eac129654acef11c32670b534670c3a06e483fce313d72e3e0a15baa8/root/bin/ls
106
+    19793696 /var/lib/docker/overlay/38f3ed2eac129654acef11c32670b534670c3a06e483fce313d72e3e0a15baa8/root/bin/ls
107
+    $ ls -i /var/lib/docker/overlay/55f1e14c361b90570df46371b20ce6d480c434981cbda5fd68c6ff61aa0a5358/root/bin/ls
108
+    19793696 /var/lib/docker/overlay/55f1e14c361b90570df46371b20ce6d480c434981cbda5fd68c6ff61aa0a5358/root/bin/ls
109
+
102 110
 Containers also exist on-disk in the Docker host's filesystem under 
103 111
 `/var/lib/docker/overlay/`. If you inspect the directory relating to a running 
104 112
 container using the `ls -l` command, you find the following file and 
... ...
@@ -106,17 +121,17 @@ directories.
106 106
 
107 107
     $ ls -l /var/lib/docker/overlay/<directory-of-running-container>
108 108
     total 16
109
-    -rw-r--r-- 1 root root   64 Oct 28 11:06 lower-id
110
-    drwxr-xr-x 1 root root 4096 Oct 28 11:06 merged
111
-    drwxr-xr-x 4 root root 4096 Oct 28 11:06 upper
112
-    drwx------ 3 root root 4096 Oct 28 11:06 work
109
+    -rw-r--r-- 1 root root   64 Jun 20 16:39 lower-id
110
+    drwxr-xr-x 1 root root 4096 Jun 20 16:39 merged
111
+    drwxr-xr-x 4 root root 4096 Jun 20 16:39 upper
112
+    drwx------ 3 root root 4096 Jun 20 16:39 work
113 113
 
114 114
 These four filesystem objects are all artifacts of OverlayFS. The "lower-id" 
115 115
 file contains the ID of the top layer of the image the container is based on. 
116 116
 This is used by OverlayFS as the "lowerdir".
117 117
 
118
-    $ cat /var/lib/docker/overlay/73de7176c223a6c82fd46c48c5f152f2c8a7e49ecb795a7197c3bb795c4d879e/lower-id
119
-    1d073211c498fd5022699b46a936b4e4bdacb04f637ad64d3475f558783f5c3e
118
+    $ cat /var/lib/docker/overlay/ec444863a55a9f1ca2df72223d459c5d940a721b2288ff86a3f27be28b53be6c/lower-id
119
+    55f1e14c361b90570df46371b20ce6d480c434981cbda5fd68c6ff61aa0a5358
120 120
 
121 121
 The "upper" directory is the containers read-write layer. Any changes made to 
122 122
 the container are written to this directory.
... ...
@@ -133,13 +148,85 @@ You can verify all of these constructs from the output of the `mount` command.
133 133
 (Ellipses and line breaks are used in the output below to enhance readability.)
134 134
 
135 135
     $ mount | grep overlay
136
-    overlay on /var/lib/docker/overlay/73de7176c223.../merged
137
-    type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay/1d073211c498.../root,
138
-    upperdir=/var/lib/docker/overlay/73de7176c223.../upper,
139
-    workdir=/var/lib/docker/overlay/73de7176c223.../work)
136
+    overlay on /var/lib/docker/overlay/ec444863a55a.../merged
137
+    type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay/55f1e14c361b.../root,
138
+    upperdir=/var/lib/docker/overlay/ec444863a55a.../upper,
139
+    workdir=/var/lib/docker/overlay/ec444863a55a.../work)
140 140
 
141 141
 The output reflects that the overlay is mounted as read-write ("rw").
142 142
 
143
+
144
+## Image layering and sharing with OverlayFS (`overlay2`)
145
+
146
+While the `overlay` driver only works with a single lower OverlayFS layer and
147
+hence requires hard links for implementation of multi-layered images, the
148
+`overlay2` driver natively supports multiple lower OverlayFS layers (up to 128).
149
+
150
+Hence the `overlay2` driver offers better performance for layer-related docker commands (e.g. `docker build` and `docker commit`), and consumes fewer inodes than the `overlay` driver.
151
+
152
+### Example: Image and container on-disk constructs (`overlay2`)
153
+
154
+After downloading a five-layer image using `docker pull ubuntu`, you can see
155
+six directories under `/var/lib/docker/overlay2`.
156
+
157
+    $ ls -l /var/lib/docker/overlay2
158
+    total 24
159
+    drwx------ 5 root root 4096 Jun 20 07:36 223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7
160
+    drwx------ 3 root root 4096 Jun 20 07:36 3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b
161
+    drwx------ 5 root root 4096 Jun 20 07:36 4e9fa83caff3e8f4cc83693fa407a4a9fac9573deaf481506c102d484dd1e6a1
162
+    drwx------ 5 root root 4096 Jun 20 07:36 e8876a226237217ec61c4baf238a32992291d059fdac95ed6303bdff3f59cff5
163
+    drwx------ 5 root root 4096 Jun 20 07:36 eca1e4e1694283e001f200a667bb3cb40853cf2d1b12c29feda7422fed78afed
164
+    drwx------ 2 root root 4096 Jun 20 07:36 l
165
+
166
+The "l" directory contains shortened layer identifiers as symbolic links.  These
167
+shortened identifiers are used for avoid hitting the page size limitation on
168
+mount arguments.
169
+
170
+    $ ls -l /var/lib/docker/overlay2/l
171
+    total 20
172
+    lrwxrwxrwx 1 root root 72 Jun 20 07:36 6Y5IM2XC7TSNIJZZFLJCS6I4I4 -> ../3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/diff
173
+    lrwxrwxrwx 1 root root 72 Jun 20 07:36 B3WWEFKBG3PLLV737KZFIASSW7 -> ../4e9fa83caff3e8f4cc83693fa407a4a9fac9573deaf481506c102d484dd1e6a1/diff
174
+    lrwxrwxrwx 1 root root 72 Jun 20 07:36 JEYMODZYFCZFYSDABYXD5MF6YO -> ../eca1e4e1694283e001f200a667bb3cb40853cf2d1b12c29feda7422fed78afed/diff
175
+    lrwxrwxrwx 1 root root 72 Jun 20 07:36 NFYKDW6APBCCUCTOUSYDH4DXAT -> ../223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/diff
176
+    lrwxrwxrwx 1 root root 72 Jun 20 07:36 UL2MW33MSE3Q5VYIKBRN4ZAGQP -> ../e8876a226237217ec61c4baf238a32992291d059fdac95ed6303bdff3f59cff5/diff
177
+
178
+The lowerest layer contains the "link" file which contains the name of the shortened
179
+identifier, and the "diff" directory which contains the contents.
180
+
181
+    $ /var/lib/docker/overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/
182
+    diff  link
183
+    $ cat /var/lib/docker/overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/link
184
+    6Y5IM2XC7TSNIJZZFLJCS6I4I4
185
+    $ ls  /var/lib/docker/overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/diff
186
+    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
187
+
188
+The second layer contains the "lower" file for denoting the layer composition,
189
+and the "diff" directory for the layer contents.  It also contains the "merged" and
190
+the "work" directories.
191
+
192
+    $ ls /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7
193
+    diff  link  lower  merged  work
194
+    $ cat /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/lower
195
+    l/6Y5IM2XC7TSNIJZZFLJCS6I4I4
196
+    $ ls /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/diff/
197
+    etc  sbin  usr  var
198
+
199
+A directory for running container have similar files and directories as well.
200
+Note that the lower list is separated by ':', and ordered from highest layer to lower.
201
+
202
+    $ ls -l /var/lib/docker/overlay/<directory-of-running-container>
203
+    $ cat /var/lib/docker/overlay/<directory-of-running-container>/lower
204
+    l/DJA75GUWHWG7EWICFYX54FIOVT:l/B3WWEFKBG3PLLV737KZFIASSW7:l/JEYMODZYFCZFYSDABYXD5MF6YO:l/UL2MW33MSE3Q5VYIKBRN4ZAGQP:l/NFYKDW6APBCCUCTOUSYDH4DXAT:l/6Y5IM2XC7TSNIJZZFLJCS6I4I4
205
+
206
+The result of `mount` is as follows:
207
+
208
+    $ mount | grep overlay
209
+    overlay on /var/lib/docker/overlay2/9186877cdf386d0a3b016149cf30c208f326dca307529e646afce5b3f83f5304/merged
210
+    type overlay (rw,relatime,
211
+    lowerdir=l/DJA75GUWHWG7EWICFYX54FIOVT:l/B3WWEFKBG3PLLV737KZFIASSW7:l/JEYMODZYFCZFYSDABYXD5MF6YO:l/UL2MW33MSE3Q5VYIKBRN4ZAGQP:l/NFYKDW6APBCCUCTOUSYDH4DXAT:l/6Y5IM2XC7TSNIJZZFLJCS6I4I4,
212
+    upperdir=9186877cdf386d0a3b016149cf30c208f326dca307529e646afce5b3f83f5304/diff,
213
+    workdir=9186877cdf386d0a3b016149cf30c208f326dca307529e646afce5b3f83f5304/work)
214
+
143 215
 ## Container reads and writes with overlay
144 216
 
145 217
 Consider three scenarios where a container opens a file for read access with 
... ...
@@ -164,9 +251,9 @@ Consider some scenarios where files in a container are modified.
164 164
 
165 165
 - **Writing to a file for the first time**. The first time a container writes 
166 166
 to an existing file, that file does not exist in the container ("upperdir"). 
167
-The `overlay` driver performs a *copy_up* operation to copy the file from the 
168
-image ("lowerdir") to the container ("upperdir"). The container then writes the
169
- changes to the new copy of the file in the container layer.
167
+The `overlay`/`overlay2` driver performs a *copy_up* operation to copy the file
168
+from the image ("lowerdir") to the container ("upperdir"). The container then
169
+writes the changes to the new copy of the file in the container layer.
170 170
 
171 171
     However, OverlayFS works at the file level not the block level. This means 
172 172
 that all OverlayFS copy-up operations copy entire files, even if the file is 
... ...
@@ -191,13 +278,13 @@ file in the image layer ("lowerdir") is not deleted. However, the whiteout file
191 191
 created in the "upperdir". This has the same effect as a whiteout file and 
192 192
 effectively masks the existence of the directory in the image's "lowerdir".
193 193
 
194
-## Configure Docker with the overlay storage driver
194
+## Configure Docker with the `overlay`/`overlay2` storage driver
195 195
 
196
-To configure Docker to use the overlay storage driver your Docker host must be 
196
+To configure Docker to use the `overlay` storage driver your Docker host must be 
197 197
 running version 3.18 of the Linux kernel (preferably newer) with the overlay 
198
-kernel module loaded. OverlayFS can operate on top of most supported Linux 
199
-filesystems. However, ext4 is currently recommended for use in production 
200
-environments.
198
+kernel module loaded. For the `overlay2` driver, the version of your kernel must
199
+be 4.0 or newer. OverlayFS can operate on top of most supported Linux filesystems.
200
+However, ext4 is currently recommended for use in production environments.
201 201
 
202 202
 The following procedure shows you how to configure your Docker host to use 
203 203
 OverlayFS. The procedure assumes that the Docker daemon is in a stopped state.
... ...
@@ -216,7 +303,7 @@ OverlayFS. The procedure assumes that the Docker daemon is in a stopped state.
216 216
         $ lsmod | grep overlay
217 217
         overlay
218 218
 
219
-3. Start the Docker daemon with the `overlay` storage driver.
219
+3. Start the Docker daemon with the `overlay`/`overlay2` storage driver.
220 220
 
221 221
         $ dockerd --storage-driver=overlay &
222 222
         [1] 29403
... ...
@@ -226,12 +313,12 @@ OverlayFS. The procedure assumes that the Docker daemon is in a stopped state.
226 226
         <output truncated>
227 227
 
228 228
     Alternatively, you can force the Docker daemon to automatically start with
229
-    the `overlay` driver by editing the Docker config file and adding the
230
-    `--storage-driver=overlay` flag to the `DOCKER_OPTS` line. Once this option
229
+    the `overlay`/`overlay2` driver by editing the Docker config file and adding
230
+    the `--storage-driver=overlay` flag to the `DOCKER_OPTS` line. Once this option
231 231
     is set you can start the daemon using normal startup scripts without having
232 232
     to manually pass in the `--storage-driver` flag.
233 233
 
234
-4. Verify that the daemon is using the `overlay` storage driver
234
+4. Verify that the daemon is using the `overlay`/`overlay2` storage driver
235 235
 
236 236
         $ docker info
237 237
         Containers: 0
... ...
@@ -244,22 +331,23 @@ OverlayFS. The procedure assumes that the Docker daemon is in a stopped state.
244 244
 `extfs`. Multiple backing filesystems are supported but `extfs` (ext4) is 
245 245
 recommended for production use cases.
246 246
 
247
-Your Docker host is now using the `overlay` storage driver. If you run the 
248
-`mount` command, you'll find Docker has automatically created the `overlay` 
249
-mount with the required "lowerdir", "upperdir", "merged" and "workdir" 
247
+Your Docker host is now using the `overlay`/`overlay2` storage driver. If you
248
+run the `mount` command, you'll find Docker has automatically created the
249
+`overlay` mount with the required "lowerdir", "upperdir", "merged" and "workdir"
250 250
 constructs.
251 251
 
252 252
 ## OverlayFS and Docker Performance
253 253
 
254
-As a general rule, the `overlay` driver should be fast. Almost certainly faster
255
- than `aufs` and `devicemapper`. In certain circumstances it may also be faster
256
- than `btrfs`. That said, there are a few things to be aware of relative to the
257
- performance of Docker using the `overlay` storage driver.
254
+As a general rule, the `overlay`/`overlay2` drivers should be fast. Almost
255
+certainly faster than `aufs` and `devicemapper`. In certain circumstances it may
256
+also be faster than `btrfs`. That said, there are a few things to be aware of
257
+relative to the performance of Docker using the `overlay`/`overlay2` storage
258
+drivers.
258 259
 
259
-- **Page Caching**. OverlayFS supports page cache sharing. This means multiple 
260
-containers accessing the same file can share a single page cache entry (or 
261
-entries). This makes the `overlay` driver efficient with memory and a good 
262
-option for PaaS and other high density use cases.
260
+- **Page Caching**. OverlayFS supports page cache sharing. This means multiple
261
+containers accessing the same file can share a single page cache entry (or
262
+entries). This makes the `overlay`/`overlay2` drivers efficient with memory and
263
+a good option for PaaS and other high density use cases.
263 264
 
264 265
 - **copy_up**. As with AUFS, OverlayFS has to perform copy-up operations any 
265 266
 time a container writes to a file for the first time. This can insert latency 
... ...
@@ -274,13 +362,14 @@ possible to incur far larger latencies if searching through many AUFS layers.
274 274
 - **RPMs and Yum**. OverlayFS only implements a subset of the POSIX standards. 
275 275
 This can result in certain OverlayFS operations breaking POSIX standards. One 
276 276
 such operation is the *copy-up* operation. Therefore, using `yum` inside of a 
277
-container on a Docker host using the `overlay` storage driver is unlikely to 
278
-work without implementing workarounds.
277
+container on a Docker host using the `overlay`/`overlay2` storage drivers is
278
+unlikely to work without implementing workarounds.
279 279
 
280 280
 - **Inode limits**. Use of the `overlay` storage driver can cause excessive 
281 281
 inode consumption. This is especially so as the number of images and containers
282 282
  on the Docker host grows. A Docker host with a large number of images and lots
283
- of started and stopped containers can quickly run out of inodes.
283
+ of started and stopped containers can quickly run out of inodes. The `overlay2`
284
+ does not have such an issue.
284 285
 
285 286
 Unfortunately you can only specify the number of inodes in a filesystem at the 
286 287
 time of creation. For this reason, you may wish to consider putting