github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/docs/userguide/storagedriver/overlayfs-driver.md (about) 1 <!--[metadata]> 2 +++ 3 title = "OverlayFS storage in practice" 4 description = "Learn how to optimize your use of OverlayFS driver." 5 keywords = ["container, storage, driver, OverlayFS "] 6 [menu.main] 7 parent = "engine_driver" 8 +++ 9 <![end-metadata]--> 10 11 # Docker and OverlayFS in practice 12 13 OverlayFS is a modern *union filesystem* that is similar to AUFS. In comparison 14 to AUFS, OverlayFS: 15 16 * has a simpler design 17 * has been in the mainline Linux kernel since version 3.18 18 * is potentially faster 19 20 As a result, OverlayFS is rapidly gaining popularity in the Docker community 21 and is seen by many as a natural successor to AUFS. As promising as OverlayFS 22 is, it is still relatively young. Therefore caution should be taken before 23 using it in production Docker environments. 24 25 Docker's `overlay` storage driver leverages several OverlayFS features to build 26 and manage the on-disk structures of images and containers. 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 35 >**Note**: Since it was merged into the mainline kernel, the OverlayFS *kernel 36 >module* was renamed from "overlayfs" to "overlay". As a result you may see the 37 > two terms used interchangeably in some documentation. However, this document 38 > uses "OverlayFS" to refer to the overall filesystem, and `overlay`/`overlay2` 39 > to refer to Docker's storage-drivers. 40 41 ## Image layering and sharing with OverlayFS (`overlay`) 42 43 OverlayFS takes two directories on a single Linux host, layers one on top of 44 the other, and provides a single unified view. These directories are often 45 referred to as *layers* and the technology used to layer them is known as a 46 *union mount*. The OverlayFS terminology is "lowerdir" for the bottom layer and 47 "upperdir" for the top layer. The unified view is exposed through its own 48 directory called "merged". 49 50 The diagram below shows how a Docker image and a Docker container are layered. 51 The image layer is the "lowerdir" and the container layer is the "upperdir". 52 The unified view is exposed through a directory called "merged" which is 53 effectively the containers mount point. The diagram shows how Docker constructs 54 map to OverlayFS constructs. 55 56  57 58 Notice how the image layer and container layer can contain the same files. When 59 this happens, the files in the container layer ("upperdir") are dominant and 60 obscure the existence of the same files in the image layer ("lowerdir"). The 61 container mount ("merged") presents the unified view. 62 63 The `overlay` driver only works with two layers. This means that multi-layered 64 images cannot be implemented as multiple OverlayFS layers. Instead, each image 65 layer is implemented as its own directory under `/var/lib/docker/overlay`. Hard 66 links are then used as a space-efficient way to reference data shared with lower 67 layers. As of Docker 1.10, image layer IDs no longer correspond to directory 68 names in `/var/lib/docker/` 69 70 To create a container, the `overlay` driver combines the directory representing 71 the image's top layer plus a new directory for the container. The image's top 72 layer is the "lowerdir" in the overlay and read-only. The new directory for the 73 container is the "upperdir" and is writable. 74 75 ### Example: Image and container on-disk constructs (`overlay`) 76 77 The following `docker pull` command shows a Docker host with downloading a 78 Docker image comprising five layers. 79 80 $ sudo docker pull ubuntu 81 Using default tag: latest 82 latest: Pulling from library/ubuntu 83 84 5ba4f30e5bea: Pull complete 85 9d7d19c9dc56: Pull complete 86 ac6ad7efd0f9: Pull complete 87 e7491a747824: Pull complete 88 a3ed95caeb02: Pull complete 89 Digest: sha256:46fb5d001b88ad904c5c732b086b596b92cfb4a4840a3abd0e35dbb6870585e4 90 Status: Downloaded newer image for ubuntu:latest 91 92 Each image layer has it's own directory under `/var/lib/docker/overlay/`. This 93 is where the contents of each image layer are stored. 94 95 The output of the command below shows the five directories that store the 96 contents of each image layer just pulled. However, as can be seen, the image 97 layer IDs do not match the directory names in `/var/lib/docker/overlay`. This 98 is normal behavior in Docker 1.10 and later. 99 100 $ ls -l /var/lib/docker/overlay/ 101 total 20 102 drwx------ 3 root root 4096 Jun 20 16:11 38f3ed2eac129654acef11c32670b534670c3a06e483fce313d72e3e0a15baa8 103 drwx------ 3 root root 4096 Jun 20 16:11 55f1e14c361b90570df46371b20ce6d480c434981cbda5fd68c6ff61aa0a5358 104 drwx------ 3 root root 4096 Jun 20 16:11 824c8a961a4f5e8fe4f4243dab57c5be798e7fd195f6d88ab06aea92ba931654 105 drwx------ 3 root root 4096 Jun 20 16:11 ad0fe55125ebf599da124da175174a4b8c1878afe6907bf7c78570341f308461 106 drwx------ 3 root root 4096 Jun 20 16:11 edab9b5e5bf73f2997524eebeac1de4cf9c8b904fa8ad3ec43b3504196aa3801 107 108 The image layer directories contain the files unique to that layer as well as 109 hard links to the data that is shared with lower layers. This allows for 110 efficient use of disk space. 111 112 $ ls -i /var/lib/docker/overlay/38f3ed2eac129654acef11c32670b534670c3a06e483fce313d72e3e0a15baa8/root/bin/ls 113 19793696 /var/lib/docker/overlay/38f3ed2eac129654acef11c32670b534670c3a06e483fce313d72e3e0a15baa8/root/bin/ls 114 $ ls -i /var/lib/docker/overlay/55f1e14c361b90570df46371b20ce6d480c434981cbda5fd68c6ff61aa0a5358/root/bin/ls 115 19793696 /var/lib/docker/overlay/55f1e14c361b90570df46371b20ce6d480c434981cbda5fd68c6ff61aa0a5358/root/bin/ls 116 117 Containers also exist on-disk in the Docker host's filesystem under 118 `/var/lib/docker/overlay/`. If you inspect the directory relating to a running 119 container using the `ls -l` command, you find the following file and 120 directories. 121 122 $ ls -l /var/lib/docker/overlay/<directory-of-running-container> 123 total 16 124 -rw-r--r-- 1 root root 64 Jun 20 16:39 lower-id 125 drwxr-xr-x 1 root root 4096 Jun 20 16:39 merged 126 drwxr-xr-x 4 root root 4096 Jun 20 16:39 upper 127 drwx------ 3 root root 4096 Jun 20 16:39 work 128 129 These four filesystem objects are all artifacts of OverlayFS. The "lower-id" 130 file contains the ID of the top layer of the image the container is based on. 131 This is used by OverlayFS as the "lowerdir". 132 133 $ cat /var/lib/docker/overlay/ec444863a55a9f1ca2df72223d459c5d940a721b2288ff86a3f27be28b53be6c/lower-id 134 55f1e14c361b90570df46371b20ce6d480c434981cbda5fd68c6ff61aa0a5358 135 136 The "upper" directory is the containers read-write layer. Any changes made to 137 the container are written to this directory. 138 139 The "merged" directory is effectively the containers mount point. This is where 140 the unified view of the image ("lowerdir") and container ("upperdir") is 141 exposed. Any changes written to the container are immediately reflected in this 142 directory. 143 144 The "work" directory is required for OverlayFS to function. It is used for 145 things such as *copy_up* operations. 146 147 You can verify all of these constructs from the output of the `mount` command. 148 (Ellipses and line breaks are used in the output below to enhance readability.) 149 150 $ mount | grep overlay 151 overlay on /var/lib/docker/overlay/ec444863a55a.../merged 152 type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay/55f1e14c361b.../root, 153 upperdir=/var/lib/docker/overlay/ec444863a55a.../upper, 154 workdir=/var/lib/docker/overlay/ec444863a55a.../work) 155 156 The output reflects that the overlay is mounted as read-write ("rw"). 157 158 159 ## Image layering and sharing with OverlayFS (`overlay2`) 160 161 While the `overlay` driver only works with a single lower OverlayFS layer and 162 hence requires hard links for implementation of multi-layered images, the 163 `overlay2` driver natively supports multiple lower OverlayFS layers (up to 128). 164 165 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. 166 167 ### Example: Image and container on-disk constructs (`overlay2`) 168 169 After downloading a five-layer image using `docker pull ubuntu`, you can see 170 six directories under `/var/lib/docker/overlay2`. 171 172 $ ls -l /var/lib/docker/overlay2 173 total 24 174 drwx------ 5 root root 4096 Jun 20 07:36 223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7 175 drwx------ 3 root root 4096 Jun 20 07:36 3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b 176 drwx------ 5 root root 4096 Jun 20 07:36 4e9fa83caff3e8f4cc83693fa407a4a9fac9573deaf481506c102d484dd1e6a1 177 drwx------ 5 root root 4096 Jun 20 07:36 e8876a226237217ec61c4baf238a32992291d059fdac95ed6303bdff3f59cff5 178 drwx------ 5 root root 4096 Jun 20 07:36 eca1e4e1694283e001f200a667bb3cb40853cf2d1b12c29feda7422fed78afed 179 drwx------ 2 root root 4096 Jun 20 07:36 l 180 181 The "l" directory contains shortened layer identifiers as symbolic links. These 182 shortened identifiers are used for avoid hitting the page size limitation on 183 mount arguments. 184 185 $ ls -l /var/lib/docker/overlay2/l 186 total 20 187 lrwxrwxrwx 1 root root 72 Jun 20 07:36 6Y5IM2XC7TSNIJZZFLJCS6I4I4 -> ../3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/diff 188 lrwxrwxrwx 1 root root 72 Jun 20 07:36 B3WWEFKBG3PLLV737KZFIASSW7 -> ../4e9fa83caff3e8f4cc83693fa407a4a9fac9573deaf481506c102d484dd1e6a1/diff 189 lrwxrwxrwx 1 root root 72 Jun 20 07:36 JEYMODZYFCZFYSDABYXD5MF6YO -> ../eca1e4e1694283e001f200a667bb3cb40853cf2d1b12c29feda7422fed78afed/diff 190 lrwxrwxrwx 1 root root 72 Jun 20 07:36 NFYKDW6APBCCUCTOUSYDH4DXAT -> ../223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/diff 191 lrwxrwxrwx 1 root root 72 Jun 20 07:36 UL2MW33MSE3Q5VYIKBRN4ZAGQP -> ../e8876a226237217ec61c4baf238a32992291d059fdac95ed6303bdff3f59cff5/diff 192 193 The lowerest layer contains the "link" file which contains the name of the shortened 194 identifier, and the "diff" directory which contains the contents. 195 196 $ ls /var/lib/docker/overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/ 197 diff link 198 $ cat /var/lib/docker/overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/link 199 6Y5IM2XC7TSNIJZZFLJCS6I4I4 200 $ ls /var/lib/docker/overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/diff 201 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var 202 203 The second layer contains the "lower" file for denoting the layer composition, 204 and the "diff" directory for the layer contents. It also contains the "merged" and 205 the "work" directories. 206 207 $ ls /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7 208 diff link lower merged work 209 $ cat /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/lower 210 l/6Y5IM2XC7TSNIJZZFLJCS6I4I4 211 $ ls /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/diff/ 212 etc sbin usr var 213 214 A directory for running container have similar files and directories as well. 215 Note that the lower list is separated by ':', and ordered from highest layer to lower. 216 217 $ ls -l /var/lib/docker/overlay/<directory-of-running-container> 218 $ cat /var/lib/docker/overlay/<directory-of-running-container>/lower 219 l/DJA75GUWHWG7EWICFYX54FIOVT:l/B3WWEFKBG3PLLV737KZFIASSW7:l/JEYMODZYFCZFYSDABYXD5MF6YO:l/UL2MW33MSE3Q5VYIKBRN4ZAGQP:l/NFYKDW6APBCCUCTOUSYDH4DXAT:l/6Y5IM2XC7TSNIJZZFLJCS6I4I4 220 221 The result of `mount` is as follows: 222 223 $ mount | grep overlay 224 overlay on /var/lib/docker/overlay2/9186877cdf386d0a3b016149cf30c208f326dca307529e646afce5b3f83f5304/merged 225 type overlay (rw,relatime, 226 lowerdir=l/DJA75GUWHWG7EWICFYX54FIOVT:l/B3WWEFKBG3PLLV737KZFIASSW7:l/JEYMODZYFCZFYSDABYXD5MF6YO:l/UL2MW33MSE3Q5VYIKBRN4ZAGQP:l/NFYKDW6APBCCUCTOUSYDH4DXAT:l/6Y5IM2XC7TSNIJZZFLJCS6I4I4, 227 upperdir=9186877cdf386d0a3b016149cf30c208f326dca307529e646afce5b3f83f5304/diff, 228 workdir=9186877cdf386d0a3b016149cf30c208f326dca307529e646afce5b3f83f5304/work) 229 230 ## Container reads and writes with overlay 231 232 Consider three scenarios where a container opens a file for read access with 233 overlay. 234 235 - **The file does not exist in the container layer**. If a container opens a 236 file for read access and the file does not already exist in the container 237 ("upperdir") it is read from the image ("lowerdir"). This should incur very 238 little performance overhead. 239 240 - **The file only exists in the container layer**. If a container opens a file 241 for read access and the file exists in the container ("upperdir") and not in 242 the image ("lowerdir"), it is read directly from the container. 243 244 - **The file exists in the container layer and the image layer**. If a 245 container opens a file for read access and the file exists in the image layer 246 and the container layer, the file's version in the container layer is read. 247 This is because files in the container layer ("upperdir") obscure files with 248 the same name in the image layer ("lowerdir"). 249 250 Consider some scenarios where files in a container are modified. 251 252 - **Writing to a file for the first time**. The first time a container writes 253 to an existing file, that file does not exist in the container ("upperdir"). 254 The `overlay`/`overlay2` driver performs a *copy_up* operation to copy the file 255 from the image ("lowerdir") to the container ("upperdir"). The container then 256 writes the changes to the new copy of the file in the container layer. 257 258 However, OverlayFS works at the file level not the block level. This means 259 that all OverlayFS copy-up operations copy entire files, even if the file is 260 very large and only a small part of it is being modified. This can have a 261 noticeable impact on container write performance. However, two things are 262 worth noting: 263 264 * The copy_up operation only occurs the first time any given file is 265 written to. Subsequent writes to the same file will operate against the copy of 266 the file already copied up to the container. 267 268 * OverlayFS only works with two layers. This means that performance should 269 be better than AUFS which can suffer noticeable latencies when searching for 270 files in images with many layers. 271 272 - **Deleting files and directories**. When files are deleted within a container 273 a *whiteout* file is created in the containers "upperdir". The version of the 274 file in the image layer ("lowerdir") is not deleted. However, the whiteout file 275 in the container obscures it. 276 277 Deleting a directory in a container results in *opaque directory* being 278 created in the "upperdir". This has the same effect as a whiteout file and 279 effectively masks the existence of the directory in the image's "lowerdir". 280 281 ## Configure Docker with the `overlay`/`overlay2` storage driver 282 283 To configure Docker to use the `overlay` storage driver your Docker host must be 284 running version 3.18 of the Linux kernel (preferably newer) with the overlay 285 kernel module loaded. For the `overlay2` driver, the version of your kernel must 286 be 4.0 or newer. OverlayFS can operate on top of most supported Linux filesystems. 287 However, ext4 is currently recommended for use in production environments. 288 289 The following procedure shows you how to configure your Docker host to use 290 OverlayFS. The procedure assumes that the Docker daemon is in a stopped state. 291 292 > **Caution:** If you have already run the Docker daemon on your Docker host 293 > and have images you want to keep, `push` them Docker Hub or your private 294 > Docker Trusted Registry before attempting this procedure. 295 296 1. If it is running, stop the Docker `daemon`. 297 298 2. Verify your kernel version and that the overlay kernel module is loaded. 299 300 $ uname -r 301 3.19.0-21-generic 302 303 $ lsmod | grep overlay 304 overlay 305 306 3. Start the Docker daemon with the `overlay`/`overlay2` storage driver. 307 308 $ dockerd --storage-driver=overlay & 309 [1] 29403 310 root@ip-10-0-0-174:/home/ubuntu# INFO[0000] Listening for HTTP on unix (/var/run/docker.sock) 311 INFO[0000] Option DefaultDriver: bridge 312 INFO[0000] Option DefaultNetwork: bridge 313 <output truncated> 314 315 Alternatively, you can force the Docker daemon to automatically start with 316 the `overlay`/`overlay2` driver by editing the Docker config file and adding 317 the `--storage-driver=overlay` flag to the `DOCKER_OPTS` line. Once this option 318 is set you can start the daemon using normal startup scripts without having 319 to manually pass in the `--storage-driver` flag. 320 321 4. Verify that the daemon is using the `overlay`/`overlay2` storage driver 322 323 $ docker info 324 Containers: 0 325 Images: 0 326 Storage Driver: overlay 327 Backing Filesystem: extfs 328 <output truncated> 329 330 Notice that the *Backing filesystem* in the output above is showing as 331 `extfs`. Multiple backing filesystems are supported but `extfs` (ext4) is 332 recommended for production use cases. 333 334 Your Docker host is now using the `overlay`/`overlay2` storage driver. If you 335 run the `mount` command, you'll find Docker has automatically created the 336 `overlay` mount with the required "lowerdir", "upperdir", "merged" and "workdir" 337 constructs. 338 339 ## OverlayFS and Docker Performance 340 341 As a general rule, the `overlay`/`overlay2` drivers should be fast. Almost 342 certainly faster than `aufs` and `devicemapper`. In certain circumstances it may 343 also be faster than `btrfs`. That said, there are a few things to be aware of 344 relative to the performance of Docker using the `overlay`/`overlay2` storage 345 drivers. 346 347 - **Page Caching**. OverlayFS supports page cache sharing. This means multiple 348 containers accessing the same file can share a single page cache entry (or 349 entries). This makes the `overlay`/`overlay2` drivers efficient with memory and 350 a good option for PaaS and other high density use cases. 351 352 - **copy_up**. As with AUFS, OverlayFS has to perform copy-up operations any 353 time a container writes to a file for the first time. This can insert latency 354 into the write operation — especially if the file being copied up is 355 large. However, once the file has been copied up, all subsequent writes to that 356 file occur without the need for further copy-up operations. 357 358 The OverlayFS copy_up operation should be faster than the same operation 359 with AUFS. This is because AUFS supports more layers than OverlayFS and it is 360 possible to incur far larger latencies if searching through many AUFS layers. 361 362 - **RPMs and Yum**. OverlayFS only implements a subset of the POSIX standards. 363 This can result in certain OverlayFS operations breaking POSIX standards. One 364 such operation is the *copy-up* operation. Therefore, using `yum` inside of a 365 container on a Docker host using the `overlay`/`overlay2` storage drivers is 366 unlikely to work without implementing workarounds. 367 368 - **Inode limits**. Use of the `overlay` storage driver can cause excessive 369 inode consumption. This is especially so as the number of images and containers 370 on the Docker host grows. A Docker host with a large number of images and lots 371 of started and stopped containers can quickly run out of inodes. The `overlay2` 372 does not have such an issue. 373 374 Unfortunately you can only specify the number of inodes in a filesystem at the 375 time of creation. For this reason, you may wish to consider putting 376 `/var/lib/docker` on a separate device with its own filesystem, or manually 377 specifying the number of inodes when creating the filesystem. 378 379 The following generic performance best practices also apply to OverlayFS. 380 381 - **Solid State Devices (SSD)**. For best performance it is always a good idea 382 to use fast storage media such as solid state devices (SSD). 383 384 - **Use Data Volumes**. Data volumes provide the best and most predictable 385 performance. This is because they bypass the storage driver and do not incur 386 any of the potential overheads introduced by thin provisioning and 387 copy-on-write. For this reason, you should place heavy write workloads on data 388 volumes.