|
| 1 | +--- |
| 2 | +description: Using image mounts |
| 3 | +title: Image mounts |
| 4 | +weight: 40 |
| 5 | +keywords: storage, mounts, image mounts, image mount |
| 6 | +--- |
| 7 | + |
| 8 | +[Volumes](volumes.md), [bind mounts](bind-mounts.md), and [tmpfs mounts](tmpfs.md) |
| 9 | +all give a container a place to read and write data. An image mount is |
| 10 | +different: instead of mounting a directory or a memory-backed filesystem, it |
| 11 | +mounts the contents of another image into the container. |
| 12 | + |
| 13 | +When you use an image mount, the filesystem of a second image is mounted into |
| 14 | +the container at a path you choose. The container can read the files from that |
| 15 | +image alongside its own filesystem, without those files being part of the |
| 16 | +container's own image. This is useful when you want to bring the tools or assets |
| 17 | +from one image into a container that's running a different image. |
| 18 | + |
| 19 | +Image mounts are read-only. The mounted image is never modified, and the |
| 20 | +container can't write to the mount. |
| 21 | + |
| 22 | +> [!NOTE] |
| 23 | +> Image mounts require the [containerd image store](containerd.md). |
| 24 | +
|
| 25 | +## When to use image mounts |
| 26 | + |
| 27 | +Image mounts are appropriate for the following types of use case: |
| 28 | + |
| 29 | +- Debugging a minimal or hardened image that doesn't include a shell or common |
| 30 | + utilities. You can mount a tool-rich image, such as `busybox`, into the |
| 31 | + running container's namespace and run those tools without changing the |
| 32 | + original image. For a worked example, see |
| 33 | + [Debug with Docker Hardened Images](/manuals/dhi/troubleshoot.md). |
| 34 | + |
| 35 | +- Sharing read-only assets, such as datasets, models, or static content, that |
| 36 | + are distributed as an image and consumed by containers running a different |
| 37 | + image. |
| 38 | + |
| 39 | +- Keeping application images small by packaging optional tooling in a separate |
| 40 | + image and mounting it only when needed. |
| 41 | + |
| 42 | +## Mounting over existing data |
| 43 | + |
| 44 | +If you mount an image into a directory in the container in which files or |
| 45 | +directories exist, the pre-existing files are obscured by the mount. This is |
| 46 | +similar to if you were to save files into `/mnt` on a Linux host, and then |
| 47 | +mounted a USB drive into `/mnt`. The contents of `/mnt` would be obscured by the |
| 48 | +contents of the USB drive until the USB drive was unmounted. |
| 49 | + |
| 50 | +With containers, there's no straightforward way of removing a mount to reveal |
| 51 | +the obscured files again. Your best option is to recreate the container without |
| 52 | +the mount. |
| 53 | + |
| 54 | +## Considerations and constraints |
| 55 | + |
| 56 | +- Image mounts are always read-only. The container can't modify the mounted |
| 57 | + image, and changes aren't persisted anywhere. |
| 58 | + |
| 59 | +- The source image must already exist in the daemon's image store. Docker |
| 60 | + doesn't pull the source image automatically when you create the mount. If the |
| 61 | + image isn't present, the command fails: |
| 62 | + |
| 63 | + ```console |
| 64 | + $ docker run --mount type=image,source=busybox:musl,destination=/dbg alpine |
| 65 | + docker: Error response from daemon: No such image: busybox:musl |
| 66 | + ``` |
| 67 | + |
| 68 | + Pull the image first with `docker pull`, then create the mount. |
| 69 | + |
| 70 | +- Image mounts require the [containerd image store](containerd.md). They aren't |
| 71 | + available when the daemon uses the classic storage drivers. |
| 72 | + |
| 73 | +- You can only create an image mount with the `--mount` flag. There is no |
| 74 | + `--volume` (`-v`) equivalent. |
| 75 | + |
| 76 | +- Running an executable from a mounted image requires a compatible runtime in |
| 77 | + the container. A dynamically linked binary only runs if the container provides |
| 78 | + a matching dynamic linker and shared libraries. For example, a glibc-based |
| 79 | + binary fails in a musl-based image such as Alpine. Statically linked binaries, |
| 80 | + or mounting only data from an image, avoid this constraint. |
| 81 | + |
| 82 | +## Syntax |
| 83 | + |
| 84 | +To mount an image with the `docker run` command, use the `--mount` flag with |
| 85 | +`type=image`. |
| 86 | + |
| 87 | +```console |
| 88 | +$ docker run --mount type=image,src=<image-reference>,dst=<container-path> |
| 89 | +``` |
| 90 | + |
| 91 | +The `--mount` flag consists of multiple key-value pairs, separated by commas and |
| 92 | +each consisting of a `<key>=<value>` tuple. The order of the keys isn't |
| 93 | +significant. |
| 94 | + |
| 95 | +```console |
| 96 | +$ docker run --mount type=image,src=<image-reference>,dst=<container-path>[,<key>=<value>...] |
| 97 | +``` |
| 98 | + |
| 99 | +### Options for --mount |
| 100 | + |
| 101 | +Valid options for `--mount type=image` include: |
| 102 | + |
| 103 | +| Option | Description | |
| 104 | +| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- | |
| 105 | +| `source`, `src` | The reference of the image to mount, for example `busybox` or `busybox:musl`. The image must exist locally. | |
| 106 | +| `destination`, `dst`, `target` | The path where the image is mounted in the container. Must be an absolute path. | |
| 107 | +| `image-subpath` | Path inside the source image to mount instead of the image root. See [Mount a subpath of an image](#mount-a-subpath-of-an-image). | |
| 108 | + |
| 109 | +```console {title="Example"} |
| 110 | +$ docker run --mount type=image,src=busybox,dst=/dbg,image-subpath=bin |
| 111 | +``` |
| 112 | + |
| 113 | +## Use an image mount in a container |
| 114 | + |
| 115 | +The following example runs an Alpine container and mounts the `busybox:musl` |
| 116 | +image at `/dbg`. Pull the source image first, since Docker doesn't pull it for |
| 117 | +you when creating the mount. This example uses the musl-based BusyBox image so |
| 118 | +its binaries are compatible with the musl-based Alpine container. |
| 119 | + |
| 120 | +```console |
| 121 | +$ docker pull busybox:musl |
| 122 | +$ docker run -d \ |
| 123 | + -it \ |
| 124 | + --name imgtest \ |
| 125 | + --mount type=image,source=busybox:musl,destination=/dbg \ |
| 126 | + alpine:latest |
| 127 | +``` |
| 128 | + |
| 129 | +The container can now read the BusyBox tools from `/dbg` while running the Alpine |
| 130 | +image: |
| 131 | + |
| 132 | +```console |
| 133 | +$ docker exec imgtest /dbg/bin/echo "hello from busybox" |
| 134 | +hello from busybox |
| 135 | +``` |
| 136 | + |
| 137 | +Verify that the mount is an `image` mount by looking in the `Mounts` section of |
| 138 | +the `docker inspect` output: |
| 139 | + |
| 140 | +```console |
| 141 | +$ docker inspect imgtest --format '{{ json .Mounts }}' |
| 142 | +[{"Type":"image","Name":"busybox:musl","Source":"/var/lib/docker/rootfs/overlayfs/...","Destination":"/dbg","Mode":"","RW":false,"Propagation":"rprivate"}] |
| 143 | +``` |
| 144 | + |
| 145 | +This shows that the mount is an `image` mount, that its source is the |
| 146 | +`busybox:musl` image, and that it's read-only (`"RW":false`). |
| 147 | + |
| 148 | +Stop and remove the container: |
| 149 | + |
| 150 | +```console |
| 151 | +$ docker container rm -fv imgtest |
| 152 | +``` |
| 153 | + |
| 154 | +## Mount a subpath of an image |
| 155 | + |
| 156 | +Use the `image-subpath` option to mount a specific directory from the source |
| 157 | +image instead of its root. For example, to mount only the `bin` directory of the |
| 158 | +`busybox` image at `/tools`: |
| 159 | + |
| 160 | +```console |
| 161 | +$ docker run -d \ |
| 162 | + -it \ |
| 163 | + --name imgtest \ |
| 164 | + --mount type=image,source=busybox,destination=/tools,image-subpath=bin \ |
| 165 | + alpine:latest |
| 166 | +``` |
| 167 | + |
| 168 | +The container sees the contents of the image's `bin` directory at `/tools`. |
| 169 | + |
| 170 | +## Use an image mount with Docker Compose |
| 171 | + |
| 172 | +A single Docker Compose service with an image mount looks like this: |
| 173 | + |
| 174 | +```yaml |
| 175 | +services: |
| 176 | + app: |
| 177 | + image: alpine:latest |
| 178 | + volumes: |
| 179 | + - type: image |
| 180 | + source: busybox |
| 181 | + target: /dbg |
| 182 | +``` |
| 183 | +
|
| 184 | +To mount a subpath of the image, use the `subpath` option under `image`: |
| 185 | + |
| 186 | +```yaml |
| 187 | +services: |
| 188 | + app: |
| 189 | + image: alpine:latest |
| 190 | + volumes: |
| 191 | + - type: image |
| 192 | + source: busybox |
| 193 | + target: /tools |
| 194 | + image: |
| 195 | + subpath: bin |
| 196 | +``` |
| 197 | + |
| 198 | +The `image.subpath` option is available in Docker Compose version 2.35.0 and |
| 199 | +later. For more information about using mounts of the `image` type with Compose, |
| 200 | +see the |
| 201 | +[Compose reference on the volume attribute](/reference/compose-file/services.md#volumes). |
| 202 | + |
| 203 | +## Next steps |
| 204 | + |
| 205 | +- Learn about [volumes](./volumes.md). |
| 206 | +- Learn about [bind mounts](./bind-mounts.md). |
| 207 | +- Learn about [tmpfs mounts](./tmpfs.md). |
| 208 | +- Learn about [storage drivers](/engine/storage/drivers/). |
0 commit comments