Skip to content

Commit b0bb4ba

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

5 files changed

Lines changed: 76 additions & 13 deletions

File tree

cli/command/image/load.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"io"
66

7+
"github.com/containerd/platforms"
78
"github.com/docker/cli/cli"
89
"github.com/docker/cli/cli/command"
910
"github.com/docker/cli/cli/command/completion"
@@ -15,8 +16,9 @@ import (
1516
)
1617

1718
type loadOptions struct {
18-
input string
19-
quiet bool
19+
input string
20+
quiet bool
21+
platform string
2022
}
2123

2224
// NewLoadCommand creates a new `docker load` command
@@ -40,7 +42,10 @@ func NewLoadCommand(dockerCli command.Cli) *cobra.Command {
4042

4143
flags.StringVarP(&opts.input, "input", "i", "", "Read from tar archive file, instead of STDIN")
4244
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress the load output")
45+
flags.StringVar(&opts.platform, "platform", "", `Load only the given platform variant. Formatted as "os[/arch[/variant]]" (e.g., "linux/amd64")`)
46+
_ = flags.SetAnnotation("platform", "version", []string{"1.48"})
4347

48+
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
4449
return cmd
4550
}
4651

@@ -63,12 +68,20 @@ func runLoad(ctx context.Context, dockerCli command.Cli, opts loadOptions) error
6368
return errors.Errorf("requested load from stdin, but stdin is empty")
6469
}
6570

66-
var loadOpts image.LoadOptions
71+
var options image.LoadOptions
6772
if opts.quiet || !dockerCli.Out().IsTerminal() {
68-
loadOpts.Quiet = true
73+
options.Quiet = true
6974
}
7075

71-
response, err := dockerCli.Client().ImageLoad(ctx, input, loadOpts)
76+
if opts.platform != "" {
77+
p, err := platforms.Parse(opts.platform)
78+
if err != nil {
79+
return errors.Wrap(err, "invalid platform")
80+
}
81+
options.Platform = &p
82+
}
83+
84+
response, err := dockerCli.Client().ImageLoad(ctx, input, options)
7285
if err != nil {
7386
return err
7487
}

cli/command/image/load_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

@@ -40,6 +42,14 @@ func TestNewLoadCommandErrors(t *testing.T) {
4042
return image.LoadResponse{}, errors.Errorf("something went wrong")
4143
},
4244
},
45+
{
46+
name: "invalid platform",
47+
args: []string{"--platform", "<invalid>"},
48+
expectedError: `invalid platform`,
49+
imageLoadFunc: func(input io.Reader, options image.LoadOptions) (image.LoadResponse, error) {
50+
return image.LoadResponse{}, nil
51+
},
52+
},
4353
}
4454
for _, tc := range testCases {
4555
tc := tc
@@ -96,6 +106,14 @@ func TestNewLoadCommandSuccess(t *testing.T) {
96106
return image.LoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil
97107
},
98108
},
109+
{
110+
name: "with platform",
111+
args: []string{"--platform", "linux/amd64"},
112+
imageLoadFunc: func(input io.Reader, options image.LoadOptions) (image.LoadResponse, error) {
113+
assert.Check(t, is.DeepEqual(ocispec.Platform{OS: "linux", Architecture: "amd64"}, *options.Platform))
114+
return image.LoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil
115+
},
116+
},
99117
}
100118
for _, tc := range testCases {
101119
tc := tc
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Success

docs/reference/commandline/image_load.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ Load an image from a tar archive or STDIN
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:------------------------------------|:---------|:--------|:---------------------------------------------|
14-
| [`-i`](#input), [`--input`](#input) | `string` | | Read from tar archive file, instead of STDIN |
15-
| `-q`, `--quiet` | `bool` | | Suppress the load output |
12+
| Name | Type | Default | Description |
13+
|:------------------------------------|:---------|:--------|:-----------------------------------------------------------------------------------------------|
14+
| [`-i`](#input), [`--input`](#input) | `string` | | Read from tar archive file, instead of STDIN |
15+
| [`--platform`](#platform) | `string` | | Load only the given platform variant. Formatted as `os[/arch[/variant]]` (e.g., `linux/amd64`) |
16+
| `-q`, `--quiet` | `bool` | | Suppress the load output |
1617

1718

1819
<!---MARKER_GEN_END-->
@@ -58,3 +59,32 @@ fedora 20 58394af37342 7 weeks ago
5859
fedora heisenbug 58394af37342 7 weeks ago 385.5 MB
5960
fedora latest 58394af37342 7 weeks ago 385.5 MB
6061
```
62+
63+
64+
### <a name="platform"></a> Load a specific platform (--platform)
65+
66+
The `--platform` option allows you to specify which platform variant of the
67+
image to load. By default, `docker load` loads all platform variants that
68+
are present in the archive. Use the `--platform` option to specify which
69+
platform variant of the image to load. An error is produced if the given
70+
platform is not present in the archive.
71+
72+
The platform option takes the `os[/arch[/variant]]` format; for example,
73+
`linux/amd64` or `linux/arm64/v8`. Architecture and variant are optional,
74+
and default to the daemon's native architecture if omitted.
75+
76+
The following example loads the `linux/amd64` variant of an `alpine` image
77+
from an archive that contains multiple platform variants.
78+
79+
```console
80+
$ docker image load -i image.tar --platform=linux/amd64
81+
Loaded image: alpine:latest
82+
```
83+
84+
The following example attempts to load a `linux/ppc64le` image from an
85+
archive, but the given platform is not present in the archive;
86+
87+
```console
88+
$ docker image load -i image.tar --platform=linux/ppc64le
89+
requested platform (linux/ppc64le) not found: image might be filtered out
90+
```

docs/reference/commandline/load.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ Load an image from a tar archive or STDIN
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:----------------|:---------|:--------|:---------------------------------------------|
14-
| `-i`, `--input` | `string` | | Read from tar archive file, instead of STDIN |
15-
| `-q`, `--quiet` | `bool` | | Suppress the load output |
12+
| Name | Type | Default | Description |
13+
|:----------------|:---------|:--------|:-----------------------------------------------------------------------------------------------|
14+
| `-i`, `--input` | `string` | | Read from tar archive file, instead of STDIN |
15+
| `--platform` | `string` | | Load only the given platform variant. Formatted as `os[/arch[/variant]]` (e.g., `linux/amd64`) |
16+
| `-q`, `--quiet` | `bool` | | Suppress the load output |
1617

1718

1819
<!---MARKER_GEN_END-->

0 commit comments

Comments
 (0)