github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/docs/userguide/storagedriver/aufs-driver.md (about) 1 <!--[metadata]> 2 +++ 3 title = "AUFS storage driver in practice" 4 description = "Learn how to optimize your use of AUFS driver." 5 keywords = ["container, storage, driver, AUFS "] 6 [menu.main] 7 parent = "engine_driver" 8 +++ 9 <![end-metadata]--> 10 11 # Docker and AUFS in practice 12 13 AUFS was the first storage driver in use with Docker. As a result, it has a 14 long and close history with Docker, is very stable, has a lot of real-world 15 deployments, and has strong community support. AUFS has several features that 16 make it a good choice for Docker. These features enable: 17 18 - Fast container startup times. 19 - Efficient use of storage. 20 - Efficient use of memory. 21 22 Despite its capabilities and long history with Docker, some Linux distributions 23 do not support AUFS. This is usually because AUFS is not included in the 24 mainline (upstream) Linux kernel. 25 26 The following sections examine some AUFS features and how they relate to 27 Docker. 28 29 ## Image layering and sharing with AUFS 30 31 AUFS is a *unification filesystem*. This means that it takes multiple 32 directories on a single Linux host, stacks them on top of each other, and 33 provides a single unified view. To achieve this, AUFS uses a *union mount*. 34 35 AUFS stacks multiple directories and exposes them as a unified view through a 36 single mount point. All of the directories in the stack, as well as the union 37 mount point, must all exist on the same Linux host. AUFS refers to each 38 directory that it stacks as a *branch*. 39 40 Within Docker, AUFS union mounts enable image layering. The AUFS storage driver 41 implements Docker image layers using this union mount system. AUFS branches 42 correspond to Docker image layers. The diagram below shows a Docker container 43 based on the `ubuntu:latest` image. 44 45  46 47 This diagram shows that each image layer, and the container layer, is 48 represented in the Docker hosts filesystem as a directory under 49 `/var/lib/docker/`. The union mount point provides the unified view of all 50 layers. As of Docker 1.10, image layer IDs do not correspond to the names of 51 the directories that contain their data. 52 53 AUFS also supports the copy-on-write technology (CoW). Not all storage drivers 54 do. 55 56 ## Container reads and writes with AUFS 57 58 Docker leverages AUFS CoW technology to enable image sharing and minimize the 59 use of disk space. AUFS works at the file level. This means that all AUFS CoW 60 operations copy entire files - even if only a small part of the file is being 61 modified. This behavior can have a noticeable impact on container performance, 62 especially if the files being copied are large, below a lot of image layers, 63 or the CoW operation must search a deep directory tree. 64 65 Consider, for example, an application running in a container needs to add a 66 single new value to a large key-value store (file). If this is the first time 67 the file is modified, it does not yet exist in the container's top writable 68 layer. So, the CoW must *copy up* the file from the underlying image. The AUFS 69 storage driver searches each image layer for the file. The search order is from 70 top to bottom. When it is found, the entire file is *copied up* to the 71 container's top writable layer. From there, it can be opened and modified. 72 73 Larger files obviously take longer to *copy up* than smaller files, and files 74 that exist in lower image layers take longer to locate than those in higher 75 layers. However, a *copy up* operation only occurs once per file on any given 76 container. Subsequent reads and writes happen against the file's copy already 77 *copied-up* to the container's top layer. 78 79 ## Deleting files with the AUFS storage driver 80 81 The AUFS storage driver deletes a file from a container by placing a *whiteout 82 file* in the container's top layer. The whiteout file effectively obscures the 83 existence of the file in the read-only image layers below. The simplified 84 diagram below shows a container based on an image with three image layers. 85 86  87 88 The `file3` was deleted from the container. So, the AUFS storage driver placed 89 a whiteout file in the container's top layer. This whiteout file effectively 90 "deletes" `file3` from the container by obscuring any of the original file's 91 existence in the image's read-only layers. This works the same no matter which 92 of the image's read-only layers the file exists in. 93 94 ## Configure Docker with AUFS 95 96 You can only use the AUFS storage driver on Linux systems with AUFS installed. 97 Use the following command to determine if your system supports AUFS. 98 99 $ grep aufs /proc/filesystems 100 nodev aufs 101 102 This output indicates the system supports AUFS. Once you've verified your 103 system supports AUFS, you can must instruct the Docker daemon to use it. You do 104 this from the command line with the `docker daemon` command: 105 106 $ sudo docker daemon --storage-driver=aufs & 107 108 109 Alternatively, you can edit the Docker config file and add the 110 `--storage-driver=aufs` option to the `DOCKER_OPTS` line. 111 112 # Use DOCKER_OPTS to modify the daemon startup options. 113 DOCKER_OPTS="--storage-driver=aufs" 114 115 Once your daemon is running, verify the storage driver with the `docker info` 116 command. 117 118 $ sudo docker info 119 Containers: 1 120 Images: 4 121 Storage Driver: aufs 122 Root Dir: /var/lib/docker/aufs 123 Backing Filesystem: extfs 124 Dirs: 6 125 Dirperm1 Supported: false 126 Execution Driver: native-0.2 127 ...output truncated... 128 129 The output above shows that the Docker daemon is running the AUFS storage 130 driver on top of an existing `ext4` backing filesystem. 131 132 ## Local storage and AUFS 133 134 As the `docker daemon` runs with the AUFS driver, the driver stores images and 135 containers within the Docker host's local storage area under 136 `/var/lib/docker/aufs/`. 137 138 ### Images 139 140 Image layers and their contents are stored under 141 `/var/lib/docker/aufs/diff/`. With Docker 1.10 and higher, image layer IDs do 142 not correspond to directory names. 143 144 The `/var/lib/docker/aufs/layers/` directory contains metadata about how image 145 layers are stacked. This directory contains one file for every image or 146 container layer on the Docker host (though file names no longer match image 147 layer IDs). Inside each file are the names of the directories that exist below 148 it in the stack 149 150 The command below shows the contents of a metadata file in 151 `/var/lib/docker/aufs/layers/` that lists the three directories that are 152 stacked below it in the union mount. Remember, these directory names do no map 153 to image layer IDs with Docker 1.10 and higher. 154 155 $ cat /var/lib/docker/aufs/layers/91e54dfb11794fad694460162bf0cb0a4fa710cfa3f60979c177d920813e267c 156 d74508fb6632491cea586a1fd7d748dfc5274cd6fdfedee309ecdcbc2bf5cb82 157 c22013c8472965aa5b62559f2b540cd440716ef149756e7b958a1b2aba421e87 158 d3a1f33e8a5a513092f01bb7eb1c2abf4d711e5105390a3fe1ae2248cfde1391 159 160 The base layer in an image has no image layers below it, so its file is empty. 161 162 ### Containers 163 164 Running containers are mounted below `/var/lib/docker/aufs/mnt/<container-id>`. 165 This is where the AUFS union mount point that exposes the container and all 166 underlying image layers as a single unified view exists. If a container is not 167 running, it still has a directory here but it is empty. This is because AUFS 168 only mounts a container when it is running. With Docker 1.10 and higher, 169 container IDs no longer correspond to directory names under 170 `/var/lib/docker/aufs/mnt/<container-id>`. 171 172 Container metadata and various config files that are placed into the running 173 container are stored in `/var/lib/docker/containers/<container-id>`. Files in 174 this directory exist for all containers on the system, including ones that are 175 stopped. However, when a container is running the container's log files are 176 also in this directory. 177 178 A container's thin writable layer is stored in a directory under 179 `/var/lib/docker/aufs/diff/`. With Docker 1.10 and higher, container IDs no 180 longer correspond to directory names. However, the containers thin writable 181 layer still exists under here and is stacked by AUFS as the top writable layer 182 and is where all changes to the container are stored. The directory exists even 183 if the container is stopped. This means that restarting a container will not 184 lose changes made to it. Once a container is deleted, it's thin writable layer 185 in this directory is deleted. 186 187 ## AUFS and Docker performance 188 189 To summarize some of the performance related aspects already mentioned: 190 191 - The AUFS storage driver is a good choice for PaaS and other similar use-cases 192 where container density is important. This is because AUFS efficiently shares 193 images between multiple running containers, enabling fast container start times 194 and minimal use of disk space. 195 196 - The underlying mechanics of how AUFS shares files between image layers and 197 containers uses the systems page cache very efficiently. 198 199 - The AUFS storage driver can introduce significant latencies into container 200 write performance. This is because the first time a container writes to any 201 file, the file has be located and copied into the containers top writable 202 layer. These latencies increase and are compounded when these files exist below 203 many image layers and the files themselves are large. 204 205 One final point. Data volumes provide the best and most predictable 206 performance. This is because they bypass the storage driver and do not incur 207 any of the potential overheads introduced by thin provisioning and 208 copy-on-write. For this reason, you may want to place heavy write workloads on 209 data volumes. 210 211 ## Related information 212 213 * [Understand images, containers, and storage drivers](imagesandcontainers.md) 214 * [Select a storage driver](selectadriver.md) 215 * [Btrfs storage driver in practice](btrfs-driver.md) 216 * [Device Mapper storage driver in practice](device-mapper-driver.md)