From 4754c64ab5b5bf5ae6f8a3040606296910e6cb19 Mon Sep 17 00:00:00 2001 From: Zhang Wei Date: Mon, 15 Aug 2016 16:38:47 +0800 Subject: [PATCH] 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 --- api/client/container/run.go | 6 +++--- container/container_unix.go | 3 +++ container/container_windows.go | 3 +++ docs/reference/api/docker_remote_api_v1.25.md | 2 +- docs/reference/commandline/update.md | 4 ++++ integration-cli/docker_cli_update_test.go | 10 ++++++++++ man/docker-update.1.md | 4 ++++ 7 files changed, 28 insertions(+), 4 deletions(-) diff --git a/api/client/container/run.go b/api/client/container/run.go index ee62b60867..70755b433d 100644 --- a/api/client/container/run.go +++ b/api/client/container/run.go @@ -95,6 +95,9 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions, return cli.StatusError{StatusCode: 125} } + if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() { + return ErrConflictRestartPolicyAndAutoRemove + } 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") } @@ -166,9 +169,6 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions, fmt.Fprintf(stdout, "%s\n", createResponse.ID) }() } - if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() { - return ErrConflictRestartPolicyAndAutoRemove - } attach := config.AttachStdin || config.AttachStdout || config.AttachStderr if attach { var ( diff --git a/container/container_unix.go b/container/container_unix.go index 9219ca1569..c5915b4569 100644 --- a/container/container_unix.go +++ b/container/container_unix.go @@ -302,6 +302,9 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi // update HostConfig of container 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 } diff --git a/container/container_windows.go b/container/container_windows.go index 38560bb59a..14f90c5f68 100644 --- a/container/container_windows.go +++ b/container/container_windows.go @@ -72,6 +72,9 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi } // update HostConfig of container 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 } return nil diff --git a/docs/reference/api/docker_remote_api_v1.25.md b/docs/reference/api/docker_remote_api_v1.25.md index e1ff26f123..79492de873 100644 --- a/docs/reference/api/docker_remote_api_v1.25.md +++ b/docs/reference/api/docker_remote_api_v1.25.md @@ -460,7 +460,7 @@ Create a container An ever increasing delay (double the previous delay, starting at 100mS) is added before each restart to prevent flooding the server. - **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. supported values are: `host`. - **NetworkMode** - Sets the networking mode for the container. Supported diff --git a/docs/reference/commandline/update.md b/docs/reference/commandline/update.md index da7162848d..3eb2b7deac 100644 --- a/docs/reference/commandline/update.md +++ b/docs/reference/commandline/update.md @@ -107,3 +107,7 @@ To update restart policy for one or more containers: ```bash $ 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. diff --git a/integration-cli/docker_cli_update_test.go b/integration-cli/docker_cli_update_test.go index 188030ffbb..0b31bb45ff 100644 --- a/integration-cli/docker_cli_update_test.go +++ b/integration-cli/docker_cli_update_test.go @@ -29,3 +29,13 @@ func (s *DockerSuite) TestUpdateRestartPolicy(c *check.C) { maximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 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") +} diff --git a/man/docker-update.1.md b/man/docker-update.1.md index ad86297a07..71fb6e476a 100644 --- a/man/docker-update.1.md +++ b/man/docker-update.1.md @@ -148,3 +148,7 @@ To update restart policy for one or more containers: ```bash $ 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.