Forbid update restart policy of container with AutoRemove flag

"--restart" and "--rm" are conflict options, if a container is started
with AutoRemove flag, we should forbid the update action for its Restart
Policy.

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
This commit is contained in:
Zhang Wei 2016-08-15 16:38:47 +08:00
parent 5ac0342e82
commit 4754c64ab5
7 changed files with 28 additions and 4 deletions

View File

@ -95,6 +95,9 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions,
return cli.StatusError{StatusCode: 125} return cli.StatusError{StatusCode: 125}
} }
if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
return ErrConflictRestartPolicyAndAutoRemove
}
if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 { if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 {
fmt.Fprintf(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.\n") fmt.Fprintf(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.\n")
} }
@ -166,9 +169,6 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions,
fmt.Fprintf(stdout, "%s\n", createResponse.ID) fmt.Fprintf(stdout, "%s\n", createResponse.ID)
}() }()
} }
if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
return ErrConflictRestartPolicyAndAutoRemove
}
attach := config.AttachStdin || config.AttachStdout || config.AttachStderr attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
if attach { if attach {
var ( var (

View File

@ -302,6 +302,9 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi
// update HostConfig of container // update HostConfig of container
if hostConfig.RestartPolicy.Name != "" { if hostConfig.RestartPolicy.Name != "" {
if container.HostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
return fmt.Errorf("Restart policy cannot be updated because AutoRemove is enabled for the container")
}
container.HostConfig.RestartPolicy = hostConfig.RestartPolicy container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
} }

View File

@ -72,6 +72,9 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi
} }
// update HostConfig of container // update HostConfig of container
if hostConfig.RestartPolicy.Name != "" { if hostConfig.RestartPolicy.Name != "" {
if container.HostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
return fmt.Errorf("Restart policy cannot be updated because AutoRemove is enabled for the container")
}
container.HostConfig.RestartPolicy = hostConfig.RestartPolicy container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
} }
return nil return nil

View File

@ -460,7 +460,7 @@ Create a container
An ever increasing delay (double the previous delay, starting at 100mS) An ever increasing delay (double the previous delay, starting at 100mS)
is added before each restart to prevent flooding the server. is added before each restart to prevent flooding the server.
- **AutoRemove** - Boolean value, set to `true` to automatically remove the container on daemon side - **AutoRemove** - Boolean value, set to `true` to automatically remove the container on daemon side
when the container's process exits. when the container's process exits. Note that `RestartPolicy` other than `none` is exclusive to `AutoRemove`.
- **UsernsMode** - Sets the usernamespace mode for the container when usernamespace remapping option is enabled. - **UsernsMode** - Sets the usernamespace mode for the container when usernamespace remapping option is enabled.
supported values are: `host`. supported values are: `host`.
- **NetworkMode** - Sets the networking mode for the container. Supported - **NetworkMode** - Sets the networking mode for the container. Supported

View File

@ -107,3 +107,7 @@ To update restart policy for one or more containers:
```bash ```bash
$ docker update --restart=on-failure:3 abebf7571666 hopeful_morse $ docker update --restart=on-failure:3 abebf7571666 hopeful_morse
``` ```
Note that if the container is started with "--rm" flag, you cannot update the restart
policy for it. The `AutoRemove` and `RestartPolicy` are mutually exclusive for the
container.

View File

@ -29,3 +29,13 @@ func (s *DockerSuite) TestUpdateRestartPolicy(c *check.C) {
maximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") maximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
c.Assert(maximumRetryCount, checker.Equals, "5") c.Assert(maximumRetryCount, checker.Equals, "5")
} }
func (s *DockerSuite) TestUpdateRestartWithAutoRemoveFlag(c *check.C) {
out, _ := runSleepingContainer(c, "--rm")
id := strings.TrimSpace(out)
// update restart policy for an AutoRemove container
out, _, err := dockerCmdWithError("update", "--restart=always", id)
c.Assert(err, checker.NotNil)
c.Assert(out, checker.Contains, "Restart policy cannot be updated because AutoRemove is enabled for the container")
}

View File

@ -148,3 +148,7 @@ To update restart policy for one or more containers:
```bash ```bash
$ docker update --restart=on-failure:3 abebf7571666 hopeful_morse $ docker update --restart=on-failure:3 abebf7571666 hopeful_morse
``` ```
Note that if the container is started with "--rm" flag, you cannot update the restart
policy for it. The `AutoRemove` and `RestartPolicy` are mutually exclusive for the
container.