Skip to content

Commit d085e24

Browse files
vvolandthaJeztah
authored andcommitted
image/history: Add --platform flag
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent b0bb4ba commit d085e24

5 files changed

Lines changed: 96 additions & 8 deletions

File tree

cli/command/image/history.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ package image
33
import (
44
"context"
55

6+
"github.com/containerd/platforms"
67
"github.com/docker/cli/cli"
78
"github.com/docker/cli/cli/command"
89
"github.com/docker/cli/cli/command/completion"
910
"github.com/docker/cli/cli/command/formatter"
1011
flagsHelper "github.com/docker/cli/cli/flags"
1112
"github.com/docker/docker/api/types/image"
13+
"github.com/pkg/errors"
1214
"github.com/spf13/cobra"
1315
)
1416

1517
type historyOptions struct {
16-
image string
18+
image string
19+
platform string
1720

1821
human bool
1922
quiet bool
@@ -45,12 +48,24 @@ func NewHistoryCommand(dockerCli command.Cli) *cobra.Command {
4548
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show image IDs")
4649
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
4750
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
51+
flags.StringVar(&opts.platform, "platform", "", `Show history for the given platform. Formatted as "os[/arch[/variant]]" (e.g., "linux/amd64")`)
52+
_ = flags.SetAnnotation("platform", "version", []string{"1.48"})
4853

54+
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
4955
return cmd
5056
}
5157

5258
func runHistory(ctx context.Context, dockerCli command.Cli, opts historyOptions) error {
53-
history, err := dockerCli.Client().ImageHistory(ctx, opts.image, image.HistoryOptions{})
59+
var options image.HistoryOptions
60+
if opts.platform != "" {
61+
p, err := platforms.Parse(opts.platform)
62+
if err != nil {
63+
return errors.Wrap(err, "invalid platform")
64+
}
65+
options.Platform = &p
66+
}
67+
68+
history, err := dockerCli.Client().ImageHistory(ctx, opts.image, options)
5469
if err != nil {
5570
return err
5671
}

cli/command/image/history_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88

99
"github.com/docker/cli/internal/test"
1010
"github.com/docker/docker/api/types/image"
11+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1112
"github.com/pkg/errors"
1213
"gotest.tools/v3/assert"
14+
is "gotest.tools/v3/assert/cmp"
1315
"gotest.tools/v3/golden"
1416
)
1517

@@ -33,6 +35,11 @@ func TestNewHistoryCommandErrors(t *testing.T) {
3335
return []image.HistoryResponseItem{{}}, errors.Errorf("something went wrong")
3436
},
3537
},
38+
{
39+
name: "invalid platform",
40+
args: []string{"--platform", "<invalid>", "arg1"},
41+
expectedError: `invalid platform`,
42+
},
3643
}
3744
for _, tc := range testCases {
3845
tc := tc
@@ -89,6 +96,17 @@ func TestNewHistoryCommandSuccess(t *testing.T) {
8996
}}, nil
9097
},
9198
},
99+
{
100+
name: "platform",
101+
args: []string{"--platform", "linux/amd64", "image:tag"},
102+
imageHistoryFunc: func(img string, options image.HistoryOptions) ([]image.HistoryResponseItem, error) {
103+
assert.Check(t, is.DeepEqual(ocispec.Platform{OS: "linux", Architecture: "amd64"}, *options.Platform))
104+
return []image.HistoryResponseItem{{
105+
ID: "1234567890123456789",
106+
Created: time.Now().Unix(),
107+
}}, nil
108+
},
109+
},
92110
}
93111
for _, tc := range testCases {
94112
tc := tc
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
IMAGE CREATED CREATED BY SIZE COMMENT
2+
123456789012 Less than a second ago 0B

docs/reference/commandline/history.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Show the history of an image
1414
| `--format` | `string` | | Format output using a custom template:<br>'table': Print output in table format with column headers (default)<br>'table TEMPLATE': Print output in table format using the given Go template<br>'json': Print in JSON format<br>'TEMPLATE': Print output using the given Go template.<br>Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
1515
| `-H`, `--human` | `bool` | `true` | Print sizes and dates in human readable format |
1616
| `--no-trunc` | `bool` | | Don't truncate output |
17+
| `--platform` | `string` | | Show history for the given platform. Formatted as `os[/arch[/variant]]` (e.g., `linux/amd64`) |
1718
| `-q`, `--quiet` | `bool` | | Only show image IDs |
1819

1920

docs/reference/commandline/image_history.md

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ Show the history of an image
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:----------------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
14-
| [`--format`](#format) | `string` | | Format output using a custom template:<br>'table': Print output in table format with column headers (default)<br>'table TEMPLATE': Print output in table format using the given Go template<br>'json': Print in JSON format<br>'TEMPLATE': Print output using the given Go template.<br>Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
15-
| `-H`, `--human` | `bool` | `true` | Print sizes and dates in human readable format |
16-
| `--no-trunc` | `bool` | | Don't truncate output |
17-
| `-q`, `--quiet` | `bool` | | Only show image IDs |
12+
| Name | Type | Default | Description |
13+
|:--------------------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
14+
| [`--format`](#format) | `string` | | Format output using a custom template:<br>'table': Print output in table format with column headers (default)<br>'table TEMPLATE': Print output in table format using the given Go template<br>'json': Print in JSON format<br>'TEMPLATE': Print output using the given Go template.<br>Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
15+
| `-H`, `--human` | `bool` | `true` | Print sizes and dates in human readable format |
16+
| `--no-trunc` | `bool` | | Don't truncate output |
17+
| [`--platform`](#platform) | `string` | | Show history for the given platform. Formatted as `os[/arch[/variant]]` (e.g., `linux/amd64`) |
18+
| `-q`, `--quiet` | `bool` | | Only show image IDs |
1819

1920

2021
<!---MARKER_GEN_END-->
@@ -76,3 +77,54 @@ $ docker history --format "{{.ID}}: {{.CreatedSince}}" busybox
7677
f6e427c148a7: 4 weeks ago
7778
<missing>: 4 weeks ago
7879
```
80+
81+
### <a name="platform"></a> Show history for a specific platform (--platform)
82+
83+
The `--platform` option allows you to specify which platform variant to show
84+
history for if multiple platforms are present. By default, `docker history`
85+
shows the history for the daemon's native platform or if not present, the
86+
first available platform.
87+
88+
If the local image store has multiple platform variants of an image, the
89+
`--platform` option selects which variant to show the history for. An error
90+
is produced if the given platform is not present in the local image cache.
91+
92+
The platform option takes the `os[/arch[/variant]]` format; for example,
93+
`linux/amd64` or `linux/arm64/v8`. Architecture and variant are optional,
94+
and if omitted falls back to the daemon's defaults.
95+
96+
97+
The following example pulls the RISC-V variant of the `alpine:latest` image
98+
and shows its history.
99+
100+
101+
```console
102+
$ docker image pull --quiet --platform=linux/riscv64 alpine
103+
docker.io/library/alpine:latest
104+
105+
$ docker image history --platform=linux/s390x alpine
106+
IMAGE CREATED CREATED BY SIZE COMMENT
107+
beefdbd8a1da 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
108+
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:ba2637314e600db5a… 8.46MB
109+
```
110+
111+
The following example attempts to show the history for a platform variant of
112+
`alpine:latest` that doesn't exist in the local image store, resulting in
113+
an error.
114+
115+
```console
116+
$ docker image ls --tree
117+
IMAGE ID DISK USAGE CONTENT SIZE IN USE
118+
alpine:latest beefdbd8a1da 10.6MB 3.37MB
119+
├─ linux/riscv64 80cde017a105 10.6MB 3.37MB
120+
├─ linux/amd64 33735bd63cf8 0B 0B
121+
├─ linux/arm/v6 50f635c8b04d 0B 0B
122+
├─ linux/arm/v7 f2f82d424957 0B 0B
123+
├─ linux/arm64/v8 9cee2b382fe2 0B 0B
124+
├─ linux/386 b3e87f642f5c 0B 0B
125+
├─ linux/ppc64le c7a6800e3dc5 0B 0B
126+
└─ linux/s390x 2b5b26e09ca2 0B 0B
127+
128+
$ docker image history --platform=linux/s390x alpine
129+
Error response from daemon: image with reference alpine:latest was found but does not match the specified platform: wanted linux/s390x
130+
```

0 commit comments

Comments
 (0)