Skip to content

Commit 9058ec3

Browse files
authored
Merge pull request moby#27596 from aaronlehmann/rolling-restart
Add force option to service update
2 parents b54a1d8 + c9fdf9a commit 9058ec3

File tree

8 files changed

+50
-6
lines changed

8 files changed

+50
-6
lines changed

api/types/swarm/task.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ type TaskSpec struct {
6161
// spec. If not present, the one on cluster default on swarm.Spec will be
6262
// used, finally falling back to the engine default if not specified.
6363
LogDriver *Driver `json:",omitempty"`
64+
65+
// ForceUpdate is a counter that triggers an update even if no relevant
66+
// parameters have been changed.
67+
ForceUpdate uint64
6468
}
6569

6670
// Resources represents resources (CPU/Memory).

cli/command/service/update.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func newUpdateCommand(dockerCli *command.DockerCli) *cobra.Command {
3737
flags.String("image", "", "Service image tag")
3838
flags.String("args", "", "Service command args")
3939
flags.Bool("rollback", false, "Rollback to previous specification")
40+
flags.Bool("force", false, "Force update even if no changes require it")
4041
addServiceFlags(cmd, opts)
4142

4243
flags.Var(newListOptsVar(), flagEnvRemove, "Remove an environment variable")
@@ -257,6 +258,15 @@ func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
257258
return err
258259
}
259260

261+
force, err := flags.GetBool("force")
262+
if err != nil {
263+
return err
264+
}
265+
266+
if force {
267+
spec.TaskTemplate.ForceUpdate++
268+
}
269+
260270
return nil
261271
}
262272

