mirror of https://github.com/docker/cli.git
Improve flow pull behavior before container creation.
- Also improve test coverage Signed-off-by: Zander Mackie <zmackie@gmail.com>
This commit is contained in:
parent
a06b5db594
commit
965664d89b
|
@ -223,37 +223,10 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerConfig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//create the container
|
//create the container, pulling the image (or not) based on opts.pull
|
||||||
var response container.ContainerCreateCreatedBody
|
var response container.ContainerCreateCreatedBody
|
||||||
if opts.pull == PullImageMissing { // Pull image only if it does not exist locally. Default.
|
|
||||||
response, err = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name)
|
|
||||||
|
|
||||||
//if image not found try to pull it
|
if opts.pull == PullImageAlways {
|
||||||
if err != nil {
|
|
||||||
if apiclient.IsErrNotFound(err) && namedRef != nil {
|
|
||||||
fmt.Fprintf(stderr, "Unable to find image '%s' locally\n", reference.FamiliarString(namedRef))
|
|
||||||
|
|
||||||
// we don't want to write to stdout anything apart from container.ID
|
|
||||||
if err := pullImage(ctx, dockerCli, config.Image, opts.platform, stderr); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil {
|
|
||||||
if err := image.TagTrusted(ctx, dockerCli, trustedRef, taggedRef); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Retry
|
|
||||||
var retryErr error
|
|
||||||
response, retryErr = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name)
|
|
||||||
if retryErr != nil {
|
|
||||||
return nil, retryErr
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if opts.pull == PullImageAlways { // Always try and pull the image.
|
|
||||||
if err := pullImage(ctx, dockerCli, config.Image, opts.platform, stderr); err != nil {
|
if err := pullImage(ctx, dockerCli, config.Image, opts.platform, stderr); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -262,21 +235,32 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerConfig
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response, err = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name)
|
}
|
||||||
if err != nil {
|
|
||||||
|
response, err = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// Pull image if it does not exist locally and we have the PullImageMissing option. Default behavior.
|
||||||
|
if apiclient.IsErrNotFound(err) && namedRef != nil && opts.pull == PullImageMissing {
|
||||||
|
fmt.Fprintf(stderr, "Unable to find image '%s' locally\n", reference.FamiliarString(namedRef))
|
||||||
|
// we don't want to write to stdout anything apart from container.ID
|
||||||
|
if err := pullImage(ctx, dockerCli, config.Image, opts.platform, stderr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil {
|
||||||
|
if err := image.TagTrusted(ctx, dockerCli, trustedRef, taggedRef); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var retryErr error
|
||||||
|
response, retryErr = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name)
|
||||||
|
if retryErr != nil {
|
||||||
|
return nil, retryErr
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if opts.pull == PullImageNever { // Never try and pull the image
|
|
||||||
response, err = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if apiclient.IsErrNotFound(err) && namedRef != nil {
|
|
||||||
fmt.Fprintf(stderr, "Unable to find image '%s' locally\nWill not pull due to '%s'", reference.FamiliarString(namedRef), opts.pull)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else { // We got something weird
|
|
||||||
return nil, errors.Errorf("Unknown pull option : %s", opts.pull)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, warning := range response.Warnings {
|
for _, warning := range response.Warnings {
|
||||||
|
|
|
@ -158,17 +158,58 @@ func TestCreateContainerNeverPullsImage(t *testing.T) {
|
||||||
},
|
},
|
||||||
HostConfig: &container.HostConfig{},
|
HostConfig: &container.HostConfig{},
|
||||||
}
|
}
|
||||||
body, err := createContainer(context.Background(), cli, config, &createOptions{
|
_, err := createContainer(context.Background(), cli, config, &createOptions{
|
||||||
name: "name",
|
name: "name",
|
||||||
platform: runtime.GOOS,
|
platform: runtime.GOOS,
|
||||||
untrusted: true,
|
untrusted: true,
|
||||||
pull: PullImageNever,
|
pull: PullImageNever,
|
||||||
})
|
})
|
||||||
|
assert.ErrorContains(t, err, "fake not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateContainerAlwaysPullsImage(t *testing.T) {
|
||||||
|
imageName := "does-not-exist-locally"
|
||||||
|
responseCounter := 0
|
||||||
|
containerID := "abcdef"
|
||||||
|
|
||||||
|
client := &fakeClient{
|
||||||
|
createContainerFunc: func(
|
||||||
|
config *container.Config,
|
||||||
|
hostConfig *container.HostConfig,
|
||||||
|
networkingConfig *network.NetworkingConfig,
|
||||||
|
containerName string,
|
||||||
|
) (container.ContainerCreateCreatedBody, error) {
|
||||||
|
defer func() { responseCounter++ }()
|
||||||
|
switch responseCounter {
|
||||||
|
case 0:
|
||||||
|
return container.ContainerCreateCreatedBody{ID: containerID}, nil
|
||||||
|
default:
|
||||||
|
return container.ContainerCreateCreatedBody{}, errors.New("unexpected")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
|
||||||
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
||||||
|
},
|
||||||
|
infoFunc: func() (types.Info, error) {
|
||||||
|
return types.Info{IndexServerAddress: "http://indexserver"}, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cli := test.NewFakeCli(client)
|
||||||
|
config := &containerConfig{
|
||||||
|
Config: &container.Config{
|
||||||
|
Image: imageName,
|
||||||
|
},
|
||||||
|
HostConfig: &container.HostConfig{},
|
||||||
|
}
|
||||||
|
body, err := createContainer(context.Background(), cli, config, &createOptions{
|
||||||
|
name: "name",
|
||||||
|
platform: runtime.GOOS,
|
||||||
|
untrusted: true,
|
||||||
|
pull: PullImageAlways,
|
||||||
|
})
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
expected := container.ContainerCreateCreatedBody{}
|
expected := container.ContainerCreateCreatedBody{ID: containerID}
|
||||||
assert.Check(t, is.DeepEqual(expected, *body))
|
assert.Check(t, is.DeepEqual(expected, *body))
|
||||||
stderr := cli.ErrBuffer().String()
|
|
||||||
assert.Check(t, is.Contains(stderr, "Unable to find image 'does-not-exist-locally:latest' locally"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
|
func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue