8000 image/inspect: Add --platform flag · docker/cli@0d9d187 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d9d187

Browse files
committed
image/inspect: Add --platform flag
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent 07b203e commit 0d9d187

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

cli/command/image/inspect.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ import (
77
"bytes"
88
"context"
99

10+
"github.com/containerd/platforms"
1011
"github.com/docker/cli/cli"
1112
"github.com/docker/cli/cli/command"
1213
"github.com/docker/cli/cli/command/completion"
1314
"github.com/docker/cli/cli/command/inspect"
1415
flagsHelper "github.com/docker/cli/cli/flags"
1516
"github.com/docker/docker/api/types/image"
1617
"github.com/docker/docker/client"
18+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1719
"github.com/spf13/cobra"
1820
)
1921

2022
type inspectOptions struct {
21-
format string
22-
refs []string
23+
format string
24+
refs []string
25+
platform string
2326
}
2427

2528
// newInspectCommand creates a new cobra.Command for `docker image inspect`
@@ -39,14 +42,36 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
3942

4043
flags := cmd.Flags()
4144
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
45+
46+
// Don't default to DOCKER_DEFAULT_PLATFORM env variable, always default to
47+
// inspecting the image as-is. This also avoids forcing the platform selection
48+
// on older APIs which don't support it.
49+
flags.StringVar(&opts.platform, "platform", "", `Inspect a specific platform of the multi-platform image.
50+
If the image or the server is not multi-platform capable, the command will error out if the platform does not match.
51+
'os[/arch[/variant]]': Explicit platform (eg. linux/amd64)`)
52+
flags.SetAnnotation("platform", "version", []string{"1.49"})
53+
54+
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
4255
return cmd
4356
}
4457

4558
func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) error {
59+
var platform *ocispec.Platform
60+
if opts.platform != "" {
61+
p, err := platforms.Parse(opts.platform)
62+
if err != nil {
63+
return err
64+
}
65+
platform = &p
66+
}
67+
4668
apiClient := dockerCLI.Client()
4769
return inspect.Inspect(dockerCLI.Out(), opts.refs, opts.format, func(ref string) (any, []byte, error) {
4870
var buf bytes.Buffer
49-
resp, err := apiClient.ImageInspect(ctx, ref, client.ImageInspectWithRawResponse(&buf))
71+
resp, err := apiClient.ImageInspect(ctx, ref,
72+
client.ImageInspectWithRawResponse(&buf),
73+
client.ImageInspectWithPlatform(platform),
74+
)
5075
if err != nil {
5176
return image.InspectResponse{}, nil, err
5277
}

docs/reference/commandline/image_inspect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Display detailed information on one or more images
88
| Name | Type | Default | Description |
99
|:-----------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1010
| `-f`, `--format` | `string` | | Format output using a custom 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 |
11+
| `--platform` | `string` | | Inspect a specific platform of the multi-platform image.<br>If the image or the server is not multi-platform capable, the command will error out if the platform does not match.<br>'os[/arch[/variant]]': Explicit platform (eg. linux/amd64) |
1112

1213

1314
<!---MARKER_GEN_END-->

0 commit comments

Comments
 (0)
0