contrib/completion/bash/docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,7 @@ _docker_service_update() {
24732473
--constraint
24742474
--endpoint-mode
24752475
--env -e
2476+
--force
24762477
--group-add
24772478
--label -l
24782479
--limit-cpu

contrib/completion/zsh/_docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ __docker_service_subcommand() {
11851185
"($help)--arg=[Service command args]:arguments: _normal" \
11861186
"($help)*--container-label-add=[Add or update container labels]:label: " \
11871187
"($help)*--container-label-rm=[Remove a container label by its key]:label: " \
1188+
"($help)--force[Force update]" \
11881189
"($help)*--group-rm=[Remove previously added user groups from the container]:group:_groups" \
11891190
"($help)--image=[Service image tag]:image:__docker_repositories" \
11901191
"($help)--rollback[Rollback to previous specification]" \

daemon/cluster/convert/service.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func serviceSpecFromGRPC(spec *swarmapi.ServiceSpec) *types.ServiceSpec {
7474
Placement: placementFromGRPC(spec.Task.Placement),
7575
LogDriver: driverFromGRPC(spec.Task.LogDriver),
7676
Networks: taskNetworks,
77+
ForceUpdate: spec.Task.ForceUpdate,
7778
},
7879

7980
Networks: serviceNetworks,
@@ -136,9 +137,10 @@ func ServiceSpecToGRPC(s types.ServiceSpec) (swarmapi.ServiceSpec, error) {
136137
Labels: s.Labels,
137138
},
138139
Task: swarmapi.TaskSpec{
139-
Resources: resourcesToGRPC(s.TaskTemplate.Resources),
140-
LogDriver: driverToGRPC(s.TaskTemplate.LogDriver),
141-
Networks: taskNetworks,
140+
Resources: resourcesToGRPC(s.TaskTemplate.Resources),
141+
LogDriver: driverToGRPC(s.TaskTemplate.LogDriver),
142+
Networks: taskNetworks,
143+
ForceUpdate: s.TaskTemplate.ForceUpdate,
142144
},
143145
Networks: serviceNetworks,
144146
}

docs/reference/api/docker_remote_api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ This section lists each version from latest to oldest. Each listing includes a
149149
containers that are tasks (part of a service in swarm mode).
150150
* `POST /containers/create` now takes `StopTimeout` field.
151151
* `POST /services/create` and `POST /services/(id or name)/update` now accept `Monitor` and `MaxFailureRatio` parameters, which control the response to failures during service updates.
152+
* `POST /services/(id or name)/update` now accepts a `ForceUpdate` parameter inside the `TaskTemplate`, which causes the service to be updated even if there are no changes which would ordinarily trigger an update.
152153
* `GET /networks/(name)` now returns field `Created` in response to show network created time.
153154
* `POST /containers/(id or name)/exec` now accepts an `Env` field, which holds a list of environment variables to be set in the context of the command execution.
154155
* `GET /volumes`, `GET /volumes/(name)`, and `POST /volumes/create` now return the `Options` field which holds the driver specific options to use for when creating the volume.

docs/reference/api/docker_remote_api_v1.25.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4915,7 +4915,8 @@ List services
49154915
"Condition": "any",
49164916
"MaxAttempts": 0
49174917
},
4918-
"Placement": {}
4918+
"Placement": {},
4919+
"ForceUpdate": 0
49194920
},
49204921
"Mode": {
49214922
"Replicated": {
@@ -5038,7 +5039,8 @@ image](#create-an-image) section for more details.
50385039
"Condition": "on-failure",
50395040
"Delay": 10000000000.0,
50405041
"MaxAttempts": 10
5041-
}
5042+
},
5043+
"ForceUpdate": 0
50425044
},
50435045
"Mode": {
50445046
"Replicated": {
@@ -5132,6 +5134,7 @@ image](#create-an-image) section for more details.
51325134
- **Window** – Windows is the time window used to evaluate the restart policy (default value is
51335135
0, which is unbounded).
51345136
- **Placement** – An array of constraints.
5137+
- **ForceUpdate**: A counter that triggers an update even if no relevant parameters have been changed.
51355138
- **Mode** – Scheduling mode for the service (`replicated` or `global`, defaults to `replicated`).
51365139
- **UpdateConfig** – Specification for the update strategy of the service.
51375140
- **Parallelism** – Maximum number of tasks to be updated in one iteration (0 means unlimited
@@ -5303,7 +5306,8 @@ image](#create-an-image) section for more details.
53035306
"Condition": "any",
53045307
"MaxAttempts": 0
53055308
},
5306-
"Placement": {}
5309+
"Placement": {},
5310+
"ForceUpdate": 0
53075311
},
53085312
"Mode": {
53095313
"Replicated": {
@@ -5374,6 +5378,7 @@ image](#create-an-image) section for more details.
53745378
- **Window** – Windows is the time window used to evaluate the restart policy (default value is
53755379
0, which is unbounded).
53765380
- **Placement** – An array of constraints.
5381+
- **ForceUpdate**: A counter that triggers an update even if no relevant parameters have been changed.
53775382
- **Mode** – Scheduling mode for the service (`replicated` or `global`, defaults to `replicated`).
53785383
- **UpdateConfig** – Specification for the update strategy of the service.
53795384
- **Parallelism** – Maximum number of tasks to be updated in one iteration (0 means unlimited

docs/reference/commandline/service_update.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Options:
2929
--endpoint-mode string Endpoint mode (vip or dnsrr)
3030
--env-add value Add or update environment variables (default [])
3131
--env-rm value Remove an environment variable (default [])
32+
--force Force update even if no changes require it
3233
--group-add value Add additional user groups to the container (default [])
3334
--group-rm value Remove previously added user groups from the container (default [])
3435
--help Print usage
@@ -67,6 +68,12 @@ Updates a service as described by the specified parameters. This command has to
6768
The parameters are the same as [`docker service create`](service_create.md). Please look at the description there
6869
for further information.
6970

71+
Normally, updating a service will only cause the service's tasks to be replaced with new ones if a change to the
72+
service requires recreating the tasks for it to take effect. For example, only changing the
73+
`--update-parallelism` setting will not recreate the tasks, because the individual tasks are not affected by this
74+
setting. However, the `--force` flag will cause the tasks to be recreated anyway. This can be used to perform a
75+
rolling restart without any changes to the service parameters.
76+
7077
## Examples
7178

7279
### Update a service
@@ -75,6 +82,19 @@ for further information.
7582
$ docker service update --limit-cpu 2 redis
7683
```
7784

85+
### Perform a rolling restart with no parameter changes
86+
87+
```bash
88+
$ docker service update --force --update-parallelism 1 --update-delay 30s redis
89+
```
90+
91+
In this example, the `--force` flag causes the service's tasks to be shut down
92+
and replaced with new ones even though none of the other parameters would
93+
normally cause that to happen. The `--update-parallelism 1` setting ensures
94+
that only one task is replaced at a time (this is the default behavior). The
95+
`--update-delay 30s` setting introduces a 30 second delay between tasks, so
96+
that the rolling restart happens gradually.
97+
7898
### Adding and removing mounts
7999

80100
Use the `--mount-add` or `--mount-rm` options add or remove a service's bind-mounts

0 commit comments

Comments
 (0)