Skip to content

Commit a697a06

Browse files
committed
introduce pull --missing flag to only pull images not present in cache
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 8af49ff commit a697a06

File tree

10 files changed

+147
-60
lines changed

10 files changed

+147
-60
lines changed

cmd/compose/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func createCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service
7272
}
7373
flags := cmd.Flags()
7474
flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
75-
flags.BoolVar(&opts.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
76-
flags.StringVar(&opts.Pull, "pull", "missing", `Pull image before running ("always"|"missing"|"never")`)
75+
flags.BoolVar(&opts.noBuild, "no-build", false, "Don't build an image, even if it's policy.")
76+
flags.StringVar(&opts.Pull, "pull", "policy", `Pull image before running ("always"|"policy"|"never")`)
7777
flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
7878
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
7979
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
@@ -145,7 +145,7 @@ func (opts createOptions) Apply(project *types.Project) error {
145145
}
146146
// N.B. opts.Build means "force build all", but images can still be built
147147
// when this is false
148-
// e.g. if a service has pull_policy: build or its local image is missing
148+
// e.g. if a service has pull_policy: build or its local image is policy
149149
if opts.Build {
150150
for i, service := range project.Services {
151151
if service.Build == nil {

cmd/compose/pull.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type pullOptions struct {
3838
includeDeps bool
3939
ignorePullFailures bool
4040
noBuildable bool
41+
policy string
4142
}
4243

4344
func pullCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
@@ -67,20 +68,39 @@ func pullCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service)
6768
flags.MarkHidden("no-parallel") //nolint:errcheck
6869
cmd.Flags().BoolVar(&opts.ignorePullFailures, "ignore-pull-failures", false, "Pull what it can and ignores images with pull failures.")
6970
cmd.Flags().BoolVar(&opts.noBuildable, "ignore-buildable", false, "Ignore images that can be built.")
71+
cmd.Flags().StringVar(&opts.policy, "policy", "", `Apply pull policy ("missing"|"always").`)
7072
return cmd
7173
}
7274

75+
func (opts pullOptions) apply(project *types.Project, services []string) error {
76+
if !opts.includeDeps {
77+
err := project.ForServices(services, types.IgnoreDependencies)
78+
if err != nil {
79+
return err
80+
}
81+
}
82+
83+
if opts.policy != "" {
84+
for i, service := range project.Services {
85+
if service.Image == "" {
86+
continue
87+
}
88+
service.PullPolicy = opts.policy
89+
project.Services[i] = service
90+
}
91+
}
92+
return nil
93+
}
94+
7395
func runPull(ctx context.Context, dockerCli command.Cli, backend api.Service, opts pullOptions, services []string) error {
7496
project, err := opts.ToProject(dockerCli, services)
7597
if err != nil {
7698
return err
7799
}
78100

79-
if !opts.includeDeps {
80-
err := project.ForServices(services, types.IgnoreDependencies)
81-
if err != nil {
82-
return err
83-
}
101+
err = opts.apply(project, services)
102+
if err != nil {
103+
return err
84104
}
85105

86106
return backend.Pull(ctx, project, api.PullOptions{

cmd/compose/pullOptions_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2023 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"testing"
21+
22+
"github.com/compose-spec/compose-go/types"
23+
"gotest.tools/v3/assert"
24+
)
25+
26+
func TestApplyPullOptions(t *testing.T) {
27+
project := &types.Project{
28+
Services: []types.ServiceConfig{
29+
{
30+
Name: "must-build",
31+
// No image, local build only
32+
Build: &types.BuildConfig{
33+
Context: ".",
34+
},
35+
},
36+
{
37+
Name: "has-build",
38+
Image: "registry.example.com/myservice",
39+
Build: &types.BuildConfig{
40+
Context: ".",
41+
},
42+
},
43+
{
44+
Name: "must-pull",
45+
Image: "registry.example.com/another-service",
46+
},
47+
},
48+
}
49+
err := pullOptions{
50+
policy: types.PullPolicyMissing,
51+
}.apply(project, nil)
52+
assert.NilError(t, err)
53+
54+
assert.Equal(t, project.Services[0].PullPolicy, "") // still default
55+
assert.Equal(t, project.Services[1].PullPolicy, types.PullPolicyMissing)
56+
assert.Equal(t, project.Services[2].PullPolicy, types.PullPolicyMissing)
57+
}

cmd/compose/up.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *c
101101
flags := upCmd.Flags()
102102
flags.BoolVarP(&up.Detach, "detach", "d", false, "Detached mode: Run containers in the background")
103103
flags.BoolVar(&create.Build, "build", false, "Build images before starting containers.")
104-
flags.BoolVar(&create.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
105-
flags.StringVar(&create.Pull, "pull", "missing", `Pull image before running ("always"|"missing"|"never")`)
104+
flags.BoolVar(&create.noBuild, "no-build", false, "Don't build an image, even if it's policy.")
105+
flags.StringVar(&create.Pull, "pull", "policy", `Pull image before running ("always"|"policy"|"never")`)
106106
flags.BoolVar(&create.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
107107
flags.StringArrayVar(&create.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
108108
flags.BoolVar(&up.noColor, "no-color", false, "Produce monochrome output.")

docs/reference/compose_create.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ Creates containers for a service.
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:-------------------|:--------------|:----------|:----------------------------------------------------------------------------------------------|
10-
| `--build` | | | Build images before starting containers. |
11-
| `--dry-run` | | | Execute command in dry run mode |
12-
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
13-
| `--no-build` | | | Don't build an image, even if it's missing. |
14-
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
15-
| `--pull` | `string` | `missing` | Pull image before running ("always"\|"missing"\|"never") |
16-
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
17-
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
8+
| Name | Type | Default | Description |
9+
|:-------------------|:--------------|:---------|:----------------------------------------------------------------------------------------------|
10+
| `--build` | | | Build images before starting containers. |
11+
| `--dry-run` | | | Execute command in dry run mode |
12+
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
13+
| `--no-build` | | | Don't build an image, even if it's policy. |
14+
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
15+
| `--pull` | `string` | `policy` | Pull image before running ("always"\|"policy"\|"never") |
16+
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
17+
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
1818

1919

2020
<!---MARKER_GEN_END-->

docs/reference/compose_pull.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ Pull service images
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:-------------------------|:-----|:--------|:--------------------------------------------------------|
10-
| `--dry-run` | | | Execute command in dry run mode |
11-
| `--ignore-buildable` | | | Ignore images that can be built. |
12-
| `--ignore-pull-failures` | | | Pull what it can and ignores images with pull failures. |
13-
| `--include-deps` | | | Also pull services declared as dependencies. |
14-
| `-q`, `--quiet` | | | Pull without printing progress information. |
8+
| Name | Type | Default | Description |
9+
|:-------------------------|:---------|:--------|:--------------------------------------------------------|
10+
| `--dry-run` | | | Execute command in dry run mode |
11+
| `--ignore-buildable` | | | Ignore images that can be built. |
12+
| `--ignore-pull-failures` | | | Pull what it can and ignores images with pull failures. |
13+
| `--include-deps` | | | Also pull services declared as dependencies. |
14+
| `--policy` | `string` | | Apply pull policy ("missing"\|"always"). |
15+
| `-q`, `--quiet` | | | Pull without printing progress information. |
1516

1617

1718
<!---MARKER_GEN_END-->

docs/reference/compose_up.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@ Create and start containers
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:-----------------------------|:--------------|:----------|:---------------------------------------------------------------------------------------------------------|
10-
| `--abort-on-container-exit` | | | Stops all containers if any container was stopped. Incompatible with -d |
11-
| `--always-recreate-deps` | | | Recreate dependent containers. Incompatible with --no-recreate. |
12-
| `--attach` | `stringArray` | | Restrict attaching to the specified services. Incompatible with --attach-dependencies. |
13-
| `--attach-dependencies` | | | Automatically attach to log output of dependent services. |
14-
| `--build` | | | Build images before starting containers. |
15-
| `-d`, `--detach` | | | Detached mode: Run containers in the background |
16-
| `--dry-run` | | | Execute command in dry run mode |
17-
| `--exit-code-from` | `string` | | Return the exit code of the selected service container. Implies --abort-on-container-exit |
18-
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
19-
| `--no-attach` | `stringArray` | | Do not attach (stream logs) to the specified services. |
20-
| `--no-build` | | | Don't build an image, even if it's missing. |
21-
| `--no-color` | | | Produce monochrome output. |
22-
| `--no-deps` | | | Don't start linked services. |
23-
| `--no-log-prefix` | | | Don't print prefix in logs. |
24-
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
25-
| `--no-start` | | | Don't start the services after creating them. |
26-
| `--pull` | `string` | `missing` | Pull image before running ("always"\|"missing"\|"never") |
27-
| `--quiet-pull` | | | Pull without printing progress information. |
28-
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
29-
| `-V`, `--renew-anon-volumes` | | | Recreate anonymous volumes instead of retrieving data from the previous containers. |
30-
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
31-
| `-t`, `--timeout` | `int` | `0` | Use this timeout in seconds for container shutdown when attached or when containers are already running. |
32-
| `--timestamps` | | | Show timestamps. |
33-
| `--wait` | | | Wait for services to be running\|healthy. Implies detached mode. |
34-
| `--wait-timeout` | `int` | `0` | Maximum duration to wait for the project to be running\|healthy. |
8+
| Name | Type | Default | Description |
9+
|:-----------------------------|:--------------|:---------|:---------------------------------------------------------------------------------------------------------|
10+
| `--abort-on-container-exit` | | | Stops all containers if any container was stopped. Incompatible with -d |
11+
| `--always-recreate-deps` | | | Recreate dependent containers. Incompatible with --no-recreate. |
12+
| `--attach` | `stringArray` | | Restrict attaching to the specified services. Incompatible with --attach-dependencies. |
13+
| `--attach-dependencies` | | | Automatically attach to log output of dependent services. |
14+
| `--build` | | | Build images before starting containers. |
15+
| `-d`, `--detach` | | | Detached mode: Run containers in the background |
16+
| `--dry-run` | | | Execute command in dry run mode |
17+
| `--exit-code-from` | `string` | | Return the exit code of the selected service container. Implies --abort-on-container-exit |
18+
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
19+
| `--no-attach` | `stringArray` | | Do not attach (stream logs) to the specified services. |
20+
| `--no-build` | | | Don't build an image, even if it's policy. |
21+
| `--no-color` | | | Produce monochrome output. |
22+
| `--no-deps` | | | Don't start linked services. |
23+
| `--no-log-prefix` | | | Don't print prefix in logs. |
24+
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
25+
| `--no-start` | | | Don't start the services after creating them. |
26+
| `--pull` | `string` | `policy` | Pull image before running ("always"\|"policy"\|"never") |
27+
| `--quiet-pull` | | | Pull without printing progress information. |
28+
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
29+
| `-V`, `--renew-anon-volumes` | | | Recreate anonymous volumes instead of retrieving data from the previous containers. |
30+
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
31+
| `-t`, `--timeout` | `int` | `0` | Use this timeout in seconds for container shutdown when attached or when containers are already running. |
32+
| `--timestamps` | | | Show timestamps. |
33+
| `--wait` | | | Wait for services to be running\|healthy. Implies detached mode. |
34+
| `--wait-timeout` | `int` | `0` | Maximum duration to wait for the project to be running\|healthy. |
3535

3636

3737
<!---MARKER_GEN_END-->

docs/reference/docker_compose_create.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ options:
2929
- option: no-build
3030
value_type: bool
3131
default_value: "false"
32-
description: Don't build an image, even if it's missing.
32+
description: Don't build an image, even if it's policy.
3333
deprecated: false
3434
hidden: false
3535
experimental: false
@@ -49,8 +49,8 @@ options:
4949
swarm: false
5050
- option: pull
5151
value_type: string
52-
default_value: missing
53-
description: Pull image before running ("always"|"missing"|"never")
52+
default_value: policy
53+
description: Pull image before running ("always"|"policy"|"never")
5454
deprecated: false
5555
hidden: false
5656
experimental: false

0 commit comments

Comments
 (0)