Skip to content

Commit 62f6d26

Browse files
authored
Merge pull request #25486 from dvdksn/docs-image-mounts
Add docs for image mounts (--mount type=image)
2 parents f193698 + 06c302b commit 62f6d26

5 files changed

Lines changed: 227 additions & 2 deletions

File tree

content/manuals/engine/storage/_index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ of the writable layer of the container:
4242
- [Volume mounts](#volume-mounts)
4343
- [Bind mounts](#bind-mounts)
4444
- [tmpfs mounts](#tmpfs-mounts)
45+
- [Image mounts](#image-mounts)
4546
- [Named pipes](#named-pipes)
4647

4748
No matter which type of mount you choose to use, the data looks the same from
@@ -85,6 +86,18 @@ such as caching intermediate data, handling sensitive information like
8586
credentials, or reducing disk I/O. Use tmpfs mounts only when the data does not
8687
need to persist beyond the current container session.
8788

89+
### Image mounts
90+
91+
An image mount makes the contents of another image available inside a container
92+
at a path you choose. The mounted image is read-only and isn't part of the
93+
container's own image, so you can bring in tools or assets from one image
94+
without rebuilding another.
95+
96+
Use image mounts when you need to consume files packaged as an image, such as
97+
mounting a tool-rich image to debug a minimal container, or sharing read-only
98+
assets across containers running different images. Image mounts require the
99+
[containerd image store](containerd.md).
100+
88101
### Named pipes
89102

90103
[Named pipes](https://docs.microsoft.com/en-us/windows/desktop/ipc/named-pipes)
@@ -99,6 +112,7 @@ Learn more about container data persistence:
99112
- [Volumes](./volumes.md)
100113
- [Bind mounts](./bind-mounts.md)
101114
- [tmpfs mounts](./tmpfs.md)
115+
- [Image mounts](./image-mounts.md)
102116

103117
Learn more about daemon storage backends:
104118

content/manuals/engine/storage/bind-mounts.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ $ docker run --mount type=bind,src=<host-path>,dst=<container-path>[,<key>=<valu
115115

116116
Valid options for `--mount type=bind` include:
117117

118-
| Option | Description |
119-
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
118+
| Option | Description |
119+
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
120120
| `source`, `src` | The location of the file or directory on the host. This can be an absolute or relative path. |
121121
| `destination`, `dst`, `target` | The path where the file or directory is mounted in the container. Must be an absolute path. |
122122
| `readonly`, `ro` | If present, causes the bind mount to be [mounted into the container as read-only](#use-a-read-only-bind-mount). |
@@ -486,4 +486,5 @@ and
486486

487487
- Learn about [volumes](./volumes.md).
488488
- Learn about [tmpfs mounts](./tmpfs.md).
489+
- Learn about [image mounts](./image-mounts.md).
489490
- Learn about [storage drivers](/engine/storage/drivers/).
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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/).

content/manuals/engine/storage/tmpfs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,5 @@ $ docker rm tmptest
203203

204204
- Learn about [volumes](volumes.md)
205205
- Learn about [bind mounts](bind-mounts.md)
206+
- Learn about [image mounts](image-mounts.md)
206207
- Learn about [storage drivers](/engine/storage/drivers/)

content/manuals/engine/storage/volumes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,5 +808,6 @@ $ docker volume prune
808808

809809
- Learn about [bind mounts](bind-mounts.md).
810810
- Learn about [tmpfs mounts](tmpfs.md).
811+
- Learn about [image mounts](image-mounts.md).
811812
- Learn about [storage drivers](/engine/storage/drivers/).
812813
- Learn about [third-party volume driver plugins](/engine/extend/legacy_plugins/).

0 commit comments

Comments
 (0)