Skip to content

Commit dced6bd

Browse files
committed
image rm: add --platform option
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent c99373c commit dced6bd

3 files changed

Lines changed: 103 additions & 10 deletions

File tree

cli/command/image/remove.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ import (
66
"fmt"
77

88
cerrdefs "github.com/containerd/errdefs"
9+
"github.com/containerd/platforms"
910
"github.com/docker/cli/cli"
1011
"github.com/docker/cli/cli/command"
1112
"github.com/docker/cli/cli/command/completion"
1213
"github.com/docker/docker/api/types/image"
14+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1315
"github.com/spf13/cobra"
1416
)
1517

1618
type removeOptions struct {
17-
force bool
18-
noPrune bool
19+
force bool
20+
noPrune bool
21+
platforms []string
1922
}
2023

2124
// NewRemoveCommand creates a new `docker remove` command
@@ -40,6 +43,11 @@ func NewRemoveCommand(dockerCli command.Cli) *cobra.Command {
4043
flags.BoolVarP(&opts.force, "force", "f", false, "Force removal of the image")
4144
flags.BoolVar(&opts.noPrune, "no-prune", false, "Do not delete untagged parents")
4245

46+
// TODO(thaJeztah): create a "platforms" option for this (including validation / parsing).
47+
flags.StringSliceVar(&opts.platforms, "platform", nil, `Remove only the given platform variant. Formatted as "os[/arch[/variant]]" (e.g., "linux/amd64")`)
48+
_ = flags.SetAnnotation("platform", "version", []string{"1.50"})
49+
50+
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
4351
return cmd
4452
}
4553

