diff --git a/daemon/daemon.go b/daemon/daemon.go index ccf254b2cf..e5fd6f9395 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -1226,6 +1226,10 @@ func checkKernel() error { func (daemon *Daemon) verifyHostConfig(hostConfig *runconfig.HostConfig) ([]string, error) { var warnings []string + if hostConfig == nil { + return warnings, nil + } + if hostConfig.LxcConf.Len() > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") { return warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name()) } diff --git a/daemon/start.go b/daemon/start.go index dbb3dd1810..d3af073a88 100644 --- a/daemon/start.go +++ b/daemon/start.go @@ -20,6 +20,10 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf return fmt.Errorf("Container already started") } + if _, err = daemon.verifyHostConfig(hostConfig); err != nil { + return err + } + // This is kept for backward compatibility - hostconfig should be passed when // creating a container, not during start. if hostConfig != nil { diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index b76eb6ba2a..1dea478452 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -817,3 +817,54 @@ func TestContainerApiPostCreateNull(t *testing.T) { logDone("containers REST API - Create Null") } + +func TestCreateWithTooLowMemoryLimit(t *testing.T) { + defer deleteAllContainers() + config := `{ + "Image": "busybox", + "Cmd": "ls", + "OpenStdin": true, + "CpuShares": 100, + "Memory": 524287 + }` + + _, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json") + b, err2 := readBody(body) + if err2 != nil { + t.Fatal(err2) + } + + if err == nil || !strings.Contains(string(b), "Minimum memory limit allowed is 4MB") { + t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!") + } + + logDone("container REST API - create can't set too low memory limit") +} + +func TestStartWithTooLowMemoryLimit(t *testing.T) { + defer deleteAllContainers() + + out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "busybox")) + if err != nil { + t.Fatal(err, out) + } + + containerID := strings.TrimSpace(out) + + config := `{ + "CpuShares": 100, + "Memory": 524287 + }` + + _, body, err := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json") + b, err2 := readBody(body) + if err2 != nil { + t.Fatal(err2) + } + + if err == nil || !strings.Contains(string(b), "Minimum memory limit allowed is 4MB") { + t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!") + } + + logDone("container REST API - start can't set too low memory limit") +} diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index de53fd4941..f2e4eb9b2b 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -3496,15 +3496,3 @@ func TestRunPidHostWithChildIsKillable(t *testing.T) { } logDone("run - can kill container with pid-host and some childs of pid 1") } - -func TestRunWithTooSmallMemoryLimit(t *testing.T) { - defer deleteAllContainers() - // this memory limit is 1 byte less than the min, which is 4MB - // https://github.com/docker/docker/blob/v1.5.0/daemon/create.go#L22 - out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-m", "4194303", "busybox")) - if err == nil || !strings.Contains(out, "Minimum memory limit allowed is 4MB") { - t.Fatalf("expected run to fail when using too low a memory limit: %q", out) - } - - logDone("run - can't set too low memory limit") -}