@@ -53,9 +61,20 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
5361
func runRemove(ctx context.Context, dockerCLI command.Cli, opts removeOptions, images []string) error {
5462
apiClient := dockerCLI.Client()
5563

64+
var ps []ocispec.Platform
65+
66+
for _, v := range opts.platforms {
67+
p, err := platforms.Parse(v)
68+
if err != nil {
69+
return err
70+
}
71+
ps = append(ps, p)
72+
}
73+
5674
options := image.RemoveOptions{
5775
Force: opts.force,
5876
PruneChildren: !opts.noPrune,
77+
Platforms: ps,
5978
}
6079

6180
// TODO(thaJeztah): this logic can likely be simplified: do we want to print "not found" errors at all when using "force"?

docs/reference/commandline/image_rm.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ Remove one or more images
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:----------------|:-------|:--------|:-------------------------------|
14-
| `-f`, `--force` | `bool` | | Force removal of the image |
15-
| `--no-prune` | `bool` | | Do not delete untagged parents |
12+
| Name | Type | Default | Description |
13+
|:--------------------------|:--------------|:--------|:-------------------------------------------------------------------------------------------------|
14+
| `-f`, `--force` | `bool` | | Force removal of the image |
15+
| `--no-prune` | `bool` | | Do not delete untagged parents |
16+
| [`--platform`](#platform) | `stringSlice` | | Remove only the given platform variant. Formatted as `os[/arch[/variant]]` (e.g., `linux/amd64`) |
1617

1718

1819
<!---MARKER_GEN_END-->
@@ -105,3 +106,75 @@ Deleted: 4986bf8c15363d1c5d15512d5266f8777bfba4974ac56e3270e7760f6f0a8125
105106
Deleted: ea13149945cb6b1e746bf28032f02e9b5a793523481a0a18645fc77ad53c4ea2
106107
Deleted: df7546f9f060a2268024c8a230d8639878585defcc1bc6f79d2728a13957871b
107108
```
109+
110+
111+
### <a name="platform"></a> Remove a specific platform (`--platform`)
112+
113+
The `--platform` option allows you to specify which platform variants of the
114+
image to remove. By default, `docker image remove` removes all platform variants
115+
that are present. Use the `--platform` option to specify which platform variant
116+
of the image to remove.
117+
118+
Removing a specific platform removes the image from all images that reference
119+
the same content, and requires the `--force` option to be used. Omitting the
120+
`--force` option produces a warning, and the remove is canceled:
121+
122+
```console
123+
$ docker image rm --platform=linux/amd64 alpine
124+
Error response from daemon: Content will be removed from all images referencing this variant. Use —-force to force delete.
125+
```
126+
127+
128+
The platform option takes the `os[/arch[/variant]]` format; for example,
129+
`linux/amd64` or `linux/arm64/v8`. Architecture and variant are optional,
130+
and default to the daemon's native architecture if omitted.
131+
132+
You can pass multiple platforms either by passing the `--platform` flag
133+
multiple times, or by passing a comma-separated list of platforms to remove.
134+
The following uses of this option are equivalent;
135+
136+
```console
137+
$ docker image rm --plaform linux/amd64 --platform linux/ppc64le myimage
138+
$ docker image rm --plaform linux/amd64,linux/ppc64le myimage
139+
```
140+
141+
The following example removes the `linux/amd64` and `linux/ppc64le` variants
142+
of an `alpine` image that contains multiple platform variants in the image
143+
cache:
144+
145+
```console
146+
$ docker image ls --tree alpine
147+
148+
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
149+
alpine:latest a8560b36e8b8 37.8MB 11.2MB U
150+
├─ linux/amd64 1c4eef651f65 12.1MB 3.64MB U
151+
├─ linux/arm/v6 903bfe2ae994 0B 0B
152+
├─ linux/arm/v7 9c2d245b3c01 0B 0B
153+
├─ linux/arm64/v8 757d680068d7 12.8MB 3.99MB
154+
├─ linux/386 2436f2b3b7d2 0B 0B
155+
├─ linux/ppc64le 9ed53fd3b831 12.8MB 3.58MB
156+
├─ linux/riscv64 1de5eb4a9a67 0B 0B
157+
└─ linux/s390x fe0dcdd1f783 0B 0B
158+
159+
$ docker image --platform=linux/amd64,linux/ppc64le --force alpine
160+
Deleted: sha256:1c4eef651f65e2f7daee7ee785882ac164b02b78fb74503052a26dc061c90474
161+
Deleted: sha256:9ed53fd3b83120f78b33685d930ce9bf5aa481f6e2d165c42cbbddbeaa196f6f
162+
```
163+
164+
After the command completes, the given variants of the `alpine` image are removed
165+
from the image cache:
166+
167+
```console
168+
$ docker image ls --tree alpine
169+
170+
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
171+
alpine:latest a8560b36e8b8 12.8MB 3.99MB
172+
├─ linux/amd64 1c4eef651f65 0B 0B
173+
├─ linux/arm/v6 903bfe2ae994 0B 0B
174+
├─ linux/arm/v7 9c2d245b3c01 0B 0B
175+
├─ linux/arm64/v8 757d680068d7 12.8MB 3.99MB
176+
├─ linux/386 2436f2b3b7d2 0B 0B
177+
├─ linux/ppc64le 9ed53fd3b831 0B 0B
178+
├─ linux/riscv64 1de5eb4a9a67 0B 0B
179+
└─ linux/s390x fe0dcdd1f783 0B 0B
180+
```

docs/reference/commandline/rmi.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ Remove one or more images
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:----------------|:-------|:--------|:-------------------------------|
14-
| `-f`, `--force` | `bool` | | Force removal of the image |
15-
| `--no-prune` | `bool` | | Do not delete untagged parents |
12+
| Name | Type | Default | Description |
13+
|:----------------|:--------------|:--------|:-------------------------------------------------------------------------------------------------|
14+
| `-f`, `--force` | `bool` | | Force removal of the image |
15+
| `--no-prune` | `bool` | | Do not delete untagged parents |
16+
| `--platform` | `stringSlice` | | Remove only the given platform variant. Formatted as `os[/arch[/variant]]` (e.g., `linux/amd64`) |
1617

1718

1819
<!---MARKER_GEN_END-->

0 commit comments

Comments
 (0)